Skip to content

Instantly share code, notes, and snippets.

@RodBr
Last active June 6, 2020 23:55
Show Gist options
  • Save RodBr/4d6ebfea8b1f9432ff088bec4455e15c to your computer and use it in GitHub Desktop.
Save RodBr/4d6ebfea8b1f9432ff088bec4455e15c to your computer and use it in GitHub Desktop.
Themes plus persistence - the controller
class ThemeController extends GetController {
static ThemeController get to => Get.find();
SharedPreferences prefs;
ThemeMode _themeMode;
ThemeMode get themeMode => _themeMode;
Future<void> setThemeMode(ThemeMode themeMode) async {
Get.changeThemeMode(themeMode);
_themeMode = themeMode;
update();
prefs = await SharedPreferences.getInstance();
await prefs.setString('theme', themeMode.toString().split('.')[1]);
}
getThemeModeFromPreferences() async {
ThemeMode themeMode;
prefs = await SharedPreferences.getInstance();
String themeText = prefs.getString('theme') ?? 'system';
try {
themeMode =
ThemeMode.values.firstWhere((e) => describeEnum(e) == themeText);
} catch (e) {
themeMode = ThemeMode.system;
}
setThemeMode(themeMode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment