@@ -20,88 +20,117 @@ enum Routes {
2020 String get path => '/$name ' ;
2121 String get subPath => name;
2222
23- void nav ({Object ? extra}) {
24- Routers .authRouter .goNamed (
23+ void nav (BuildContext context, {Object ? extra}) {
24+ context.router .goNamed (
2525 name,
2626 extra: extra,
2727 );
2828 }
2929
30- static GoRouter get router => Routers .authRouter;
30+ static GoRouter init (BuildContext context, {String ? initialLocation}) =>
31+ Routers .appRouter (context, initialLocation: initialLocation);
32+ }
33+
34+ extension ContextOnRouter on BuildContext {
35+ GoRouter get router => GoRouter .of (this );
3136}
3237
3338class Routers {
34- static GoRouter authRouter = GoRouter (
35- initialLocation: '/' ,
36- routes: [
37- GoRoute (
38- path: '/' ,
39- builder: (context, state) {
40- return BlocListener <AuthCubit , Resource >(
41- listenWhen: (previous, current) => current is RSuccess ,
42- listener: (_, state) {
43- if (state is RSuccess ) {
44- switch (state.data) {
45- case AuthStateAuthenticated _:
46- debugPrint ('User is authenticated' );
47- Routes .app.nav ();
48- break ;
49- case AuthStateUnauthenticated _:
50- debugPrint ('User is unauthenticated' );
51- Routes .auth.nav ();
52- break ;
53- case _:
54- }
55- }
56- },
57- child: const SplashPage (),
58- );
59- },
39+ static GoRouter appRouter (
40+ BuildContext context, {
41+ String ? initialLocation,
42+ }) =>
43+ GoRouter (
44+ initialLocation: initialLocation ??
45+ (context.read <AuthCubit >().isLoggedIn ()
46+ ? Routes .app.path
47+ : Routes .auth.path),
6048 routes: [
61- ShellRoute (
62- builder: (context, state, child) => child,
63- redirect: (context, state) {
64- if (context.read <AuthCubit >().isLoggedIn ()) {
65- return Routes .app.path;
66- }
67- return null ;
68- },
69- routes: [
70- GoRoute (
71- name: Routes .auth.name,
72- path: Routes .auth.path,
73- builder: (context, state) => const LoginPage (),
74- ),
75- GoRoute (
76- name: Routes .signup.name,
77- path: '${Routes .auth .path }${Routes .signup .path }' ,
78- builder: (context, state) => const SignUpPage (),
79- ),
80- ],
81- ),
82- ShellRoute (
83- builder: (context, state, child) => child,
84- redirect: (context, state) {
85- if (! context.read <AuthCubit >().isLoggedIn ()) {
86- return Routes .auth.path;
87- }
88- return null ;
49+ GoRoute (
50+ path: '/' ,
51+ builder: (context, state) {
52+ return BlocListener <AuthCubit , Resource >(
53+ listenWhen: (previous, current) => current is RSuccess ,
54+ listener: (_, appState) {
55+ if (appState is RSuccess ) {
56+ switch (appState.data) {
57+ case AuthStateAuthenticated _:
58+ debugPrint ('User is authenticated: ${state .fullPath }' );
59+ if (state.fullPath? .contains (Routes .app.path) ??
60+ false ) {
61+ // Already navigating to app, do nothing
62+ return ;
63+ }
64+ debugPrint ('Navigating to app route' );
65+ Routes .app.nav (context);
66+ break ;
67+ case AuthStateUnauthenticated _:
68+ debugPrint (
69+ 'User is unauthenticated: ${state .fullPath }' );
70+ if (state.fullPath? .contains (Routes .auth.path) ??
71+ false ) {
72+ // Already navigating to auth, do nothing
73+ return ;
74+ }
75+ debugPrint ('Navigating to auth route' );
76+ Routes .auth.nav (context);
77+ break ;
78+ case _:
79+ }
80+ }
81+ },
82+ child: const SplashPage (),
83+ );
8984 },
9085 routes: [
91- GoRoute (
92- name: Routes .app.name,
93- path: Routes .app.path,
94- builder: (context, state) => const HomePage (),
86+ ShellRoute (
87+ builder: (context, state, child) => child,
88+ routes: [
89+ GoRoute (
90+ name: Routes .auth.name,
91+ path: Routes .auth.path,
92+ redirect: (context, state) {
93+ if (context.read <AuthCubit >().isLoggedIn ()) {
94+ return Routes .app.path;
95+ }
96+ return null ;
97+ },
98+ builder: (context, state) => const LoginPage (),
99+ routes: [
100+ GoRoute (
101+ name: Routes .signup.name,
102+ path: Routes .signup.subPath,
103+ builder: (context, state) => const SignUpPage (),
104+ ),
105+ ],
106+ ),
107+ ],
95108 ),
96- GoRoute (
97- name: Routes .placeholder.name,
98- path: "${Routes .app .path }${Routes .placeholder .path }" ,
99- builder: (context, state) => const Placeholder (),
109+ ShellRoute (
110+ builder: (context, state, child) => child,
111+ routes: [
112+ GoRoute (
113+ name: Routes .app.name,
114+ path: Routes .app.path,
115+ redirect: (context, state) {
116+ if (! context.read <AuthCubit >().isLoggedIn ()) {
117+ return Routes .auth.path;
118+ }
119+ return null ;
120+ },
121+ builder: (context, state) => const HomePage (),
122+ routes: [
123+ GoRoute (
124+ name: Routes .placeholder.name,
125+ path: Routes .placeholder.subPath,
126+ builder: (context, state) => const Placeholder (),
127+ ),
128+ ],
129+ ),
130+ ],
100131 ),
101132 ],
102133 ),
103134 ],
104- ),
105- ],
106- );
135+ );
107136}
0 commit comments