gpt4 book ai didi

dart - 如何检查当前路线是哪条?

转载 作者:IT老高 更新时间:2023-10-28 12:29:05 25 4
gpt4 key购买 nike

我想使用抽屉导航到不同的路线,但如果我已经在该路线上,我不想每次点击它时都打开一个新的路线实例,而是我更喜欢在这种情况下新路线未开通。到目前为止,这是我的代码:

Widget build(BuildContext context){
return new Drawer(
child:
new ListView(
children: <Widget>[
new ListTile(
title: new Text("NewRoute"),
onTap: () {
Navigator.of(context).pop;
Navigator.of(context).pushNamed('/NewRoute');
}
)
)
)
}

我想使用条件语句来检查我们是否在某个路线上。我知道有一种方法可以使用 Route 类的 isCurrent 来检查我们当前所在的 Route

https://docs.flutter.io/flutter/widgets/Route/isCurrent.html

虽然我不确定如何实现它。

提前谢谢你!

最佳答案

Navigator 不暴露当前路由。

您可以做的是使用 Navigator.popUntil(callback) 作为 popUtil 将当前 Route 传递给回调,其中包括它的名字和东西。

final newRouteName = "/NewRoute";
bool isNewRouteSameAsCurrent = false;

Navigator.popUntil(context, (route) {
if (route.settings.name == newRouteName) {
isNewRouteSameAsCurrent = true;
}
return true;
});

if (!isNewRouteSameAsCurrent) {
Navigator.pushNamed(context, newRouteName);
}

关于dart - 如何检查当前路线是哪条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50817086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com