generated from Stacked-Org/package-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
theme_builder.dart
135 lines (120 loc) · 3.91 KB
/
theme_builder.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:stacked_shared/stacked_shared.dart';
import 'package:stacked_themes/src/theme_manager.dart';
import '../stacked_themes.dart';
/// A widget that rebuilds itself with a new theme
class ThemeBuilder extends StatefulWidget {
final Widget Function(BuildContext, ThemeData?, ThemeData?, ThemeMode?)
builder;
final List<ThemeData>? themes;
final ThemeData? lightTheme;
final ThemeData? darkTheme;
final Color? Function(ThemeData?)? statusBarColorBuilder;
final Color? Function(ThemeData?)? navigationBarColorBuilder;
final ThemeMode defaultThemeMode;
ThemeBuilder({
Key? key,
required this.builder,
this.themes,
this.lightTheme,
this.darkTheme,
this.statusBarColorBuilder,
this.navigationBarColorBuilder,
this.defaultThemeMode = ThemeMode.system,
}) : super(key: key);
@override
_ThemeBuilderState createState() => _ThemeBuilderState(
ThemeManager(
themes: themes,
statusBarColorBuilder: statusBarColorBuilder,
navigationBarColorBuilder: navigationBarColorBuilder,
darkTheme: darkTheme,
lightTheme: lightTheme,
defaultTheme: defaultThemeMode,
),
);
}
class _ThemeBuilderState extends State<ThemeBuilder>
with WidgetsBindingObserver {
final ThemeManager themeManager;
_ThemeBuilderState(this.themeManager);
@override
Widget build(BuildContext context) {
return Provider<ThemeManager>.value(
value: themeManager,
builder: (context, child) => StreamProvider<ThemeModel>(
lazy: false,
initialData: themeManager.initialTheme,
create: (context) => themeManager.themesStream,
builder: (context, child) => Consumer<ThemeModel>(
child: child,
builder: (context, themeModel, child) => widget.builder(
context,
themeModel.selectedTheme,
themeModel.darkTheme,
themeModel.themeMode,
),
),
),
);
}
// Get all services
// final themeService = locator<ThemeService>();
// @override
// Widget build(BuildContext context) {
// return widget.child;
// }
@override
void initState() {
super.initState();
ambiguate(WidgetsBinding.instance)!.addObserver(this);
}
@override
void dispose() {
super.dispose();
ambiguate(WidgetsBinding.instance)!.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
adjustSystemThemeIfNecessary();
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
// Should update theme whenever platform brightness changes.
// This makes sure that theme changes even if the brightness changes from notification bar.
@override
void didChangePlatformBrightness() {
super.didChangePlatformBrightness();
adjustSystemThemeIfNecessary();
}
//NOTE: re-apply the appropriate theme when the application gets back into the foreground
void adjustSystemThemeIfNecessary() {
switch (themeManager.selectedThemeMode) {
// When app becomes inactive the overlay colors might change.
// Therefore when the app is resumed we also need to update
// overlay colors back to their original state. In case
// selected theme mode is system the overlay colors will be
// automatically updated.
case ThemeMode.light:
case ThemeMode.dark:
final selectedTheme = themeManager.getSelectedTheme().selectedTheme;
themeManager.updateOverlayColors(selectedTheme);
break;
//reapply theme
case ThemeMode.system:
themeManager.setThemeMode(ThemeMode.system);
break;
default:
}
}
}