creek
The AI Image Editor of 2030git clone https://github.com/maydayv7/creek
lib/services/theme_service.dart (982B)
1import 'package:flutter/material.dart';
2import 'package:shared_preferences/shared_preferences.dart';
3
4// Global instance for easy access
5final themeService = ThemeService();
6
7class ThemeService with ChangeNotifier {
8 ThemeMode _mode = ThemeMode.system;
9 ThemeMode get mode => _mode;
10
11 Future<void> loadTheme() async {
12 final prefs = await SharedPreferences.getInstance();
13 final String? saved = prefs.getString('theme_mode');
14
15 if (saved == 'light') {
16 _mode = ThemeMode.light;
17 } else if (saved == 'dark') {
18 _mode = ThemeMode.dark;
19 } else {
20 _mode = ThemeMode.system;
21 }
22
23 notifyListeners();
24 }
25
26 Future<void> toggleTheme() async {
27 final prefs = await SharedPreferences.getInstance();
28
29 if (_mode == ThemeMode.dark) {
30 _mode = ThemeMode.light;
31 await prefs.setString('theme_mode', 'light');
32 } else {
33 _mode = ThemeMode.dark;
34 await prefs.setString('theme_mode', 'dark');
35 }
36
37 notifyListeners();
38 }
39}