Git Product home page Git Product logo

theme_mode_handler's Introduction

theme_mode_handler

This library is considered done - no new features or changes are foreseen. You may not see recent updates to this repository, but the library is totally functional. Please open an issue if you find an issue or think something is missing so we can discuss it.

version All Contributors

Flutter 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: ^3.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);
  • Get the current themeMode with:
ThemeModeHandler.of(context).themeMode;

Check the example folder for a complete example.

API

builder

Type: Widget Function(ThemeMode themeMode).

Function that runs when themeMode changes.

manager

Type: IThemeModeManager.

Implementation of IThemeModeManager to load and save the selected value.

defaultTheme

Type: ThemeMode.

Default value to be used when manager.loadThemeMode returns null or an invalid value.

placeholderWidget

Type: Widget?.

While the themeMode is loaded, you can choose to render a different widget. By default, it'll render an empty container.

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);
}

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

theme_mode_handler's People

Contributors

arthurdenner avatar

Stargazers

Abirami Valentina Kosimin avatar Bogdan avatar Andre avatar  avatar Andrew avatar  avatar  avatar Ever avatar Matias de Andrea avatar Bryan Monsalvatge avatar xcplus avatar Bouwe Ceunen avatar Jacob Phillips avatar Ugurcan Yildirim avatar Kevin McGill avatar 脉脉不得语 avatar  avatar adii shukla avatar Chingiz Huseynzade avatar Miracle Anyanwu avatar Alistair Holmes avatar Pedro Massango avatar Kelven Galvão avatar ByeongGyu Kim avatar  avatar Otacílio Neto avatar Junior Duda avatar  avatar Pedro Pereira avatar

Watchers

James Cloos avatar  avatar  avatar

theme_mode_handler's Issues

feat: allow management of theme by consumer

Currently, this library is very restricted - my fault, first Dart/Flutter library 😅

The current approach forces the usage of shared_preferences but the consumers (devs using the library) must be able to store the preference using whatever approach they prefer like storing locally with Hive or saving it in a database such as Firebase or MongoDB.

Therefore, we need to change the approach to allow for a manager property which will require an implementation of an interface with methods to load and save the selected themeMode.

This will require a major version bump.

On theme change opens keyboard pad

On theme change opens keyboard pad. This happens only on first change theme invoke. The following changes do not open keyboard pad. The same expirience I noticed using dynamic_theme on which this package based. What is the reason for that?

is it supposed to persist to local storage for next use?

I was wondering if this package is supposed to persist the user's preference of theme on the device. Because I can't figure out of this is my fault or the package is worded so that I think maybe it is supposed to do that when it's not. Thanks

Null Safety

Any intentions on migrating this to null safety anytime soon? 😄

WidgetsBindingObserver Listener

Hi 👋. Great library!

This looks to be a near perfect solution for what I'm going for. Have you considered implementing
WidgetsBindingObserver to listen for theme mode changes while the app is open?

The use case I'm thinking of covering is when a user has an app open and has their mode set to system. For example on iOS users can have the mode change at sunset from light to dark. I realize this is a bit of a edge case and maybe not worth the effort.

If you are interested I'd be happy to PR. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.