我想使用抽屉导航到不同的路线,但如果我已经在该路线上,我不想每次点击它时都打开一个新的路线实例,而是我更喜欢在这种情况下新路线未开通。到目前为止,这是我的代码:
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);
}
https://stackoverflow.com/questions/50817086/