-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathroute_delegate.dart
132 lines (115 loc) · 3.55 KB
/
route_delegate.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'package:flutter/material.dart';
import 'package:flutter_web_navigation/services/hive_storage_service.dart';
import '../core.dart';
/// AppRouterDelegate includes the parsed result from RoutesInformationParser
class AppRouterDelegate extends RouterDelegate<RoutePath>
with ChangeNotifier, PopNavigatorRouterDelegateMixin<RoutePath> {
static final AppRouterDelegate _instance = AppRouterDelegate._();
bool? isLoggedIn;
String? pathName;
bool isError = false;
factory AppRouterDelegate({bool? isLoggedIn}) {
_instance.isLoggedIn = isLoggedIn;
return _instance;
}
AppRouterDelegate._();
// A custom trasition delegate to overwrite the default animation.
TransitionDelegate transitionDelegate = CustomTransitionDelegate();
/// Keeps the app stack
late List<Page> _stack = [];
/// currentConfiguration detects a route information may have changed as a result of rebuild.
@override
RoutePath get currentConfiguration {
if (isError) {
return RoutePath.unknown();
}
if (pathName == null) return RoutePath.home('splash'); //main
return RoutePath.otherPage(pathName);
}
@override
GlobalKey<NavigatorState> get navigatorKey =>
CustomNavigationKeys.navigatorKey;
/// App Stack - Profile screen and other known and unknown routes
List<Page> get _appStack => [
MaterialPage(
key: const ValueKey('home'),
child: MainScreen(
routeName: pathName ?? RouteData.home.name,
),
)
];
/// Auth route
List<Page> get _authStack => [
MaterialPage(
key: const ValueKey('login'),
child: Login(),
),
];
/// UnKnownRoute Stack
List<Page> get _unknownRoute => [
const MaterialPage(
key: ValueKey('unknown'),
child: UnknownRoute(),
)
];
@override
Widget build(BuildContext context) {
if (isLoggedIn == true) {
_stack = _appStack;
} else if ((isLoggedIn == false)) {
_stack = _authStack;
} else {
_stack = _unknownRoute;
}
return Navigator(
transitionDelegate: transitionDelegate,
key: navigatorKey,
pages: _stack,
onPopPage: (route, result) {
if (!route.didPop(result)) return false;
pathName = null;
// isError = false;
notifyListeners();
return true;
},
);
}
/// setNewRoutePath is called when a new route has been pushed to the application.
@override
Future<void> setNewRoutePath(RoutePath configuration) async {
bool isLoggedIn = await HiveDataStorageService.getUser();
pathName = configuration.pathName;
if (configuration.isOtherPage) {
if (configuration.pathName != null) {
if (isLoggedIn == true) {
/// If logged in
if (configuration.pathName == RouteData.login.name) {
pathName = RouteData.home.name;
isError = false;
} else {
pathName = configuration.pathName != RouteData.login.name
? configuration.pathName
: RouteData.home.name;
isError = false;
}
} else {
pathName = RouteData.login.name;
isError = false;
}
} else {
pathName = configuration.pathName;
isError = false;
}
} else {
pathName = "/";
}
notifyListeners();
}
/// setPathName sets url path
void setPathName(String path, {bool loggedIn = true}) {
pathName = path;
isLoggedIn = loggedIn;
setNewRoutePath(RoutePath.otherPage(pathName));
notifyListeners();
}
}