route_observer.dart
4.81 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'package:flutter/widgets.dart';
import '../../get_main.dart';
class Routing {
final current;
final previous;
final args;
final previousArgs;
final removed;
final Route<dynamic> route;
final bool isBack;
final bool isSnackbar;
final bool isBottomSheet;
final bool isDialog;
Routing({
this.current,
this.previous,
this.args,
this.previousArgs,
this.removed,
this.route,
this.isBack,
this.isSnackbar,
this.isBottomSheet,
this.isDialog,
});
}
class GetObserver extends NavigatorObserver {
final Function(Routing) routing;
GetObserver([this.routing]);
Route<dynamic> route;
bool isBack;
bool isSnackbar;
bool isBottomSheet;
bool isDialog;
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
if ('${route?.settings?.name}' == 'snackbar') {
if (Get.isLogEnable) print("[OPEN SNACKBAR] ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'bottomsheet') {
if (Get.isLogEnable) print("[OPEN BOTTOMSHEET] ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'dialog') {
if (Get.isLogEnable) print("[OPEN DIALOG] ${route?.settings?.name}");
} else {
if (Get.isLogEnable) print("[GOING TO ROUTE] ${route?.settings?.name}");
}
isSnackbar = '${route?.settings?.name}' == 'snackbar';
isDialog = '${route?.settings?.name}' == 'dialog';
isBottomSheet = '${route?.settings?.name}' == 'bottomsheet';
final routeSend = Routing(
removed: null,
isBack: false,
route: route,
current: '${route?.settings?.name}',
previous: '${previousRoute?.settings?.name}',
args: route?.settings?.arguments,
previousArgs: previousRoute?.settings?.arguments,
isSnackbar: isSnackbar,
isDialog: isDialog,
isBottomSheet: isBottomSheet,
);
if (routing != null) {
routing(routeSend);
}
Get.setRouting(routeSend);
}
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
if ('${route?.settings?.name}' == 'snackbar') {
if (Get.isLogEnable) print("[CLOSE SNACKBAR] ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'bottomsheet') {
if (Get.isLogEnable)
print("[CLOSE BOTTOMSHEET] ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'dialog') {
if (Get.isLogEnable) print("[CLOSE DIALOG] ${route?.settings?.name}");
} else if ('${route?.settings?.name}' == 'snackbar') {
if (Get.isLogEnable) print("[CLOSE SNACKBAR] ${route?.settings?.name}");
} else {
if (Get.isLogEnable) print("[BACK ROUTE] ${route?.settings?.name}");
}
isSnackbar = false;
isDialog = false;
isBottomSheet = false;
final routeSend = Routing(
removed: null,
isBack: true,
route: previousRoute,
current: '${previousRoute?.settings?.name}',
previous: '${route?.settings?.name}',
args: previousRoute?.settings?.arguments,
previousArgs: route?.settings?.arguments,
isSnackbar: false, //'${route?.settings?.name}' == 'snackbar',
isDialog: false, //'${route?.settings?.name}' == 'dialog',
isBottomSheet: false, //'${route?.settings?.name}' == 'bottomsheet',
);
if (routing != null) {
routing(routeSend);
}
Get.setRouting(routeSend);
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (Get.isLogEnable) print("[REPLACE ROUTE] ${oldRoute?.settings?.name}");
if (Get.isLogEnable) print("[NEW ROUTE] ${newRoute?.settings?.name}");
isSnackbar = false;
isDialog = false;
isBottomSheet = false;
final routeSend = Routing(
removed: null, // add '${oldRoute?.settings?.name}' or remain null ???
isBack: false,
route: newRoute,
current: '${newRoute?.settings?.name}',
previous: '${oldRoute?.settings?.name}',
args: newRoute?.settings?.arguments,
isSnackbar: false,
isBottomSheet: false,
isDialog: false,
previousArgs: null);
if (routing != null) {
routing(routeSend);
}
Get.setRouting(routeSend);
}
@override
void didRemove(Route route, Route previousRoute) {
super.didRemove(route, previousRoute);
if (Get.isLogEnable) print("[REMOVING ROUTE] ${route?.settings?.name}");
final routeSend = Routing(
isBack: false,
route: previousRoute,
current: '${previousRoute?.settings?.name}',
removed: '${route?.settings?.name}',
isSnackbar: isSnackbar,
isBottomSheet: isBottomSheet,
isDialog: isDialog,
args: previousRoute?.settings?.arguments,
previousArgs: route?.settings?.arguments);
if (routing != null) {
routing(routeSend);
}
Get.setRouting(routeSend);
}
}