本文介绍了使用Navigator.popUntil和不带固定名称的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将Navigator.popUntil与没有固定名称的路由一起使用?

Is it possible to use Navigator.popUntil with routes that do not have fixed names?

我有一条通过以下方式创建的路由:

I have a route created the following way:

  final _key_homepage = new GlobalKey<HomePageState>();

    Navigator.pushReplacement(context, new MaterialPageRoute(
      builder: (BuildContext context) =>  new HomePage(key: _key_homepage, somevariable1: 'some value', somevariable2: 'some value 2'),
    ));

现在,当我在任何屏幕上收到推送通知并显示弹出消息时,其中一个按钮应指向上面列出的路线。该路线已创建,必须弹出。

Now when I receive push notification on any screen and display popup message, one of the buttons should lead to the route listed above. The route was already created and must be 'poped' to. How to do it?

使用命名路线,可以这样完成:

With named route, it can be done like this:

 new FlatButton(
    child: new Text('Go to homepage'),
    onPressed: () {
      Navigator.popUntil(context, ModalRoute.withName('/homepage'));  

     //how to do the same without ModalRoute.withName('/homepage')

    },
)

键和所需路线的上下文均可用。但是,重新创建路由并不是一个好的解决方案,因为原始路由创建包含一些变量(somevariable1,somevariable2等)。

Both 'key' and context of desired route is available. However recreating the route again does not feel like a good solution, because original route creation includes some variables (somevariable1, somevariable2, etc).

有什么方法可以实现?

推荐答案

您应该添加一个设定路线时的设定;

You should add a setting when pushing your route; with a custom name

Navigator.pushReplacement(
  context,
  MaterialPageRoute(
    settings: RouteSettings(name: "Foo"),
    builder: ...,
  ),
);

然后,您可以使用 popUntil d使用命名路由

Then you can use popUntil as you'd do with named routes

Navigator.popUntil(context, ModalRoute.withName("Foo"))

这篇关于使用Navigator.popUntil和不带固定名称的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:08