Last active
June 6, 2020 23:55
-
-
Save RodBr/4d6ebfea8b1f9432ff088bec4455e15c to your computer and use it in GitHub Desktop.
Themes plus persistence - the controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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