Last active
May 14, 2023 21:01
-
-
Save nonsocchi/899a098b7d5350ba28003eb36b223bf5 to your computer and use it in GitHub Desktop.
riverpod_theme_app: Service class for reading and writing theme settings from local storage.
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 ThemingService { | |
final SharedPreferences _prefs; | |
ThemingService(this._prefs); | |
static const _themeKey = 'theme'; | |
/// Loads the preferred theme from local storage with shared_preferences. | |
/// Returns system theme initially if no theme is saved. | |
ThemeMode themeMode() { | |
final themeValue = _prefs.getInt(_themeKey); | |
if (themeValue == null) return ThemeMode.system; | |
return ThemeMode.values[themeValue]; | |
} | |
/// Use the shared_preferences package to persist theme settings locally. | |
Future<void> updateThemeMode(ThemeMode theme) async { | |
await _prefs.setInt(_themeKey, theme.index); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment