ThemeController constructor
- required String providerId,
- required List<
AppTheme> themes, - required String? defaultThemeId,
- required bool saveThemesOnChange,
- required bool loadThemeOnInit,
- ThemeChanged? onThemeChanged,
- ThemeControllerHandler? onInitCallback,
Controller which handles updating and controlling current theme.
themes
determine the list of themes that will be available.
themes
cannot have conflicting id
parameters
If conflicting id
s were found AssertionError will be thrown.
defaultThemeId
is optional.
If not provided, default theme will be the first provided theme.
Otherwise the given theme will be set as the default theme.
AssertionError will be thrown if there is no theme with defaultThemeId
.
saveThemesOnChange
is required.
This refers to whether to persist the theme on change.
If it is true
, theme will be saved to disk whenever the theme changes.
If you use this, do NOT use nested ThemeProviders as all will be saved in the same key
onInitCallback
is the callback which is called when the ThemeController is first initialed.
You can use this to call controller.loadThemeById(ID)
or equivalent to set theme.
loadThemeOnInit
will load a previously saved theme from disk.
If loadThemeOnInit
is provided, onInitCallback
will be ignored.
So onInitCallback
and loadThemeOnInit
can't both be provided at the same time.
Implementation
ThemeController({
required String providerId,
required List<AppTheme> themes,
required String? defaultThemeId,
required bool saveThemesOnChange,
required bool loadThemeOnInit,
ThemeChanged? onThemeChanged,
ThemeControllerHandler? onInitCallback,
}) : _saveThemesOnChange = saveThemesOnChange,
_loadThemeOnInit = loadThemeOnInit,
_providerId = providerId,
_onThemeChanged = onThemeChanged ?? _defaultOnThemeChanged,
_currentThemeIndex = 0 {
for (AppTheme theme in themes) {
assert(
!_appThemes.containsKey(theme.id),
"Conflicting theme ids found: "
"${theme.id} is already added to the widget tree,");
_appThemes[theme.id] = theme;
_appThemeIds.add(theme.id);
}
if (defaultThemeId != null) {
_currentThemeIndex = _appThemeIds.indexOf(defaultThemeId);
assert(_currentThemeIndex != -1,
"No app theme with the default theme id: $defaultThemeId");
}
assert(!(onInitCallback != null && _loadThemeOnInit),
"Cannot set both onInitCallback and loadThemeOnInit");
if (_loadThemeOnInit) {
_getPreviousSavedTheme().then((savedTheme) {
if (savedTheme != null) setTheme(savedTheme);
});
} else if (onInitCallback != null) {
onInitCallback(this, _getPreviousSavedTheme());
}
}