theme_mode_handler 2.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

theme_mode_handler #

version All Contributors

Widget to change themeMode during runtime and persist it across restarts.

Motivation #

Flutter 1.9 introduced a new way to control which theme is used: MaterialApp.themeMode. If you have specified the darkTheme and theme properties, you can use themeMode to control it. The property defaults to ThemeMode.system.

This package wraps this functionality and allows you to persist and retrieve the user's preference wherever you want by implementing an interface.

Installation #

Add this to your pubspec.yaml:

dependencies:
  theme_mode_handler: ^2.0.0

Usage #

  • Create a class that implements the IThemeModeManager interface:
class MyManager implements IThemeModeManager {
  @override
  Future<String> loadThemeMode() async {}

  @override
  Future<bool> saveThemeMode(String value) async {}
}
  • Import the ThemeModeHandler widget, wrap MaterialApp with it and pass it an instance of your manager:
import 'package:theme_mode_handler/theme_mode_handler.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ThemeModeHandler(
      manager: MyManager(),
      builder: (ThemeMode themeMode) {
        return MaterialApp(
          themeMode: themeMode,
          darkTheme: ThemeData(
            brightness: Brightness.dark,
          ),
          theme: ThemeData(
            brightness: Brightness.light,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      },
    );
  }
}
  • Change the themeMode with:
ThemeModeHandler.of(context).saveThemeMode(value);

This updates the shared preference.

  • Get the current themeMode with:
ThemeModeHandler.of(context).themeMode;

Extra #

This package exports a dialog and a method to simplify its usage.

  • Import the dialog with:
import 'package:theme_mode_handler/theme_picker_dialog.dart';
  • Use it like this:
void _selectThemeMode(BuildContext context) async {
  final newThemeMode = await showThemePickerDialog(context: context);
  print(newThemeMode);
}

A complete example can be found on the example folder.

Inspiration #

This package is inspired and based on the great package dynamic_theme.

Contributors ✨ #

Thanks goes to these wonderful people (emoji key):


Arthur Denner

💻 🎨 💡 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

License #

MIT © Arthur Denner

2.0.0 #

  • Allow management of theme by consumer.
    • BREAKING CHANGE: A new manager property that implements IThemeModeManager is required.

1.0.0 #

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:theme_mode_handler/theme_mode_handler.dart';
import 'package:theme_mode_handler/theme_picker_dialog.dart';

import 'theme_mode_manager.dart';

void main() => runApp(ExampleApp());

class ExampleApp extends StatefulWidget {
  @override
  _ExampleAppState createState() => _ExampleAppState();
}

class _ExampleAppState extends State<ExampleApp> {
  @override
  Widget build(BuildContext context) {
    return ThemeModeHandler(
      manager: ExampleThemeModeManager(),
      builder: (ThemeMode themeMode) {
        return MaterialApp(
          themeMode: themeMode,
          darkTheme: ThemeData(
            brightness: Brightness.dark,
          ),
          theme: ThemeData(
            brightness: Brightness.light,
          ),
          home: HomePage(),
        );
      },
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final themeMode = ThemeModeHandler.of(context).themeMode;

    return Scaffold(
      appBar: AppBar(
        title: const Text('ThemeModeHandler Example'),
      ),
      body: Center(
        child: Container(
          width: 400,
          padding: const EdgeInsets.all(50),
          child: Card(
            child: ListTile(
              onTap: () => _selectThemeMode(context),
              title: Text(themeMode.toString()),
              subtitle: const Text('Tap to select another'),
              trailing: const Icon(Icons.settings),
            ),
          ),
        ),
      ),
    );
  }

  void _selectThemeMode(BuildContext context) async {
    final newThemeMode = await showThemePickerDialog(context: context);
    print(newThemeMode);
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  theme_mode_handler: ^2.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:theme_mode_handler/theme_mode_handler.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
69
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • theme_mode_handler that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
effective_dart ^1.2.0
flutter_test
mockito ^4.1.1