router_outlet.dart
6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'package:flutter/material.dart';
import '../../../get.dart';
class RouterOutlet<TDelegate extends RouterDelegate<T>, T extends Object>
extends StatefulWidget {
final TDelegate routerDelegate;
final Widget Function(BuildContext context) builder;
//keys
RouterOutlet.builder({
super.key,
TDelegate? delegate,
required this.builder,
}) : routerDelegate = delegate ?? Get.delegate<TDelegate, T>()!;
RouterOutlet({
Key? key,
TDelegate? delegate,
required Iterable<GetPage> Function(T currentNavStack) pickPages,
required Widget Function(
BuildContext context,
TDelegate,
Iterable<GetPage>? page,
)
pageBuilder,
}) : this.builder(
key: key,
builder: (context) {
final currentConfig = context.delegate.currentConfiguration as T?;
final rDelegate = context.delegate as TDelegate;
var picked =
currentConfig == null ? null : pickPages(currentConfig);
if (picked?.isEmpty ?? true) {
picked = null;
}
return pageBuilder(context, rDelegate, picked);
},
delegate: delegate,
);
@override
RouterOutletState<TDelegate, T> createState() =>
RouterOutletState<TDelegate, T>();
}
class RouterOutletState<TDelegate extends RouterDelegate<T>, T extends Object>
extends State<RouterOutlet<TDelegate, T>> {
RouterDelegate? delegate;
late ChildBackButtonDispatcher _backButtonDispatcher;
void _listener() {
setState(() {});
}
VoidCallback? disposer;
@override
void didChangeDependencies() {
super.didChangeDependencies();
disposer?.call();
final router = Router.of(context);
delegate ??= router.routerDelegate;
delegate?.addListener(_listener);
disposer = () => delegate?.removeListener(_listener);
_backButtonDispatcher =
router.backButtonDispatcher!.createChildBackButtonDispatcher();
}
@override
void dispose() {
super.dispose();
disposer?.call();
// Get.routerDelegate?.removeListener(_listener);
//_backButtonDispatcher.forget(_backButtonDispatcher)
}
@override
Widget build(BuildContext context) {
_backButtonDispatcher.takePriority();
return widget.builder(context);
}
}
// class RouterOutletState<TDelegate extends RouterDelegate<T>,
//T extends Object>
// extends State<RouterOutlet<TDelegate, T>> {
// TDelegate get delegate => context.delegate as TDelegate;
// @override
// void initState() {
// super.initState();
// }
// VoidCallback? disposer;
// @override
// void didChangeDependencies() {
// disposer?.call();
// delegate.addListener(onRouterDelegateChanged);
// disposer = () => delegate.removeListener(onRouterDelegateChanged);
// _getCurrentRoute();
// super.didChangeDependencies();
// }
// @override
// void dispose() {
// disposer?.call();
// super.dispose();
// }
// T? currentRoute;
// void _getCurrentRoute() {
// currentRoute = delegate.currentConfiguration;
// }
// void onRouterDelegateChanged() {
// setState(_getCurrentRoute);
// }
// @override
// Widget build(BuildContext context) {
// return widget.builder(context);
// }
// }
class GetRouterOutlet extends RouterOutlet<GetDelegate, RouteDecoder> {
GetRouterOutlet({
String? anchorRoute,
required String initialRoute,
Iterable<GetPage> Function(Iterable<GetPage> afterAnchor)? filterPages,
// GlobalKey<NavigatorState>? key,
GetDelegate? delegate,
}) : this.pickPages(
pickPages: (config) {
Iterable<GetPage<dynamic>> ret;
if (anchorRoute == null) {
// jump the ancestor path
final length = Uri.parse(initialRoute).pathSegments.length;
return config.currentTreeBranch
.skip(length)
.take(length)
.toList();
}
ret = config.currentTreeBranch.pickAfterRoute(anchorRoute);
if (filterPages != null) {
ret = filterPages(ret);
}
return ret;
},
emptyPage: (delegate) =>
delegate.matchRoute(initialRoute).route ?? delegate.notFoundRoute,
key: Get.nestedKey(anchorRoute)?.navigatorKey,
delegate: delegate,
);
GetRouterOutlet.pickPages({
super.key,
Widget Function(GetDelegate delegate)? emptyWidget,
GetPage Function(GetDelegate delegate)? emptyPage,
required Iterable<GetPage> Function(RouteDecoder currentNavStack) pickPages,
bool Function(Route<dynamic>, dynamic)? onPopPage,
GetDelegate? delegate,
}) : super(
pageBuilder: (context, rDelegate, pages) {
final pageRes = <GetPage?>[
...?pages,
if (pages == null || pages.isEmpty) emptyPage?.call(rDelegate),
].whereType<GetPage>();
if (pageRes.isNotEmpty) {
return GetNavigator(
onPopPage: onPopPage ??
(route, result) {
final didPop = route.didPop(result);
if (!didPop) {
return false;
}
return true;
},
pages: pageRes.toList(),
key: key,
);
}
return (emptyWidget?.call(rDelegate) ?? const SizedBox.shrink());
},
pickPages: pickPages,
delegate: delegate ?? Get.rootController.rootDelegate,
);
GetRouterOutlet.builder({
super.key,
required Widget Function(
BuildContext context,
)
builder,
GetDelegate? routerDelegate,
}) : super.builder(
builder: builder,
delegate: routerDelegate,
);
}
extension PagesListExt on List<GetPage> {
Iterable<GetPage> pickAtRoute(String route) {
return skipWhile((value) {
return value.name != route;
});
}
Iterable<GetPage> pickAfterRoute(String route) {
return pickAtRoute(route).skip(1);
}
}