https://github.com/arthurdenner/theme_mode_handler
Flutter widget to change `themeMode` during runtime and persist it across restarts.
https://github.com/arthurdenner/theme_mode_handler
dart flutter flutter-plugin hacktoberfest theme
Last synced: 6 months ago
JSON representation
Flutter widget to change `themeMode` during runtime and persist it across restarts.
- Host: GitHub
- URL: https://github.com/arthurdenner/theme_mode_handler
- Owner: arthurdenner
- License: mit
- Created: 2019-12-19T01:12:10.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-10-29T16:42:29.000Z (over 2 years ago)
- Last Synced: 2024-11-16T03:27:49.993Z (7 months ago)
- Topics: dart, flutter, flutter-plugin, hacktoberfest, theme
- Language: Dart
- Homepage:
- Size: 85 KB
- Stars: 29
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://github.com/arthurdenner/theme_mode_handler/issues) if you find an issue
> or think something is missing so we can discuss it.[![version][version-badge]][package]
[](#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`:
```yaml
dependencies:
theme_mode_handler: ^3.0.0
```## Usage
- Create a class that implements the `IThemeModeManager` interface:
```dart
class MyManager implements IThemeModeManager {
@override
Future loadThemeMode() async {}@override
Future saveThemeMode(String value) async {}
}
```- Import the `ThemeModeHandler` widget, wrap `MaterialApp` with it and pass it an instance of your manager:
```dart
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:
```dart
ThemeModeHandler.of(context).saveThemeMode(value);
```- Get the current `themeMode` with:
```dart
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:
```dart
import 'package:theme_mode_handler/theme_picker_dialog.dart';
```- Use it like this:
```dart
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](https://github.com/Norbert515/dynamic_theme).
## Contributors ✨
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
## License
MIT © [Arthur Denner](https://github.com/arthurdenner/)
[version-badge]: https://img.shields.io/pub/v/theme_mode_handler?style=flat-square
[package]: https://pub.dev/packages/theme_mode_handler