Skip to content

Instantly share code, notes, and snippets.

@nonsocchi
Last active May 14, 2023 21:01
Show Gist options
  • Save nonsocchi/899a098b7d5350ba28003eb36b223bf5 to your computer and use it in GitHub Desktop.
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.
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