forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.dart
More file actions
299 lines (275 loc) · 9.59 KB
/
state.dart
File metadata and controls
299 lines (275 loc) · 9.59 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'configuration.dart';
import 'misc/errors.dart';
import 'route.dart';
/// The route state during routing.
///
/// The state contains parsed artifacts of the current URI.
@immutable
class GoRouterState {
/// Default constructor for creating route state during routing.
const GoRouterState(
this._configuration, {
required this.uri,
required this.matchedLocation,
this.name,
this.path,
required this.fullPath,
required this.pathParameters,
this.extra,
this.error,
required this.pageKey,
this.topRoute,
});
final RouteConfiguration _configuration;
/// The full uri of the route, e.g. /family/f2/person/p1?filter=name#fragment
final Uri uri;
/// The matched location until this point.
///
/// For example:
///
/// location = /family/f2/person/p1
/// route = GoRoute('/family/:id')
///
/// matchedLocation = /family/f2
final String matchedLocation;
/// The optional name of the route associated with this app.
///
/// This can be null for GoRouterState pass into top level redirect.
final String? name;
/// The path of the route associated with this app. e.g. family/:fid
///
/// This can be null for GoRouterState pass into top level redirect.
final String? path;
/// The full path to this sub-route, e.g. /family/:fid
///
/// For top level redirect, this is the entire path that matches the location.
/// It can be empty if go router can't find a match. In that case, the [error]
/// contains more information.
final String? fullPath;
/// The parameters for this match, e.g. {'fid': 'f2'}
final Map<String, String> pathParameters;
/// An extra object to pass along with the navigation.
final Object? extra;
/// The error associated with this sub-route.
final GoException? error;
/// A unique string key for this sub-route.
/// E.g.
/// ```dart
/// ValueKey('/family/:fid')
/// ```
final ValueKey<String> pageKey;
/// The current matched top route associated with this state.
///
/// If this state represents a [ShellRoute], the top [GoRoute] will be the current
/// matched location associated with the [ShellRoute]. This allows the [ShellRoute]'s
/// associated GoRouterState to be uniquely identified using [GoRoute.name]
final GoRoute? topRoute;
/// Gets the [GoRouterState] from context.
///
/// The returned [GoRouterState] will depends on which [GoRoute] or
/// [ShellRoute] the input `context` is in.
///
/// This method only supports [GoRoute] and [ShellRoute] that generate
/// [ModalRoute]s. This is typically the case if one uses [GoRoute.builder],
/// [ShellRoute.builder], [CupertinoPage], [MaterialPage],
/// [CustomTransitionPage], or [NoTransitionPage].
///
/// This method is fine to be called during [GoRoute.builder] or
/// [ShellRoute.builder].
///
/// This method cannot be called during [GoRoute.pageBuilder] or
/// [ShellRoute.pageBuilder] since there is no [GoRouterState] to be
/// associated with yet.
///
/// To access GoRouterState from a widget.
///
/// ```dart
/// GoRoute(
/// path: '/:id'
/// builder: (_, __) => MyWidget(),
/// );
///
/// class MyWidget extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// return Text('${GoRouterState.of(context).pathParameters['id']}');
/// }
/// }
/// ```
static GoRouterState of(BuildContext context) {
ModalRoute<Object?>? route;
GoRouterStateRegistryScope? scope;
while (true) {
route = ModalRoute.of(context);
if (route == null) {
throw _noGoRouterStateError;
}
final RouteSettings settings = route.settings;
if (settings is Page<Object?>) {
scope = context
.dependOnInheritedWidgetOfExactType<GoRouterStateRegistryScope>();
if (scope == null) {
throw _noGoRouterStateError;
}
final GoRouterState? state = scope.notifier!
._createPageRouteAssociation(
route.settings as Page<Object?>, route);
if (state != null) {
return state;
}
}
final NavigatorState? state = Navigator.maybeOf(context);
if (state == null) {
throw _noGoRouterStateError;
}
context = state.context;
}
}
static GoError get _noGoRouterStateError => GoError(
'There is no GoRouterState above the current context. '
'This method should only be called under the sub tree of a '
'RouteBase.builder.',
);
/// Get a location from route name and parameters.
/// This is useful for redirecting to a named location.
String namedLocation(
String name, {
Map<String, String> pathParameters = const <String, String>{},
Map<String, String> queryParameters = const <String, String>{},
String? fragment,
}) {
// Generate base location using configuration, with optional path and query parameters
// Then conditionally append fragment if it exists and is not empty
return _configuration.namedLocation(name,
pathParameters: pathParameters,
queryParameters: queryParameters,
fragment: fragment);
}
@override
bool operator ==(Object other) {
return other is GoRouterState &&
other.uri == uri &&
other.matchedLocation == matchedLocation &&
other.name == name &&
other.path == path &&
other.fullPath == fullPath &&
other.pathParameters == pathParameters &&
other.extra == extra &&
other.error == error &&
other.pageKey == pageKey;
}
@override
int get hashCode => Object.hash(
uri,
matchedLocation,
name,
path,
fullPath,
pathParameters,
extra,
error,
pageKey,
);
}
/// An inherited widget to host a [GoRouterStateRegistry] for the subtree.
///
/// Should not be used directly, consider using [GoRouterState.of] to access
/// [GoRouterState] from the context.
@internal
class GoRouterStateRegistryScope
extends InheritedNotifier<GoRouterStateRegistry> {
/// Creates a GoRouterStateRegistryScope.
const GoRouterStateRegistryScope({
super.key,
required GoRouterStateRegistry registry,
required super.child,
}) : super(notifier: registry);
}
/// A registry to record [GoRouterState] to [Page] relation.
///
/// Should not be used directly, consider using [GoRouterState.of] to access
/// [GoRouterState] from the context.
@internal
class GoRouterStateRegistry extends ChangeNotifier {
/// creates a [GoRouterStateRegistry].
GoRouterStateRegistry();
/// A [Map] that maps a [Page] to a [GoRouterState].
@visibleForTesting
final Map<Page<Object?>, GoRouterState> registry =
<Page<Object?>, GoRouterState>{};
final Map<Route<Object?>, Page<Object?>> _routePageAssociation =
<ModalRoute<Object?>, Page<Object?>>{};
GoRouterState? _createPageRouteAssociation(
Page<Object?> page, ModalRoute<Object?> route) {
assert(route.settings == page);
if (!registry.containsKey(page)) {
return null;
}
final Page<Object?>? oldPage = _routePageAssociation[route];
if (oldPage == null) {
// This is a new association.
_routePageAssociation[route] = page;
// If there is an association, the registry relies on the route to remove
// entry from registry because it wants to preserve the GoRouterState
// until the route finishes the popping animations.
route.completed.then<void>((Object? result) {
// Can't use `page` directly because Route.settings may have changed during
// the lifetime of this route.
final Page<Object?> associatedPage =
_routePageAssociation.remove(route)!;
assert(registry.containsKey(associatedPage));
registry.remove(associatedPage);
});
} else if (oldPage != page) {
// Need to update the association to avoid memory leak.
_routePageAssociation[route] = page;
assert(registry.containsKey(oldPage));
registry.remove(oldPage);
}
assert(_routePageAssociation[route] == page);
return registry[page]!;
}
/// Updates this registry with new records.
void updateRegistry(Map<Page<Object?>, GoRouterState> newRegistry) {
bool shouldNotify = false;
final Set<Page<Object?>> pagesWithAssociation =
_routePageAssociation.values.toSet();
for (final MapEntry<Page<Object?>, GoRouterState> entry
in newRegistry.entries) {
final GoRouterState? existingState = registry[entry.key];
if (existingState != null) {
if (existingState != entry.value) {
shouldNotify =
shouldNotify || pagesWithAssociation.contains(entry.key);
registry[entry.key] = entry.value;
}
continue;
}
// Not in the _registry.
registry[entry.key] = entry.value;
// Adding or removing registry does not need to notify the listen since
// no one should be depending on them.
}
registry.removeWhere((Page<Object?> key, GoRouterState value) {
if (newRegistry.containsKey(key)) {
return false;
}
// For those that have page route association, it will be removed by the
// route future. Need to notify the listener so they can update the page
// route association if its page has changed.
if (pagesWithAssociation.contains(key)) {
shouldNotify = true;
return false;
}
return true;
});
if (shouldNotify) {
notifyListeners();
}
}
}