Reputation: 117
I have implements the theme mode into the app. But the problem is when I quit the app and open it again the theme mode(dark mode) get reset all the time. How can I solve that problem.
Some of the code: Theme Screen
@override
void dispose() {
themeManager.removeListener(themeListener);
super.dispose();
}
@override
void initState() {
themeManager.addListener(themeListener);
super.initState();
}
themeListener() {
if (mounted) {
setState(() {});
}
}
//Button
Switch(
activeColor: Colors.grey,
value: themeManager.themeMode == ThemeMode.dark,
onChanged: (newValue) {
themeManager.toggleTheme(newValue);
}),
I haven’t done anything yet in the LoginScreen
I have set home: const LoginScreen(),
in main.dart
when I open the app I want it to check that if the app has changed the theme or not.If yes then set to dark mode automatically. How can I achieve such as that.
If you guys know how to do it please help. Or provide some link. 🙏🏽*
Upvotes: 0
Views: 269
Reputation: 91
I did that by saving using shared_preferences. Then, it's simple to just pull the value. Since my app just has two themes, I saved it as a boolean.
Be aware that they do say "there is no guarantee that writes will be persisted to disk after returning, so this plugin must not be used for storing critical data." Since I don't consider theme a critical piece of data, I've been using it. I've had issues with larger amounts of data being lost, but not the theme so far.
Upvotes: 1