dynamic_theme_mode
A Flutter widget that allows theme mode changing during runtime.
Installation
Add this to your pubspec.yaml
:
dependencies:
dynamic_theme_mode: ^1.0.0
Usage
- Import the
DynamicThemeMode
widget, wrapMaterialApp
with it and pass an initialThemeMode
value.
import 'package:dynamic_theme_mode/dynamic_theme_mode.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return DynamicThemeMode(
initialThemeMode: ThemeMode.system,
builder: (BuildContext context, ThemeMode themeMode) {
return MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.orange,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.yellow,
),
// Here we use the themeMode dynamic value!
themeMode: themeMode,
home: ExampleHome(),
);
},
);
}
}
- Change the
themeMode
to use with:
DynamicThemeMode.managerOf(context)?.useDarkMode();
// Or
DynamicThemeMode.managerOf(context)?.setThemeMode(ThemeMode.light);
- Get the current
themeMode
value with:
DynamicThemeMode.managerOf(context)?.themeMode;
The example
folder has a complete example!
API
builder
Type: Widget Function(BuildContext context, ThemeMode themeMode)
.
Method that runs to rebuild the tree when themeMode
changes.
manager
Type: ThemeModeManager
.
Holds a ThemeMode
value and notifies when it changes, also, it
has the methods that changes the themeMode
.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Carlos Schwarz 💻 🎨 💡 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!
License
MIT © Carlos Schwarz