This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/web_native/lib/settings/preferences.dart
2021-01-23 19:28:26 -05:00

40 lines
1.0 KiB
Dart

import 'dart:convert';
import 'package:imagini/settings/app_themes.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Preferences {
//
static SharedPreferences preferences;
static const String KEY_SELECTED_THEME = 'key_selected_theme';
static init() async {
preferences = await SharedPreferences.getInstance();
}
static void saveTheme(AppTheme selectedTheme) async {
if (null == selectedTheme) {
selectedTheme = AppTheme.lightTheme;
}
String theme = jsonEncode(selectedTheme.toString());
preferences.setString(KEY_SELECTED_THEME, theme);
}
static AppTheme getTheme() {
String theme = preferences.getString(KEY_SELECTED_THEME);
if (null == theme) {
return AppTheme.lightTheme;
}
return getThemeFromString(jsonDecode(theme));
}
static AppTheme getThemeFromString(String themeString) {
for (AppTheme theme in AppTheme.values) {
if (theme.toString() == themeString) {
return theme;
}
}
return null;
}
}