1
1
//
2
2
// ignore_for_file: deprecated_member_use
3
3
4
+ import 'package:flex_color_scheme/flex_color_scheme.dart' ; // Added
4
5
import 'package:flutter/material.dart' ;
5
6
import 'package:flutter_bloc/flutter_bloc.dart' ;
6
7
import 'package:go_router/go_router.dart' ;
@@ -12,6 +13,8 @@ import 'package:ht_main/authentication/bloc/authentication_bloc.dart';
12
13
import 'package:ht_main/l10n/l10n.dart' ;
13
14
import 'package:ht_main/router/router.dart' ;
14
15
import 'package:ht_main/shared/theme/app_theme.dart' ;
16
+ import 'package:ht_main/shared/widgets/failure_state_widget.dart' ; // Added
17
+ import 'package:ht_main/shared/widgets/loading_state_widget.dart' ; // Added
15
18
import 'package:ht_shared/ht_shared.dart' ; // Shared models, FromJson, ToJson, etc.
16
19
17
20
class App extends StatelessWidget {
@@ -168,24 +171,93 @@ class _AppViewState extends State<_AppView> {
168
171
// (specifically for updating the ValueNotifier) with a BlocListener.
169
172
// The BlocBuilder remains for theme changes.
170
173
return BlocListener <AppBloc , AppState >(
171
- // Only listen when the status actually changes
174
+ // Listen for status changes to update the GoRouter's ValueNotifier
172
175
listenWhen: (previous, current) => previous.status != current.status,
173
176
listener: (context, state) {
174
- // Directly update the ValueNotifier when the AppBloc status changes.
175
- // This triggers the GoRouter's refreshListenable.
176
177
_statusNotifier.value = state.status;
177
178
},
178
179
child: BlocBuilder <AppBloc , AppState >(
179
- // Build when theme-related properties change (including text scale factor)
180
- buildWhen:
181
- (previous, current) =>
182
- previous.themeMode != current.themeMode ||
183
- previous.flexScheme != current.flexScheme ||
184
- previous.fontFamily != current.fontFamily ||
185
- previous.appTextScaleFactor != current.appTextScaleFactor ||
186
- previous.locale != current.locale ||
187
- previous.settings != current.settings, // Added settings check
180
+ // Rebuild the UI based on AppBloc's state (theme, locale, and critical app statuses)
188
181
builder: (context, state) {
182
+ // Defer l10n access until inside a MaterialApp context
183
+
184
+ // Handle critical AppConfig loading states globally
185
+ if (state.status == AppStatus .configFetching) {
186
+ return MaterialApp (
187
+ debugShowCheckedModeBanner: false ,
188
+ theme: lightTheme (
189
+ scheme: FlexScheme .material, // Default scheme
190
+ appTextScaleFactor: AppTextScaleFactor .medium, // Default
191
+ appFontWeight: AppFontWeight .regular, // Default
192
+ fontFamily: null , // System default font
193
+ ),
194
+ darkTheme: darkTheme (
195
+ scheme: FlexScheme .material, // Default scheme
196
+ appTextScaleFactor: AppTextScaleFactor .medium, // Default
197
+ appFontWeight: AppFontWeight .regular, // Default
198
+ fontFamily: null , // System default font
199
+ ),
200
+ themeMode: state.themeMode, // Still respect light/dark if available from system
201
+ localizationsDelegates: AppLocalizations .localizationsDelegates,
202
+ supportedLocales: AppLocalizations .supportedLocales,
203
+ home: Scaffold (
204
+ body: Builder ( // Use Builder to get context under MaterialApp
205
+ builder: (innerContext) {
206
+ final l10n = innerContext.l10n;
207
+ return LoadingStateWidget (
208
+ icon: Icons .settings_applications_outlined,
209
+ headline: l10n.headlinesFeedLoadingHeadline, // "Loading..."
210
+ subheadline: l10n.pleaseWait, // "Please wait..."
211
+ );
212
+ },
213
+ ),
214
+ ),
215
+ );
216
+ }
217
+
218
+ if (state.status == AppStatus .configFetchFailed) {
219
+ return MaterialApp (
220
+ debugShowCheckedModeBanner: false ,
221
+ theme: lightTheme (
222
+ scheme: FlexScheme .material, // Default scheme
223
+ appTextScaleFactor: AppTextScaleFactor .medium, // Default
224
+ appFontWeight: AppFontWeight .regular, // Default
225
+ fontFamily: null , // System default font
226
+ ),
227
+ darkTheme: darkTheme (
228
+ scheme: FlexScheme .material, // Default scheme
229
+ appTextScaleFactor: AppTextScaleFactor .medium, // Default
230
+ appFontWeight: AppFontWeight .regular, // Default
231
+ fontFamily: null , // System default font
232
+ ),
233
+ themeMode: state.themeMode,
234
+ localizationsDelegates: AppLocalizations .localizationsDelegates,
235
+ supportedLocales: AppLocalizations .supportedLocales,
236
+ home: Scaffold (
237
+ body: Builder ( // Use Builder to get context under MaterialApp
238
+ builder: (innerContext) {
239
+ final l10n = innerContext.l10n;
240
+ return FailureStateWidget (
241
+ message: l10n.unknownError, // "An unknown error occurred."
242
+ retryButtonText: "Retry" , // Hardcoded for now
243
+ onRetry: () {
244
+ // Use outer context for BLoC access
245
+ context
246
+ .read <AppBloc >()
247
+ .add (const AppConfigFetchRequested ());
248
+ },
249
+ );
250
+ },
251
+ ),
252
+ ),
253
+ );
254
+ }
255
+
256
+ // If config is loaded (or not in a failed/fetching state for config), proceed with main app UI
257
+ // It's safe to access l10n here if needed for print statements,
258
+ // as this path implies we are about to build the main MaterialApp.router
259
+ // which provides localizations.
260
+ // final l10n = context.l10n;
189
261
print ('[_AppViewState] Building MaterialApp.router' );
190
262
print ('[_AppViewState] state.fontFamily: ${state .fontFamily }' );
191
263
print (
@@ -197,23 +269,22 @@ class _AppViewState extends State<_AppView> {
197
269
return MaterialApp .router (
198
270
debugShowCheckedModeBanner: false ,
199
271
themeMode: state.themeMode,
200
- // Pass scheme and font family from state to theme functions
201
272
theme: lightTheme (
202
273
scheme: state.flexScheme,
203
274
appTextScaleFactor:
204
275
state.settings.displaySettings.textScaleFactor,
205
- appFontWeight: state.settings.displaySettings.fontWeight, // Added
276
+ appFontWeight: state.settings.displaySettings.fontWeight,
206
277
fontFamily: state.settings.displaySettings.fontFamily,
207
278
),
208
279
darkTheme: darkTheme (
209
280
scheme: state.flexScheme,
210
281
appTextScaleFactor:
211
282
state.settings.displaySettings.textScaleFactor,
212
- appFontWeight: state.settings.displaySettings.fontWeight, // Added
283
+ appFontWeight: state.settings.displaySettings.fontWeight,
213
284
fontFamily: state.settings.displaySettings.fontFamily,
214
285
),
215
286
routerConfig: _router,
216
- locale: state.locale, // Use locale from AppBloc state
287
+ locale: state.locale,
217
288
localizationsDelegates: AppLocalizations .localizationsDelegates,
218
289
supportedLocales: AppLocalizations .supportedLocales,
219
290
);
0 commit comments