共计 1063 个字符,预计需要花费 3 分钟才能阅读完成。
Navigator.push(context, route) vs Navigator.of(context).push(route)
Navigator is used to manage the app’s stack of pages(routes). When push the given route onto the screen(Navigator), We need get the right Navigator and push.
Navigator.of(context).push(route)
split .of(context)
to get the right Navigator and .push(route)
. Navigator.of(context)
has optional parameters, if rootNavigator
is set to true, the NavigatorState from the furthest is given instead.
static NavigatorState of(
BuildContext context, {
bool rootNavigator = false,
bool nullOk = false,
})
Navigator.push(context, route)
is a static method and do both at the same time. It internal calls Navigator.of(context).push(route)
. The navigator is most tightly encloses the given context.
static Future<T> push<T extends Object>(BuildContext context, Route<T> route) {return Navigator.of(context).push(route);
}
pop similar to push.
When multiple Navigators are nested in App. The dialog route created by showDialog
method is pushed to the root navigator. If the application has multiple Navigator objects, it may be necessary to call Navigator.of(context, rootNavigator: true).pop(result)
to close the dialog rather than just Navigator.pop(context, result)
.