Skip to content

Commit 72010f2

Browse files
add tag filter argument to of() and maybeOf()
1 parent 428e019 commit 72010f2

File tree

6 files changed

+66
-29
lines changed

6 files changed

+66
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 0.2.1
2+
* Add `tag` argument to `of()` and `maybeOf()` for reliably accessing navigators with a particular tag
3+
14
# 0.2.0+1
25
* Port for Flutter v2.8.0
36

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Furthermore, a *path navigations* operation performed at a given navigator can a
129129

130130
Paths are in most cases declared through the `paths` argument which provides a simple and clear interface for fully customizable page stack manipulations. It maps a set of URIs to path builder functions which will be invoked whenever `AdvancedNavigator.openNamed(context, <uri>)` with the associated URI is called. The returned path (list of pages) then replaces the navigators current page stack.
131131

132-
> `AdvancedNavigator` expects each requested path name to be in the standard [URI](https://tools.ietf.org/html/rfc2396) format and will parse it as such. Therefore, to take full advantage of this widget it is recommended to design path names with that format in mind.
132+
> `AdvancedNavigator` expects each requested path name to be in the standard [URI](https://tools.ietf.org/html/rfc2396) format and will parse it as such. Therefore, to take full advantage of this widget it is recommended to choose path names with that format in mind.
133133
134134
There is built in argument parsing for extracting arguments such as id's directly from the provided URI. In the path name, arguments are marked with enclosing parentheses `.../{argName}/...` and can be read from the args argument in the path builder function to be used for building the page stack.
135135

@@ -256,7 +256,7 @@ TextButton(
256256
),
257257
```
258258

259-
The `of()` function also provides the option to specify a `skip` parameter which allows you to access navigators which are further up in the widget tree above other navigators without having to pass down the build context.
259+
The `of()` function also provides the option to specify a `skip` parameter which allows you to access navigators which are further up in the widget tree above other navigators or even a `tag` filter argument for precise control over which navigator is returned without having to pass down any build contexts across widgets.
260260

261261
## Nesting
262262

example/pubspec.lock

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
path: ".."
88
relative: true
99
source: path
10-
version: "0.2.0+1"
10+
version: "0.2.1"
1111
async:
1212
dependency: transitive
1313
description:
@@ -81,6 +81,13 @@ packages:
8181
url: "https://pub.dartlang.org"
8282
source: hosted
8383
version: "0.12.11"
84+
material_color_utilities:
85+
dependency: transitive
86+
description:
87+
name: material_color_utilities
88+
url: "https://pub.dartlang.org"
89+
source: hosted
90+
version: "0.1.3"
8491
meta:
8592
dependency: transitive
8693
description:
@@ -155,7 +162,7 @@ packages:
155162
name: test_api
156163
url: "https://pub.dartlang.org"
157164
source: hosted
158-
version: "0.4.3"
165+
version: "0.4.8"
159166
typed_data:
160167
dependency: transitive
161168
description:

lib/src/advanced_navigator.dart

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class AdvancedNavigator extends StatefulWidget {
6666
this.reportsRouteUpdateToEngine = false,
6767
this.observers = const <NavigatorObserver>[],
6868
this.restorationScopeId,
69-
required this.tag,
69+
this.tag,
7070
}) : super(key: key);
7171

7272
/// The navigator instance to which all navigation events from this navigator
@@ -133,7 +133,7 @@ class AdvancedNavigator extends StatefulWidget {
133133
final bool reportsRouteUpdateToEngine;
134134
final List<NavigatorObserver> observers;
135135
final String? restorationScopeId;
136-
final String tag;
136+
final String? tag;
137137

138138
static const String defaultPathName = '/';
139139

@@ -220,23 +220,30 @@ class AdvancedNavigator extends StatefulWidget {
220220
///
221221
/// The `skip` argument denominates the number of instances to be skipped when
222222
/// searching up the widget tree for an instance of [AdvancedNavigator].
223-
///
224-
/// If `rootNavigator` is set to true, `skip` is ignored and the state from
225-
/// the furthest instance of this class is given instead. Useful for pushing
226-
/// contents above all subsequent instances of [AdvancedNavigator].
227-
///
228-
/// If there is no [AdvancedNavigator] in the give `context`, this function
229-
/// will throw a [FlutterError] in debug mode, and an exception in release
230-
/// mode.
223+
///
224+
/// With the `tag` argument specified, only navigators with a matching tag
225+
/// will be considered. Should multiple navigators carry a matching tag, the
226+
/// closest instance after skipping `skip` instances will be returned.
227+
///
228+
/// If `rootNavigator` is set to true, both `skip` and `tag` are ignored and
229+
/// the state from the furthest instance of this class is given instead.
230+
/// Useful for pushing contents above all subsequent instances of
231+
/// [AdvancedNavigator].
232+
///
233+
/// If there is no mathcing [AdvancedNavigator] in the give `context`, this
234+
/// function will throw a [FlutterError] in debug mode, and an exception in
235+
/// release mode.
231236
static AdvancedNavigatorState of(
232237
BuildContext context, {
233238
bool rootNavigator = false,
234239
int skip = 0,
240+
String? tag,
235241
}) {
236242
var navigator = AdvancedNavigator.maybeOf(
237243
context,
238244
rootNavigator: rootNavigator,
239245
skip: skip,
246+
tag: tag,
240247
);
241248
assert(() {
242249
if (navigator == null) {
@@ -267,17 +274,23 @@ class AdvancedNavigator extends StatefulWidget {
267274
///
268275
/// The `skip` argument denominates the number of instances to be skipped when
269276
/// searching up the widget tree for an instance of [AdvancedNavigator].
270-
///
271-
/// If `rootNavigator` is set to true, `skip` is ignored and the state from
272-
/// the furthest instance of this class is given instead. Useful for pushing
273-
/// contents above all subsequent instances of [AdvancedNavigator].
274-
///
275-
/// Will return null if there is no ancestor [AdvancedNavigator] in the
276-
/// `context`.
277+
///
278+
/// With the `tag` argument specified, only navigators with a matching tag
279+
/// will be considered. Should multiple navigators carry a matching tag, the
280+
/// closest instance after skipping `skip` instances will be returned.
281+
///
282+
/// If `rootNavigator` is set to true, both `skip` and `tag` are ignored and
283+
/// the state from the furthest instance of this class is given instead.
284+
/// Useful for pushing contents above all subsequent instances of
285+
/// [AdvancedNavigator].
286+
///
287+
/// Will return null if there is no matching ancestor [AdvancedNavigator] in
288+
/// the `context`.
277289
static AdvancedNavigatorState? maybeOf(
278290
BuildContext context, {
279291
bool rootNavigator = false,
280292
int skip = 0,
293+
String? tag,
281294
}) {
282295
assert(rootNavigator || skip >= 0);
283296
AdvancedNavigatorState? navigator;
@@ -289,12 +302,17 @@ class AdvancedNavigator extends StatefulWidget {
289302
navigator = context.findRootAncestorStateOfType<AdvancedNavigatorState>();
290303
} else {
291304
context.visitAncestorElements((element) {
292-
if (element is StatefulElement && element.state is AdvancedNavigatorState) {
293-
if (skip <= 0) {
294-
navigator = element.state as AdvancedNavigatorState;
295-
return false;
305+
if (element is StatefulElement) {
306+
var state = element.state;
307+
if (state is AdvancedNavigatorState) {
308+
if (tag == null || tag == state.tag) {
309+
if (skip == 0) {
310+
navigator = state;
311+
return false;
312+
}
313+
skip--;
314+
}
296315
}
297-
skip--;
298316
}
299317
return true;
300318
});
@@ -396,6 +414,8 @@ class AdvancedNavigatorState extends State<AdvancedNavigator> with RouteInformat
396414

397415
Set<AdvancedNavigatorState> _children = {};
398416

417+
String? get tag => widget.tag;
418+
399419
RouteInformation? get currentNestedPath => _routerDelegate._currentNestedPath;
400420

401421
@override
@@ -646,7 +666,7 @@ class DefaultRouterDelegate extends RouterDelegate<RouteInformation>
646666
final bool reportsRouteUpdateToEngine;
647667
final List<NavigatorObserver> observers;
648668
final String? restorationScopeId;
649-
final String tag;
669+
final String? tag;
650670

651671
RouteInformation _currentInternalPath = RouteInformation();
652672
RouteInformation? _currentNestedPath;

pubspec.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ packages:
6767
url: "https://pub.dartlang.org"
6868
source: hosted
6969
version: "0.12.11"
70+
material_color_utilities:
71+
dependency: transitive
72+
description:
73+
name: material_color_utilities
74+
url: "https://pub.dartlang.org"
75+
source: hosted
76+
version: "0.1.3"
7077
meta:
7178
dependency: transitive
7279
description:
@@ -134,7 +141,7 @@ packages:
134141
name: test_api
135142
url: "https://pub.dartlang.org"
136143
source: hosted
137-
version: "0.4.3"
144+
version: "0.4.8"
138145
typed_data:
139146
dependency: transitive
140147
description:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: advanced_navigator
22
description: Flutter's Navigator 2.0 as one easy-to-use widget with full page history manipulation operations, simplified pop event delegation and powerful nesting.
3-
version: 0.2.0+1
3+
version: 0.2.1
44
repository: https://github.com/LucasAschenbach/advanced_navigator
55

66
environment:

0 commit comments

Comments
 (0)