theme_mode_handler
実行時に `themeMode` を変更し、再起動間で永続化するための Flutter ウィジェット
実行時における `themeMode` の変更と再起動後の保持を可能にする Flutter ウィジェット。
{"sdk":"flutter"}^5.0.0{"sdk":"flutter"}以下は英語原文のスナップショットです。最新版は GitHub をご覧ください。
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.
Flutter widget to change themeMode during runtime and persist it across restarts.
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.
Add this to your pubspec.yaml:
dependencies:
theme_mode_handler: ^3.0.0
IThemeModeManager interface:class MyManager implements IThemeModeManager {
@override
Future<String> loadThemeMode() async {}
@override
Future<bool> saveThemeMode(String value) async {}
}
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'),
);
},
);
}
}
themeMode with:ThemeModeHandler.of(context).saveThemeMode(value);
themeMode with:ThemeModeHandler.of(context).themeMode;
Check the example folder for a complete example.
Type: Widget Function(ThemeMode themeMode).
Function that runs when themeMode changes.
Type: IThemeModeManager.
Implementation of IThemeModeManager to load and save the selected value.
Type: ThemeMode.
Default value to be used when manager.loadThemeMode returns null or an invalid value.
Type: Widget?.
While the themeMode is loaded, you can choose to render a different widget. By default, it'll render an empty container.
This package exports a dialog and a method to simplify its usage.
import 'package:theme_mode_handler/theme_picker_dialog.dart';
void _selectThemeMode(BuildContext context) async {
final newThemeMode = await showThemePickerDialog(context: context);
print(newThemeMode);
}
This package is inspired and based on the great package dynamic_theme.
Thanks goes to these wonderful people (emoji key):
| https://avatars0.githubusercontent.com/u/13774309?v=4 Arthur Denner 💻 🎨 💡 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT © Arthur Denner