Skip to content

Commit e383801

Browse files
committed
improve app navigation, and save navigation based on the auth state
1 parent be28f8f commit e383801

File tree

8 files changed

+114
-46
lines changed

8 files changed

+114
-46
lines changed

app/lib/main/app.dart

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:app/presentation/ui/custom/loading_screen.dart';
12
import 'package:common/core/resource.dart';
23
import 'package:flutter/material.dart';
34
import 'package:domain/bloc/app/app_cubit.dart';
@@ -10,13 +11,10 @@ import 'package:app/presentation/themes/app_themes.dart';
1011
import 'package:app/presentation/utils/lang_extensions.dart';
1112
import 'package:flutter_bloc/flutter_bloc.dart';
1213
import 'package:flutter_localizations/flutter_localizations.dart';
13-
import 'package:go_router/go_router.dart';
1414

1515
import 'init.dart';
1616

1717
class App extends StatelessWidget {
18-
GoRouter get _goRouter => Routers.authRouter;
19-
2018
const App({super.key});
2119

2220
@override
@@ -38,23 +36,14 @@ class App extends StatelessWidget {
3836
GlobalWidgetsLocalizations.delegate,
3937
GlobalCupertinoLocalizations.delegate,
4038
],
41-
builder: (context, child) {
42-
return BlocListener<AuthCubit, Resource>(
43-
listener: (_, state) {
44-
if (state is RSuccess<AuthState>) {
45-
switch (state.data) {
46-
case AuthStateAuthenticated _:
47-
_goRouter.go('/home');
48-
case AuthStateUnauthenticated _:
49-
_goRouter.go('/login');
50-
case _:
51-
}
52-
}
53-
},
54-
child: child,
55-
);
56-
},
57-
routerConfig: _goRouter,
39+
builder: (context, child) =>
40+
child ??
41+
const Material(
42+
child: Center(
43+
child: CircularProgressIndicator(),
44+
),
45+
),
46+
routerConfig: Routes.router,
5847
);
5948
},
6049
),

app/lib/main/init.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import 'package:data/init.dart';
66
import 'package:domain/init.dart';
77
import 'package:flutter/material.dart';
88
import 'package:get_it/get_it.dart';
9-
import 'package:url_strategy/url_strategy.dart';
9+
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
10+
1011

1112
void init() async {
1213
WidgetsFlutterBinding.ensureInitialized();
14+
usePathUrlStrategy();
1315
await initialize();
14-
setHashUrlStrategy();
1516
runApp(const App());
1617
}
1718

app/lib/presentation/navigation/routers.dart

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,102 @@
1+
import 'package:app/main/init.dart';
12
import 'package:app/presentation/ui/pages/home/home_page.dart';
23
import 'package:app/presentation/ui/pages/login/login_page.dart';
34
import 'package:app/presentation/ui/pages/sign_up/sign_up_page.dart';
45
import 'package:app/presentation/ui/pages/splash/splash_page.dart';
6+
import 'package:common/core/resource.dart';
7+
import 'package:domain/bloc/auth/auth_cubit.dart';
8+
import 'package:domain/bloc/auth/auth_state.dart';
9+
import 'package:flutter_bloc/flutter_bloc.dart';
510
import 'package:go_router/go_router.dart';
611

