美文网首页
Navigator路由返回push/pop的使用

Navigator路由返回push/pop的使用

作者: xmb | 来源:发表于2019-07-11 09:52 被阅读0次
  1. 根目录push到页面一
Navigator.of(context, rootNavigator: true).push(CupertinoPageRoute(
            builder: (BuildContext context) {
              return new Page1();
            },
            settings: RouteSettings(name: "/page1"),
          ));

其中ofrootNavigatortrueroute中使用settings来设置页面一的routename,便于以后pop到当前页面。

  1. 页面一push到页面二
Navigator.of(context).push(CupertinoPageRoute(
            builder: (BuildContext context) {
              return new Page2();
            },
            settings: RouteSettings(name: "/page2"),
          ));
  1. 页面二push到页面三
Navigator.of(context).push(CupertinoPageRoute(
            builder: (BuildContext context) {
              return new Page3();
            },
            settings: RouteSettings(name: "/page3"),
          ));
  1. 页面三pop到根目录
Navigator.of(context).popUntil(ModalRoute.withName('/'));

Navigator.of(context).popUntil((r) => r.settings.isInitialRoute);
  1. 页面三pop到上一个页面
Navigator.of(context).pop();
  1. 页面三pop到页面二
Navigator.of(context).popUntil(ModalRoute.withName('/page2'));
  1. 页面三pop到页面一
Navigator.of(context).popUntil(ModalRoute.withName('/page1'));

容易引起的问题:
popUntil(ModalRoute.withName('/page2'))中的page2不是路由中的页面时,会报错.

相关文章

网友评论

      本文标题:Navigator路由返回push/pop的使用

      本文链接:https://www.haomeiwen.com/subject/itfrkctx.html