blob: 80e84d56779f0830d5fb44807baae032f75095bf (
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
47
48
49
50
51
|
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
String defaultTheme = 'dark';
class Config {
String theme = defaultTheme;
Config({ this.theme }) {}
bool isDarkMode() {
return theme != 'light';
}
}
StreamController<Config> config_$ = StreamController<Config>.broadcast();
Future<Config> getConfig() async {
final prefs = await SharedPreferences.getInstance();
return Config(
theme: prefs.getString('theme'),
);
}
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.setString('theme', config.theme);
config_$.add(await getConfig());
}
void toggleTheme() async {
final config = await getConfig();
if (config.theme == 'dark') {
setConfig(Config(theme: 'light'));
} else {
setConfig(Config(theme: 'dark'));
}
}
|