Skip to content

Commit fb63f83

Browse files
committed
refactor(app): set host theme into AppBloc, keep AppState the single source
Stop overriding the theme in build (`hostThemeSettings ?? state.themeSettings`). The app renders only `state.themeSettings`; when a host (the configurator's realtime preview) supplies a theme through the tree, App pushes it into the AppBloc via AppThemeSettingsChanged/AppThemeModeChanged in didChangeDependencies (same place it reacts to FeatureAccess). No streams, no build-time fallback — AppState stays the single source of truth.
1 parent 7ff865c commit fb63f83

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

lib/app/view/app.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class _AppState extends State<App> {
3131
late final AppBloc appBloc;
3232
late final AppRouter appRouter;
3333

34+
ThemeSettings? _lastHostThemeSettings;
35+
ThemeMode? _lastHostThemeMode;
36+
3437
@override
3538
void initState() {
3639
super.initState();
@@ -104,6 +107,20 @@ class _AppState extends State<App> {
104107
initialTabResolver,
105108
featureAccess.checker,
106109
);
110+
111+
// A host (the configurator's realtime preview) supplies its theme through the
112+
// tree; push it into the AppBloc so AppState stays the single source of truth.
113+
final hostThemeSettings = context.watch<ThemeSettings?>();
114+
if (hostThemeSettings != null && hostThemeSettings != _lastHostThemeSettings) {
115+
_lastHostThemeSettings = hostThemeSettings;
116+
appBloc.add(AppThemeSettingsChanged(hostThemeSettings));
117+
}
118+
119+
final hostThemeMode = context.watch<ThemeMode?>();
120+
if (hostThemeMode != null && hostThemeMode != _lastHostThemeMode) {
121+
_lastHostThemeMode = hostThemeMode;
122+
appBloc.add(AppThemeModeChanged(hostThemeMode));
123+
}
107124
}
108125

109126
@override
@@ -118,16 +135,11 @@ class _AppState extends State<App> {
118135

119136
final featureAccess = context.watch<FeatureAccess>();
120137

121-
// A host (the configurator's realtime preview) provides its own theme down the
122-
// tree; the app falls back to its AppBloc theme otherwise.
123-
final hostThemeSettings = context.watch<ThemeSettings?>();
124-
final hostThemeMode = context.watch<ThemeMode?>();
125-
126138
final materialApp = BlocBuilder<AppBloc, AppState>(
127139
buildWhen: (previous, current) => previous.themeSettings != current.themeSettings,
128140
builder: (context, state) {
129141
return ThemeProvider(
130-
settings: hostThemeSettings ?? state.themeSettings,
142+
settings: state.themeSettings,
131143
lightDynamic: null,
132144
darkDynamic: null,
133145
child: BlocBuilder<AppBloc, AppState>(
@@ -137,9 +149,7 @@ class _AppState extends State<App> {
137149
builder: (context, state) {
138150
final themeProvider = ThemeProvider.of(context);
139151
final forcedMode = featureAccess.supportedConfig.themeMode;
140-
final finalThemeMode = forcedMode == ThemeMode.system
141-
? (hostThemeMode ?? state.effectiveThemeMode)
142-
: forcedMode;
152+
final finalThemeMode = forcedMode == ThemeMode.system ? state.effectiveThemeMode : forcedMode;
143153

144154
return MaterialApp.router(
145155
locale: state.effectiveLocale,

0 commit comments

Comments
 (0)