返回扑扑前的路线

时间:2018-10-27 18:46:43

标签: routes dart flutter pop

首先,我不会像这样在MaterialApp中设置路线

new MaterialApp(
  home: new Screen1(),
  routes: <String, WidgetBuilder> {
    '/screen1': (BuildContext context) => new Screen1(),
    '/screen2' : (BuildContext context) => new Screen2(),
    '/screen3' : (BuildContext context) => new Screen3(),
    '/screen4' : (BuildContext context) => new Screen4()
  },
)

相反,我是通过像这样推送新的路由从应用程序的不同位置进行路由的:

Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (BuildContext context) =>
                        Screen3(someInputData)));

因此,就我而言,例如,如何将屏幕从当前屏幕弹出到2号屏幕?

1 个答案:

答案 0 :(得分:3)

假设:屏幕1->屏幕2->屏幕3->屏幕4

打开Screen2时,您可以执行以下操作:

    Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => Screen2(),
                settings: RouteSettings(name: '/screen2')),
          );

当您想从Screen4返回到Screen2时:

 Navigator.popUntil(context, ModalRoute.withName("/screen2"));

如果您只想返回上一个屏幕,请使用:

 Navigator.of(context).pop();