maybeOf static method
The navigator state from the closest instance of this class that encloses the given context, if any.
Typical usage is as follows:
AdvancedNavigatorState? navigator = AdvancedNavigator.maybeOf(context);
if (navigator != null) {
navigator
..pop()
..pop()
..pushNamed('/settings');
}
The skip
argument denominates the number of instances to be skipped when
searching up the widget tree for an instance of AdvancedNavigator.
With the tag
argument specified, only navigators with a matching tag
will be considered. Should multiple navigators carry a matching tag, the
closest instance after skipping skip
instances will be returned.
If rootNavigator
is set to true, both skip
and tag
are ignored and
the state from the furthest instance of this class is given instead.
Useful for pushing contents above all subsequent instances of
AdvancedNavigator.
Will return null if there is no matching ancestor AdvancedNavigator in
the context
.
Implementation
static AdvancedNavigatorState? maybeOf(
BuildContext context, {
bool rootNavigator = false,
int skip = 0,
String? tag,
}) {
assert(rootNavigator || skip >= 0);
AdvancedNavigatorState? navigator;
// Handles the case where the input context is a navigator element.
if (context is StatefulElement && context.state is AdvancedNavigatorState) {
navigator = context.state as AdvancedNavigatorState;
}
if (rootNavigator) {
navigator = context.findRootAncestorStateOfType<AdvancedNavigatorState>();
} else {
context.visitAncestorElements((element) {
if (element is StatefulElement) {
var state = element.state;
if (state is AdvancedNavigatorState) {
if (tag == null || tag == state.tag) {
if (skip == 0) {
navigator = state;
return false;
}
skip--;
}
}
}
return true;
});
}
return navigator;
}