1
- import 'package:e_commerce_app/bottom_navigation.dart' ;
2
1
import 'package:e_commerce_app/configs/application.dart' ;
3
- import 'package:e_commerce_app/presentation/screens/login/login_screen.dart' ;
4
- import 'package:e_commerce_app/presentation/screens/splash/splash_screen.dart' ;
5
2
import 'package:flutter_localizations/flutter_localizations.dart' ;
6
3
import 'package:flutter/material.dart' ;
7
4
import 'package:flutter_bloc/flutter_bloc.dart' ;
@@ -20,6 +17,10 @@ class AppView extends StatefulWidget {
20
17
}
21
18
22
19
class _AppViewState extends State <AppView > {
20
+ final _navigatorKey = GlobalKey <NavigatorState >();
21
+
22
+ NavigatorState ? get _navigator => _navigatorKey.currentState;
23
+
23
24
@override
24
25
void initState () {
25
26
CommonBloc .applicationBloc.add (SetupApplication ());
@@ -32,6 +33,10 @@ class _AppViewState extends State<AppView> {
32
33
super .dispose ();
33
34
}
34
35
36
+ void onNavigate (String route) {
37
+ _navigator! .pushNamedAndRemoveUntil (route, (route) => false );
38
+ }
39
+
35
40
void loadData () {
36
41
// Only load data when authenticated
37
42
BlocProvider .of <ProfileBloc >(context).add (LoadProfile ());
@@ -40,15 +45,17 @@ class _AppViewState extends State<AppView> {
40
45
41
46
@override
42
47
Widget build (BuildContext context) {
43
- return BlocBuilder <LanguageBloc , LanguageState >(
44
- builder: (context, state ) {
45
- return BlocBuilder <AuthenticationBloc , AuthenticationState >(
46
- builder: (context, authState ) {
48
+ return BlocBuilder <ApplicationBloc , ApplicationState >(
49
+ builder: (context, applicationState ) {
50
+ return BlocBuilder <LanguageBloc , LanguageState >(
51
+ builder: (context, state ) {
47
52
return MaterialApp (
53
+ navigatorKey: _navigatorKey,
48
54
debugShowCheckedModeBanner: Application .debug,
49
55
title: Application .title,
50
56
theme: AppTheme .currentTheme,
51
57
onGenerateRoute: AppRouter .generateRoute,
58
+ initialRoute: AppRouter .SPLASH ,
52
59
locale: AppLanguage .defaultLanguage,
53
60
supportedLocales: AppLanguage .supportLanguage,
54
61
localizationsDelegates: [
@@ -57,20 +64,25 @@ class _AppViewState extends State<AppView> {
57
64
GlobalWidgetsLocalizations .delegate,
58
65
GlobalCupertinoLocalizations .delegate,
59
66
],
60
- home: BlocBuilder <ApplicationBloc , ApplicationState >(
61
- builder: (context, applicationState) {
62
- if (applicationState is ApplicationCompleted ) {
63
- if (authState is Unauthenticated ) {
64
- return LoginScreen ();
65
- }
66
- if (authState is Authenticated ) {
67
- loadData ();
68
- return BottomNavigation ();
67
+ builder: (context, child) {
68
+ return BlocListener <AuthenticationBloc , AuthenticationState >(
69
+ listener: (context, authState) {
70
+ if (applicationState is ApplicationCompleted ) {
71
+ if (authState is Unauthenticated ) {
72
+ onNavigate (AppRouter .LOGIN );
73
+ } else if (authState is Uninitialized ) {
74
+ onNavigate (AppRouter .SPLASH );
75
+ } else if (authState is Authenticated ) {
76
+ loadData ();
77
+ onNavigate (AppRouter .HOME );
78
+ }
79
+ } else {
80
+ onNavigate (AppRouter .SPLASH );
69
81
}
70
- }
71
- return SplashScreen ();
72
- },
73
- ) ,
82
+ },
83
+ child : child,
84
+ );
85
+ } ,
74
86
);
75
87
},
76
88
);
0 commit comments