12+
enum Routes {
13+
auth,
14+
login,
15+
signUp,
16+
app,
17+
home;
18+
19+
String get path => '/$name';
20+
String get subPath => name;
21+
22+
void nav({Object? extra}) {
23+
Routers.authRouter.goNamed(
24+
name,
25+
extra: extra,
26+
);
27+
}
28+
29+
static GoRouter get router => Routers.authRouter;
30+
}
31+
732
class Routers {
833
static GoRouter authRouter = GoRouter(
9-
initialLocation: "/splash",
34+
initialLocation: '/',
1035
routes: [
1136
GoRoute(
12-
name: "login",
13-
path: "/login",
14-
builder: (context, state) => const LoginPage(),
15-
),
16-
GoRoute(
17-
name: "splash",
18-
path: "/splash",
19-
builder: (context, state) => const SplashPage(),
20-
),
21-
GoRoute(
22-
name: "signUp",
23-
path: "/signUp",
24-
builder: (context, state) => const SignUpPage(),
25-
),
26-
GoRoute(
27-
name: "home",
28-
path: "/home",
29-
builder: (context, state) => const HomePage(),
37+
path: '/',
38+
builder: (context, state) {
39+
return BlocListener<AuthCubit, Resource>(
40+
listener: (_, state) {
41+
if (state is RSuccess) {
42+
switch (state.data) {
43+
case AuthStateAuthenticated _:
44+
Routes.app.nav();
45+
break;
46+
case AuthStateUnauthenticated _:
47+
Routes.auth.nav();
48+
break;
49+
case _:
50+
}
51+
}
52+
},
53+
child: const SplashPage(instant: true),
54+
);
55+
},
56+
routes: [
57+
GoRoute(
58+
name: Routes.auth.name,
59+
path: Routes.auth.path,
60+
redirect: (context, state) {
61+
if (getIt<AuthCubit>().isLoggedIn()) {
62+
return '${Routes.app.path}${Routes.home.path}';
63+
}
64+
65+
return '${Routes.auth.path}${Routes.login.path}';
66+
},
67+
routes: [
68+
GoRoute(
69+
name: Routes.login.name,
70+
path: Routes.login.subPath,
71+
builder: (context, state) => const LoginPage(),
72+
),
73+
GoRoute(
74+
name: Routes.signUp.name,
75+
path: Routes.signUp.subPath,
76+
builder: (context, state) => const SignUpPage(),
77+
),
78+
],
79+
),
80+
GoRoute(
81+
name: Routes.app.name,
82+
path: Routes.app.path,
83+
builder: (context, state) => const SplashPage(),
84+
redirect: (context, state) {
85+
if (!getIt<AuthCubit>().isLoggedIn()) {
86+
return '${Routes.auth.path}${Routes.login.path}';
87+
}
88+
89+
return '${Routes.app.path}${Routes.home.path}';
90+
},
91+
routes: [
92+
GoRoute(
93+
name: Routes.home.name,
94+
path: Routes.home.subPath,
95+
builder: (context, state) => const HomePage(),
96+
),
97+
],
98+
),
99+
],
30100
),
31101
],
32102
);

app/lib/presentation/ui/pages/home/home_view.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class HomeView extends StatelessWidget {
1414
Widget build(BuildContext context) {
1515
return Scaffold(
1616
appBar: AppBar(
17+
title: const Text('Home'),
18+
automaticallyImplyLeading: false,
1719
actions: [
1820
IconButton(
1921
onPressed: () => _authCubit.logOut(),

app/lib/presentation/ui/pages/login/login_page.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:app/main/init.dart';
22
import 'package:app/presentation/resources/resources.dart';
3-
import 'package:app/presentation/themes/app_themes.dart';
43
import 'package:app/presentation/ui/custom/app_theme_switch.dart';
54
import 'package:app/presentation/ui/custom/loading_screen.dart';
65
import 'package:common/core/resource.dart';

app/lib/presentation/ui/pages/splash/splash_page.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import 'package:domain/bloc/auth/auth_cubit.dart';
33
import 'package:flutter/material.dart';
44

55
class SplashPage extends StatefulWidget {
6-
const SplashPage({super.key});
6+
final bool instant;
7+
const SplashPage({super.key, this.instant = true});
78

89
@override
910
State<SplashPage> createState() => _SplashPageState();
@@ -20,7 +21,7 @@ class _SplashPageState extends State<SplashPage> {
2021

2122
/// Add post frame callback to avoid calling bloc methods during build
2223
WidgetsBinding.instance.addPostFrameCallback((_) async {
23-
await Future.delayed(const Duration(seconds: 1));
24+
await Future.delayed(Duration(seconds: widget.instant ? 0 : 2));
2425
_authCubit.onValidate();
2526
});
2627
}

app/pubspec.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
1818
version: 1.0.0+1
1919

2020
environment:
21-
sdk: '>=3.0.0 <4.0.0'
21+
sdk: ">=3.0.0 <4.0.0"
2222

2323
dependencies:
2424
flutter:
@@ -35,7 +35,7 @@ dependencies:
3535
package_info_plus: ^8.3.0
3636
permission_handler: ^11.4.0
3737
universal_html: ^2.2.2
38-
go_router: ^7.0.1
38+
go_router: ^17.0.0
3939
equatable: ^2.0.5
4040
firebase_core: ^3.13.0
4141
url_strategy: ^0.2.0
@@ -51,6 +51,9 @@ dependencies:
5151
http: ^1.5.0
5252
melos: ^3.4.0
5353
dev: ^1.0.0
54+
flutter_dotenv: ^5.2.1
55+
flutter_web_plugins:
56+
sdk: flutter
5457

5558
dev_dependencies:
5659
bloc_test: ^9.0.2

modules/domain/lib/bloc/auth/auth_cubit.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class AuthCubit extends BaseCubit<AuthState> {
3131
isLogOut();
3232
}
3333

34+
bool isLoggedIn() =>
35+
state is RSuccess && (state as RSuccess).data is AuthStateAuthenticated;
36+
3437
void isLogin() => isSuccess(AuthStateAuthenticated());
3538

3639
void isLogOut() => isSuccess(AuthStateUnauthenticated());

0 commit comments

Comments
 (0)