first commit
This commit is contained in:
50
lib/providers/theme.dart
Normal file
50
lib/providers/theme.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ThemeProvider extends ChangeNotifier {
|
||||
final SharedPreferences _prefs;
|
||||
static const String _themeKey = 'theme_mode';
|
||||
|
||||
ThemeMode _themeMode = ThemeMode.system;
|
||||
ThemeMode get themeMode => _themeMode;
|
||||
|
||||
ThemeProvider(this._prefs) {
|
||||
_loadTheme();
|
||||
}
|
||||
|
||||
void _loadTheme() {
|
||||
final themeString = _prefs.getString(_themeKey);
|
||||
switch (themeString) {
|
||||
case 'light':
|
||||
_themeMode = ThemeMode.light;
|
||||
break;
|
||||
case 'dark':
|
||||
_themeMode = ThemeMode.dark;
|
||||
break;
|
||||
default:
|
||||
_themeMode = ThemeMode.system;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setThemeMode(ThemeMode mode) async {
|
||||
if (mode == _themeMode) return;
|
||||
_themeMode = mode;
|
||||
notifyListeners();
|
||||
|
||||
String themeString;
|
||||
switch (mode) {
|
||||
case ThemeMode.light:
|
||||
themeString = 'light';
|
||||
break;
|
||||
case ThemeMode.dark:
|
||||
themeString = 'dark';
|
||||
break;
|
||||
case ThemeMode.system:
|
||||
default:
|
||||
themeString = 'system';
|
||||
break;
|
||||
}
|
||||
await _prefs.setString(_themeKey, themeString);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user