Navigator路由返回push/pop的使用
2019-07-11 本文已影响0人
xmb
- 根目录
push
到页面一
Navigator.of(context, rootNavigator: true).push(CupertinoPageRoute(
builder: (BuildContext context) {
return new Page1();
},
settings: RouteSettings(name: "/page1"),
));
其中of
中rootNavigator
为true
,route
中使用settings
来设置页面一的route
的name
,便于以后pop
到当前页面。
- 页面一
push
到页面二
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) {
return new Page2();
},
settings: RouteSettings(name: "/page2"),
));
- 页面二
push
到页面三
Navigator.of(context).push(CupertinoPageRoute(
builder: (BuildContext context) {
return new Page3();
},
settings: RouteSettings(name: "/page3"),
));
- 页面三
pop
到根目录
Navigator.of(context).popUntil(ModalRoute.withName('/'));
或
Navigator.of(context).popUntil((r) => r.settings.isInitialRoute);
- 页面三
pop
到上一个页面
Navigator.of(context).pop();
- 页面三
pop
到页面二
Navigator.of(context).popUntil(ModalRoute.withName('/page2'));
- 页面三
pop
到页面一
Navigator.of(context).popUntil(ModalRoute.withName('/page1'));
容易引起的问题:
当popUntil(ModalRoute.withName('/page2'))
中的page2
不是路由中的页面时,会报错.