问题描述
是否可以将 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).
有什么方法可以实现吗?
Any way to achieve this?
推荐答案
推送路由时应该添加设置;自定义名称
You should add a setting when pushing your route; with a custom name
Navigator.pushReplacement(
context,
MaterialPageRoute(
settings: RouteSettings(name: "Foo"),
builder: ...,
),
);
然后你可以像使用命名路由一样使用 popUntil
Then you can use popUntil
as you'd do with named routes
Navigator.popUntil(context, ModalRoute.withName("Foo"))
这篇关于使用 Navigator.popUntil 和没有固定名称的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!