A Flutter package that will automatically retrieve your OS defined Theme (Dynamic), force your
prefered one (Light / Dark), allow you to present the user with a set of color themes to choose
from and of course, persist this choice in your device.
Features
Set default theme
Override system theme mode and set yours (light/dark)
Change theme colors
Customize color palettes for different theme modes
This package allows for an arbitrary number of themes to be defined. Each theme has a unique ID
of type int. To create the themes and map them to IDs, first you have to create an instance of
ThemeCollection, like so:
final themeCollection = ThemeCollection(
themes: {
0: ThemeData(primarySwatch: Colors.blue),
1: ThemeData(primarySwatch: Colors.red),
},
fallbackTheme: ThemeData.light(), // optional fallback theme, default value is ThemeData.light()
);
I would however recommend you introduce a class with static const int values to
associate a name to each ID value for easier reference:
You can utilise the changeThemeMode function from anywhere in your app. It provides a simple
and
straight forward way of altering the theme modes: light to dark, dark to light or to system
default.
This function has two optional parameters: dynamic and dark.
If the value of dynamic is true, it takes precedence over dark.
// sets theme mode to darkAdaptiveChameleonTheme.of(context).changeThemeMode(dark: true);
// sets theme mode to lightAdaptiveChameleonTheme.of(context).changeThemeMode(dark: false);
// sets theme mode to system defaultAdaptiveChameleonTheme.of(context).changeThemeMode(dynamic: true);
Changing Theme Color
The theme can be set anywhere in the app, provided you have a BuildContext, e.g.:
By setting the theme, its ID is automatically saved via the shared_preferences package, so the next time the
app starts, the theme can be restored automatically.
The example app can be found in the example folder. It implements a dropdown menu to select
between 5 themes. The app is the source for the video demo above.
Showcase
Using this package in a live app? Let me know and I will add it here.