Skip to content

Commit f2b5385

Browse files
committed
update
1 parent 51b394e commit f2b5385

2 files changed

Lines changed: 66 additions & 39 deletions

File tree

lib/app.dart

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import 'package:dynamic_color/dynamic_color.dart';
21
import 'package:flutter/material.dart';
32
import 'package:flutter_debug_overlay/flutter_debug_overlay.dart';
43
import 'package:flutter_localizations/flutter_localizations.dart';
5-
//import 'package:logger/logger.dart' hide LogEvent;
64
import 'package:provider/provider.dart';
75

86
import 'app_view_model.dart';
97
import 'routing/router.dart';
8+
import 'theme/theme_wrapper.dart';
109

1110
const double windowWidth = 360;
1211
const double windowHeight = 640;
@@ -39,17 +38,16 @@ class App extends StatelessWidget {
3938
@override
4039
Widget build(BuildContext context) {
4140
final AppViewModel viewModel = context.read();
42-
4341
return ListenableBuilder(
4442
listenable: viewModel,
4543
builder: (_, _) {
46-
switch (viewModel.uiState) {
47-
.loading() :
48-
return const Text('Loading...'),
49-
.success(final shouldUseDynamicColor, final themeConfig) :
50-
ThemeBuilder(
51-
useDynamicColor: shouldUseDynamicColor,
52-
builder: (ThemeData theme, ThemeData darkTheme) {
44+
return switch (viewModel.uiState) {
45+
.loading() => const Text('App loading...'),
46+
.success(final shouldUseDynamicColor, final themeConfig) =>
47+
ThemeWrapper(
48+
useDynamicColor: shouldUseDynamicColor
49+
themeConfig: themeConfig,
50+
builder: (ThemeData theme, ThemeData darkTheme, ThemeMode themeMode) {
5351
return MaterialApp.router(
5452
builder: (context, child) => DebugOverlay(
5553
logBucket: App.logBucket,
@@ -60,41 +58,15 @@ class App extends StatelessWidget {
6058
showPerformanceOverlay: false,
6159
theme: theme,
6260
darkTheme: darkTheme,
63-
themeMode: switch (viewModel.themeConfig) {
64-
.followSystem => .system,
65-
.light => .light,
66-
.dark => .dark,
67-
},
61+
themeMode: themeMode,
6862
routerConfig: router,
6963
localizationsDelegates: AppLocalizations.localizationsDelegates,
7064
supportedLocales: AppLocalizations.supportedLocales,
71-
localeResolutionCallback: (locale, supportedLocales) {
72-
return locale;
73-
},
65+
localeResolutionCallback: (locale, _) => locale,
7466
);
7567
}
76-
)
68+
),
7769
}
78-
79-
DynamicColorBuilder(
80-
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
81-
ThemeData theme = .light();
82-
ThemeData darkTheme = .dark();
83-
if (
84-
viewModel.useDynamicColor &&
85-
lightDynamic != null &&
86-
darkDynamic != null
87-
) {
88-
theme = theme.copyWith(colorScheme: lightDynamic);
89-
darkTheme = theme.copyWith(colorScheme: darkDynamic);
90-
}
91-
92-
return ListenableBuilder(
93-
listenable: viewModel.loadThemeConfig,
94-
builder: (_, _) =>
95-
);
96-
},
97-
),
9870
);
9971
}
10072
}

lib/theme/theme_wrapper.dart

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import 'package:dynamic_color/dynamic_color.dart';
2+
import 'package:flutter/material.dart';
3+
4+
typedef ThemeBuilder = Widget Function(ThemeModel theme, ThemeModel darkTheme, bool useDynamicColor);
5+
6+
class ThemeWrapper extends StatelessWidget {
7+
const ThemeWrapper({
8+
required this.useDynamicColor,
9+
required this.themeConfig,
10+
required this.builder,
11+
super.key,
12+
});
13+
14+
final Stream<Bool> useDynamicColor;
15+
final Stream<ThemeConfig> themeConfig;
16+
final ThemeBuilder builder;
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return StreamBuilder(
21+
stream: useDynamicColor,
22+
builder: (_, snapshot) {
23+
ThemeData theme = const .light();
24+
ThemeData darkTheme = const .dark();
25+
return switch (snapshot.data) {
26+
true => DynamicColorBuilder(
27+
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
28+
if (lightDynamic != null && darkDynamic != null) {
29+
theme = theme.copyWith(colorScheme: lightDynamic);
30+
darkTheme = theme.copyWith(colorScheme: darkDynamic);
31+
}
32+
return _buildThemeModel(theme, darkTheme);
33+
},
34+
),
35+
false => _buildThemeModel(theme, darkTheme),
36+
_ => const Text('dynamic color preference loading...'),
37+
}
38+
}
39+
)
40+
}
41+
42+
Widget _buildThemeModel(ThemeModel theme, ThemeModel darkTheme) {
43+
return StreamBuilder(
44+
stream: themeConfig,
45+
builder: (_, snapshot) {
46+
return switch (snapshot.data) {
47+
.followSystem => builder(theme, darkTheme, .system),
48+
.light => builder(theme, darkTheme, .light),
49+
.dark => builder(theme, darkTheme, .dark),
50+
_ => const Text('Theme model loading...'),
51+
}
52+
},
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)