blob: 01625d20f8051a8daad4cb2cd317949372ca89f5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
final defaultDarkMode = true;
class Config {
bool isDark = defaultDarkMode;
Config({ this.isDark }) {}
bool isDarkMode() {
return isDark == null ? defaultDarkMode : isDark;
}
}
StreamController<Config> config_$ = StreamController<Config>.broadcast();
Future<Config> getConfig() async {
final prefs = await SharedPreferences.getInstance();
final isDark = prefs.getBool('isDark');
return Config(isDark: isDark == null ? defaultDarkMode : isDark);
}
void refreshConfig() async {
Config c = await getConfig();
config_$.add(c);
}
Stream<Config> getConfig$() {
return config_$.stream;
}
void setConfig(Config config) async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool('isDark', config.isDark);
config_$.add(await getConfig());
}
void toggleTheme() async {
final config = await getConfig();
setConfig(Config(isDark: !config.isDark));
}
|