1
+ // Required for StreamSubscription
2
+
1
3
import 'package:flex_color_scheme/flex_color_scheme.dart' ;
4
+ // Required for Listenable
2
5
import 'package:flutter/material.dart' ;
3
6
import 'package:flutter_bloc/flutter_bloc.dart' ;
7
+ import 'package:go_router/go_router.dart' ; // Import GoRouter
4
8
import 'package:google_fonts/google_fonts.dart' ;
5
9
import 'package:ht_authentication_repository/ht_authentication_repository.dart' ;
6
10
import 'package:ht_headlines_repository/ht_headlines_repository.dart' ;
7
11
import 'package:ht_main/app/bloc/app_bloc.dart' ;
8
12
import 'package:ht_main/l10n/l10n.dart' ;
13
+ // Import the createRouter function and the helper stream
14
+ import 'package:ht_main/router/go_router_refresh_stream.dart' ;
9
15
import 'package:ht_main/router/router.dart' ;
16
+ // Routes class is still needed if createRouter uses it, which it does
10
17
11
18
class App extends StatelessWidget {
12
19
const App ({
13
20
required HtHeadlinesRepository htHeadlinesRepository,
14
21
required HtAuthenticationRepository htAuthenticationRepository,
22
+ // AppBloc is no longer passed in constructor
15
23
super .key,
16
24
}) : _htHeadlinesRepository = htHeadlinesRepository,
17
25
_htAuthenticationRepository = htAuthenticationRepository;
18
26
19
27
final HtHeadlinesRepository _htHeadlinesRepository;
20
28
final HtAuthenticationRepository _htAuthenticationRepository;
29
+ // No longer storing AppBloc instance here
21
30
22
31
@override
23
32
Widget build (BuildContext context) {
33
+ // Provide repositories globally
24
34
return MultiRepositoryProvider (
25
35
providers: [
26
36
RepositoryProvider .value (value: _htHeadlinesRepository),
27
37
RepositoryProvider .value (value: _htAuthenticationRepository),
28
38
],
29
- child: MultiBlocProvider (
30
- providers: [
31
- BlocProvider (
32
- create: (context) => AppBloc (
33
- authenticationRepository:
34
- context.read <HtAuthenticationRepository >(),
35
- ),
36
- ),
37
- ],
39
+ // Create AppBloc here using BlocProvider
40
+ child: BlocProvider (
41
+ create: (context) => AppBloc (
42
+ authenticationRepository: context.read <HtAuthenticationRepository >(),
43
+ ),
44
+ // _AppView now reads AppBloc from context
38
45
child: const _AppView (),
39
46
),
40
47
);
41
48
}
42
49
}
43
50
44
- class _AppView extends StatelessWidget {
45
- const _AppView ();
51
+ // Change _AppView to StatefulWidget to manage GoRouter lifecycle
52
+ class _AppView extends StatefulWidget {
53
+ const _AppView (); // No longer needs appBloc passed
54
+
55
+ @override
56
+ State <_AppView > createState () => _AppViewState ();
57
+ }
58
+
59
+ class _AppViewState extends State <_AppView > {
60
+ // Store the router and the refresh stream listener
61
+ late final GoRouter _router;
62
+ late final GoRouterRefreshStream _refreshListener;
63
+
64
+ @override
65
+ void initState () {
66
+ super .initState ();
67
+ // Get the AppBloc instance from the BlocProvider above
68
+ final appBloc = context.read <AppBloc >();
69
+ // Create the refresh listener using the AppBloc stream
70
+ _refreshListener = GoRouterRefreshStream (appBloc.stream);
71
+ // Create the router instance by calling the function from router.dart
72
+ _router = createRouter (refreshListenable: _refreshListener);
73
+ }
74
+
75
+ @override
76
+ void dispose () {
77
+ // Dispose the refresh listener when the widget is disposed
78
+ _refreshListener.dispose ();
79
+ super .dispose ();
80
+ }
46
81
47
82
@override
48
83
Widget build (BuildContext context) {
84
+ // Use BlocBuilder to react to theme changes from AppBloc
49
85
return BlocBuilder <AppBloc , AppState >(
86
+ // Use buildWhen for optimization if only theme affects MaterialApp
87
+ buildWhen: (previous, current) => previous.themeMode != current.themeMode,
50
88
builder: (context, state) {
51
89
return MaterialApp .router (
52
90
debugShowCheckedModeBanner: false ,
53
- theme:
54
- state.themeMode == ThemeMode .light ? lightTheme () : darkTheme (),
55
- routerConfig: appRouter,
91
+ // Apply theme based on AppBloc state
92
+ themeMode: state.themeMode,
93
+ theme: lightTheme (),
94
+ darkTheme: darkTheme (),
95
+ // Use the router created and stored in the state
96
+ routerConfig: _router,
56
97
localizationsDelegates: AppLocalizations .localizationsDelegates,
57
98
supportedLocales: AppLocalizations .supportedLocales,
58
99
);
@@ -61,16 +102,17 @@ class _AppView extends StatelessWidget {
61
102
}
62
103
}
63
104
105
+ // --- Themes (unchanged) ---
64
106
ThemeData lightTheme () {
65
107
return FlexThemeData .light (
66
- scheme: FlexScheme .material ,
108
+ scheme: FlexScheme .greyLaw ,
67
109
fontFamily: GoogleFonts .notoSans ().fontFamily,
68
110
);
69
111
}
70
112
71
113
ThemeData darkTheme () {
72
114
return FlexThemeData .dark (
73
- scheme: FlexScheme .material ,
115
+ scheme: FlexScheme .greyLaw ,
74
116
fontFamily: GoogleFonts .notoSans ().fontFamily,
75
117
);
76
118
}
0 commit comments