-
Notifications
You must be signed in to change notification settings - Fork 21
/
tracker_route_observer.dart
107 lines (89 loc) · 3.02 KB
/
tracker_route_observer.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
// 监控路由状态,并保存路由栈
import 'package:flutter/material.dart';
import 'page_tracker_aware.dart';
class TrackerStackObserver<R extends Route<dynamic>> extends NavigatorObserver {
// 存放路由堆栈
final List<Route> routes = [];
// 每个页面对应的监听
final Map<R, Set<PageTrackerAware>> _listeners = <R, Set<PageTrackerAware>>{};
void subscribe(PageTrackerAware pageTrackerAware, R route) {
assert(pageTrackerAware != null);
assert(route != null);
final Set<PageTrackerAware> subscribers = _listeners.putIfAbsent(route, () => Set<PageTrackerAware>());
if (subscribers.add(pageTrackerAware)) {
pageTrackerAware.didPageView();
}
}
void unsubscribe(PageTrackerAware pageTrackerAware) {
assert(pageTrackerAware != null);
for (R route in _listeners.keys) {
final Set<PageTrackerAware> subscribers = _listeners[route];
subscribers?.remove(pageTrackerAware);
}
}
@override
void didPush(Route route, Route previousRoute) {
super.didPush(route, previousRoute);
if (route is R) {
// 触发PageExit事件
if (routes.length > 0) {
R previousRoute = routes.last;
final Set<PageTrackerAware> previousSubscribers = _listeners[previousRoute];
if (previousSubscribers != null) {
for (PageTrackerAware pageTrackerAware in previousSubscribers) {
pageTrackerAware.didPageExit();
}
}
}
// 存储在路由栈上
routes.add(route);
// 触发相关PageView事件,在订阅的时候触发
/*final Set<PageTrackerAware> subscribers = _listeners[route];
if (subscribers != null) {
for (PageTrackerAware pageTrackerAware in subscribers) {
pageTrackerAware.didPageView();
}
}*/
}
}
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
if (route is R) {
// 触发PageExit
final Set<PageTrackerAware> subscribers = _listeners[route];
if (subscribers != null) {
for (PageTrackerAware pageTrackerAware in subscribers) {
pageTrackerAware.didPageExit();
}
}
// 清除路由栈
routes.removeLast();
// 触发PageView
if (routes.length > 0) {
R previousRoute = routes.last;
final Set<PageTrackerAware> previousSubscribers = _listeners[previousRoute];
if (previousSubscribers != null) {
Future.microtask(() {
for (PageTrackerAware pageTrackerAware in previousSubscribers) {
pageTrackerAware.didPageView();
}
});
}
}
}
}
@override
void didRemove(Route route, Route previousRoute) {
super.didRemove(route, previousRoute);
routes.remove(route);
}
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
int index = routes.indexOf(oldRoute);
assert(index != -1);
routes.removeAt(index);
routes.insert(index, newRoute);
}
}