-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy paththeme.dart
77 lines (59 loc) · 1.59 KB
/
theme.dart
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import 'package:flutter/material.dart';
import 'package:preferences/preferences.dart';
import 'package:skydroid/app.dart';
typedef ThemedWidgetBuilder = Widget Function(
BuildContext context, ThemeData data);
typedef ThemeDataWithBrightnessBuilder = ThemeData Function(String theme);
class AppTheme extends StatefulWidget {
const AppTheme({Key key, this.data, this.themedWidgetBuilder})
: super(key: key);
final ThemedWidgetBuilder themedWidgetBuilder;
final ThemeDataWithBrightnessBuilder data;
@override
AppThemeState createState() => AppThemeState();
static AppThemeState of(BuildContext context) {
return context.findAncestorStateOfType<AppThemeState>();
}
}
class AppThemeState extends State<AppTheme> {
ThemeData _data;
String _theme;
ThemeData get data => _data;
@override
void initState() {
super.initState();
_theme = loadTheme();
_data = widget.data(_theme);
if (mounted) {
setState(() {});
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_data = widget.data(_theme);
}
@override
void didUpdateWidget(AppTheme oldWidget) {
super.didUpdateWidget(oldWidget);
_data = widget.data(_theme);
}
void setTheme(String theme) {
setState(() {
_data = widget.data(theme);
_theme = theme;
});
}
void setThemeData(ThemeData data) {
setState(() {
_data = data;
});
}
String loadTheme() {
return PrefService.getString('theme');
}
@override
Widget build(BuildContext context) {
return widget.themedWidgetBuilder(context, _data);
}
}