flutter_settings_screen_ex 0.0.1+5 copy "flutter_settings_screen_ex: ^0.0.1+5" to clipboard
flutter_settings_screen_ex: ^0.0.1+5 copied to clipboard

A Simple plugin to implement settings UI screens for your flutter apps. Customize what you want, From UI elements, behaviour or underlying storage library. This library is a null compliant version of [...]

example/lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:flutter_settings_screen_ex/flutter_settings_screen_ex.dart';

import 'app_settings_page.dart';
import 'cache_provider.dart';

void main() {
  initSettings().then((accentColor) {
    runApp(MyApp(accentColor: accentColor));
  });
}

Future<ValueNotifier<Color>> initSettings() async {
  await Settings.init(
    cacheProvider: _isUsingHive ? HiveCache() : SharePreferenceCache(),
  );
  final accentColor = ValueNotifier(Colors.blueAccent);
  return accentColor;
}

bool _isDarkTheme = true;
bool _isUsingHive = true;

class MyApp extends StatelessWidget {
  final ValueNotifier<Color> accentColor;

  const MyApp({Key? key, required this.accentColor}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(
        title: 'Flutter Demo Home Page',
        accentColor: accentColor,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title, required this.accentColor})
      : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;
  final ValueNotifier<Color> accentColor;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<Color>(
      valueListenable: widget.accentColor,
      builder: (_, color, __) {
        final darkTheme = ThemeData.dark();
        final lightTheme = ThemeData.light();

        return MaterialApp(
          title: 'App Settings Demo',
          theme: _isDarkTheme
              ? darkTheme.copyWith(
                  colorScheme: darkTheme.colorScheme.copyWith(
                    secondary: color,
                  ),
                  toggleableActiveColor: color,
                )
              : lightTheme.copyWith(
                  colorScheme: darkTheme.colorScheme.copyWith(
                    secondary: color,
                  ),
                  toggleableActiveColor: color,
                ),
          home: Scaffold(
            appBar: AppBar(
              title: Text(widget.title),
            ),
            body: Center(
              child: Column(
                children: <Widget>[
                  _buildThemeSwitch(context),
                  _buildPreferenceSwitch(context),
                  const SizedBox(
                    height: 50.0,
                  ),
                  const AppBody(),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildPreferenceSwitch(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        const Text('Shared Pref'),
        Switch(
            activeColor: Theme.of(context).colorScheme.secondary,
            value: _isUsingHive,
            onChanged: (newVal) {
              if (kIsWeb) {
                return;
              }
              _isUsingHive = newVal;
              setState(() {
                initSettings();
              });
            }),
        const Text('Hive Storage'),
      ],
    );
  }

  Widget _buildThemeSwitch(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        const Text('Light Theme'),
        Switch(
            activeColor: Theme.of(context).colorScheme.secondary,
            value: _isDarkTheme,
            onChanged: (newVal) {
              _isDarkTheme = newVal;
              setState(() {});
            }),
        const Text('Dark Theme'),
      ],
    );
  }
}

class AppBody extends StatefulWidget {
  const AppBody({Key? key}) : super(key: key);

  @override
  State<AppBody> createState() => _AppBodyState();
}

class _AppBodyState extends State<AppBody> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _buildClearCacheButton(context),
        const SizedBox(
          height: 25.0,
        ),
        ElevatedButton(
          onPressed: () {
            openAppSettings(context);
          },
          child: const Text('Start Demo'),
        ),
      ],
    );
  }

  void openAppSettings(BuildContext context) {
    Navigator.of(context).push(MaterialPageRoute(
      builder: (context) => const AppSettings(),
    ));
  }

  Widget _buildClearCacheButton(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Settings.clearCache();
        showSnackBar(
          context,
          'Cache cleared for selected cache.',
        );
      },
      child: const Text('Clear selected Cache'),
    );
  }
}

void showSnackBar(BuildContext context, String message) {
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Text(
        message,
        style: const TextStyle(
          color: Colors.white,
        ),
      ),
      backgroundColor: Theme.of(context).primaryColor,
    ),
  );
}
4
likes
80
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

A Simple plugin to implement settings UI screens for your flutter apps. Customize what you want, From UI elements, behaviour or underlying storage library. This library is a null compliant version of flutter_settings_screens.

Documentation

API reference

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on flutter_settings_screen_ex