route_observer.dart
4.43 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'package:flutter/widgets.dart';
import 'package:get/src/instance/get_instance.dart';
class Routing {
String current;
String previous;
Object args;
String removed;
Route<dynamic> route;
bool isBack;
bool isSnackbar;
bool isBottomSheet;
bool isDialog;
Routing({
this.current = '',
this.previous = '',
this.args,
this.removed = '',
this.route,
this.isBack,
this.isSnackbar,
this.isBottomSheet,
this.isDialog,
});
void update(void fn(Routing value)) {
fn(this);
GetConfig.currentRoute = this.current;
}
}
class GetObserver extends NavigatorObserver {
final Function(Routing) routing;
GetObserver([this.routing, this._routeSend]);
final Routing _routeSend;
Route<dynamic> route;
bool isBack;
bool isSnackbar;
bool isBottomSheet;
bool isDialog;
String current;
String previous;
Object args;
// String previousArgs;
String removed;
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
if ('${route?.settings?.name}' == 'snackbar') {
if (GetConfig.isLogEnable)
print("[GETX] OPEN SNACKBAR ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'bottomsheet') {
if (GetConfig.isLogEnable)
print("[GETX] OPEN BOTTOMSHEET ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'dialog') {
if (GetConfig.isLogEnable)
print("[GETX] OPEN DIALOG ${route?.settings?.name}");
} else {
if (GetConfig.isLogEnable)
print("[GETX] GOING TO ROUTE ${route?.settings?.name}");
}
_routeSend.update((value) {
if (route is PageRoute) value.current = '${route?.settings?.name}';
value.args = route?.settings?.arguments;
value.route = route;
value.isBack = false;
value.removed = '';
value.previous = '${previousRoute?.settings?.name}';
value.isSnackbar = '${route?.settings?.name}' == 'snackbar';
value.isBottomSheet = '${route?.settings?.name}' == 'bottomsheet';
value.isDialog = '${route?.settings?.name}' == 'dialog';
});
if (routing != null) routing(_routeSend);
}
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
if ('${route?.settings?.name}' == 'snackbar') {
if (GetConfig.isLogEnable)
print("[GETX] CLOSE SNACKBAR ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'bottomsheet') {
if (GetConfig.isLogEnable)
print("[GETX] CLOSE BOTTOMSHEET ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'dialog') {
if (GetConfig.isLogEnable)
print("[GETX] CLOSE DIALOG ${route?.settings?.name}");
} else {
if (GetConfig.isLogEnable)
print("[GETX] BACK ROUTE ${route?.settings?.name}");
}
_routeSend.update((value) {
if (previousRoute is PageRoute)
value.current = '${previousRoute?.settings?.name}';
value.args = route?.settings?.arguments;
value.route = previousRoute;
value.isBack = true;
value.removed = '';
value.previous = '${route?.settings?.name}';
value.isSnackbar = false;
value.isBottomSheet = false;
value.isDialog = false;
});
if (routing != null) routing(_routeSend);
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (GetConfig.isLogEnable)
print("[GETX] REPLACE ROUTE ${oldRoute?.settings?.name}");
if (GetConfig.isLogEnable)
print("[GETX] NEW ROUTE ${newRoute?.settings?.name}");
_routeSend.update((value) {
if (newRoute is PageRoute) value.current = '${newRoute?.settings?.name}';
value.args = newRoute?.settings?.arguments;
value.route = newRoute;
value.isBack = false;
value.removed = '';
value.previous = '${oldRoute?.settings?.name}';
value.isSnackbar = false;
value.isBottomSheet = false;
value.isDialog = false;
});
if (routing != null) routing(_routeSend);
}
@override
void didRemove(Route route, Route previousRoute) {
super.didRemove(route, previousRoute);
if (GetConfig.isLogEnable)
print("[GETX] REMOVING ROUTE ${route?.settings?.name}");
_routeSend.update((value) {
value.route = previousRoute;
value.isBack = false;
value.removed = '${route?.settings?.name}';
value.previous = '${route?.settings?.name}';
});
if (routing != null) routing(_routeSend);
}
}