savedPreferences for themes
This commit is contained in:
@@ -44,3 +44,8 @@ Folgende Dateien wurden in diesem Prompt verändert:
|
||||
- Use the first three letters of the username as the avatar for the profile and remove the team name input in the settings.<br><br>
|
||||
Folgende Dateien wurden in diesem Prompt verändert:
|
||||
- frontend_splatournament_manager/lib/widgets/profile_widget.dart
|
||||
|
||||
- Save the theme preferences so they persist across restarts.<br><br>
|
||||
Folgende Dateien wurden in diesem Prompt verändert:
|
||||
- frontend_splatournament_manager/pubspec.yaml
|
||||
- frontend_splatournament_manager/lib/providers/theme_provider.dart
|
||||
|
||||
@@ -1,12 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class ThemeProvider extends ChangeNotifier {
|
||||
static const String _themeKey = 'theme_mode';
|
||||
ThemeMode _themeMode = ThemeMode.system;
|
||||
|
||||
ThemeMode get theme => _themeMode;
|
||||
|
||||
void setTheme(ThemeMode mode) {
|
||||
_themeMode = mode;
|
||||
ThemeProvider() {
|
||||
_loadTheme();
|
||||
}
|
||||
|
||||
Future<void> _loadTheme() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final themeName = prefs.getString(_themeKey) ?? 'system';
|
||||
_themeMode = _themeFromString(themeName);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setTheme(ThemeMode mode) async {
|
||||
_themeMode = mode;
|
||||
notifyListeners();
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_themeKey, _themeToString(mode));
|
||||
}
|
||||
|
||||
String _themeToString(ThemeMode mode) {
|
||||
switch (mode) {
|
||||
case ThemeMode.light:
|
||||
return 'light';
|
||||
case ThemeMode.dark:
|
||||
return 'dark';
|
||||
case ThemeMode.system:
|
||||
return 'system';
|
||||
}
|
||||
}
|
||||
|
||||
ThemeMode _themeFromString(String theme) {
|
||||
switch (theme) {
|
||||
case 'light':
|
||||
return ThemeMode.light;
|
||||
case 'dark':
|
||||
return ThemeMode.dark;
|
||||
case 'system':
|
||||
default:
|
||||
return ThemeMode.system;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ dependencies:
|
||||
intl: ^0.20.2
|
||||
flutter_secure_storage: ^10.0.0
|
||||
jwt_decoder: ^2.0.1
|
||||
shared_preferences: ^2.3.3
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user