This package is a fork of dynamic_theme by Norbert515, I've already opened an issue and a PR to merge my changes but the maintainer seems to not be active anymore on this package.
This packages manages changing your theme during runtime and persiting that theme.
dependencies:
dynamic_theme:
git:
url: https://github.com/TesteurManiak/dynamic_theme.git
ref: "<version>"
run packages get and import it
import 'package:dynamic_theme/dynamic_theme.dart';
If all you want is to change the theme between light and dark you can just use the themeMode
parameter and set the light theme with the theme
property and dark theme with the darkTheme
property.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DynamicTheme(
themedWidgetBuilder: (_, themeMode, __) {
return MaterialApp(
title: 'Flutter Demo',
themeMode: themeMode,
theme: ThemeData.light(),
darkTheme: ThemeData.dark(),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
);
}
}
If you want to apply some custom ThemeData
you can use the data
parameter to provide a custom ThemeData
and rely on the themeData
from the themedWidgetBuilder
.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return DynamicTheme(
data: (themeMode) => ThemeData(
primarySwatch: Colors.indigo,
brightness: themeMode == ThemeMode.dark ? Brightness.dark : Brightness.light,
),
themedWidgetBuilder: (_, __, themeData) {
return MaterialApp(
title: 'Flutter Demo',
theme: themeData,
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
);
}
}
Change the theme like this:
void changeThemeMode() {
DynamicTheme.of(context).setThemeMode(Theme.of(context).brightness == Brightness.dark ? ThemeMode.light : ThemeMode.dark);
}
void changeColor() {
DynamicTheme.of(context).setThemeData(ThemeData(
primaryColor: Theme.of(context).primaryColor == Colors.indigo ? Colors.red : Colors.indigo
));
}
When changing the theme mode with setThemeMode
, it is additionally stored in the shared preferences.