32 lines
868 B
Dart
32 lines
868 B
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class ThemeController extends ChangeNotifier {
|
|
ThemeMode _themeMode = ThemeMode.system;
|
|
|
|
ThemeMode get themeMode => _themeMode;
|
|
|
|
ThemeController() {
|
|
_loadTheme();
|
|
}
|
|
|
|
// Load saved theme from disk
|
|
Future<void> _loadTheme() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final themeIndex = prefs.getInt('theme_mode') ?? 0;
|
|
_themeMode = ThemeMode.values[themeIndex];
|
|
notifyListeners();
|
|
}
|
|
|
|
// Update and save theme
|
|
Future<void> updateTheme(ThemeMode? newThemeMode) async {
|
|
if (newThemeMode == null || newThemeMode == _themeMode) return;
|
|
|
|
_themeMode = newThemeMode;
|
|
notifyListeners();
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setInt('theme_mode', newThemeMode.index);
|
|
}
|
|
}
|