本文介绍了Flutter Navigator.of(context).pop与Navigator.pop(context)的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Navigator.of(context).pop Navigator.pop(context)有什么区别?

对我来说,两者似乎都做同样的工作,实际的区别是什么。被弃用了吗?

To me both seems to do the same work, what is the actual difference. Is one deprecated?

推荐答案

Navigator.push(上下文,路由)与Navigator.of(上下文).push(路由)

导航器用于管理应用程序的页面堆栈(路线)。将给定的路线推送到屏幕上(导航器)时,我们需要获取正确的导航器,然后进行推送。

Navigator is used to manage the app's stack of pages(routes). When push the given route onto the screen(Navigator), We need to get the right Navigator and then push.

Navigator.of(上下文) .push(route)拆分 .of(context)以获得正确的导航器和 .push(route) Navigator.of(context)具有可选参数,如果 rootNavigator 设置为true,则给出距离最远的NavigatorState代替。

Navigator.of(context).push(route) splits .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 ,路线)是一个静态方法,并且两者同时执行。它在内部调用 Navigator.of(context).push(route)。导航器最紧密地包围了给定的上下文。

Navigator.push(context, route) is a static method and do both at the same time. It internally 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()类似于 push()

当多个Navigator嵌套在App中时。通过 showDialog(...)方法创建的对话框路由被推送到根导航器。如果应用程序具有多个Navigator对象,则可能有必要调用 Navigator.of(context,rootNavigator:true).pop(result)关闭对话框,而不仅仅是 Navigator.pop(上下文,结果)

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).

这篇关于Flutter Navigator.of(context).pop与Navigator.pop(context)的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:22