如何查看当前路线? [英] How to check which the current Route is?

查看:113
本文介绍了如何查看当前路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to navigate to different Routes using a Drawer, though I do not want to open a new instance of a Route each time I tap on it if I am already on that Route, rather I would prefer that in this case a new Route is not opened. This is my code so far:

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

I want to use a conditional statement to check whether we are on a certain route. I know there is a way to check which Route we are on currently with the isCurrent of the Route class

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

尽管我不确定如何实现它。

though I am not sure how to implement it.

提前谢谢!

推荐答案

Navigator 不公开当前路线。

您可以使用的是使用 Navigator.popUntil(回调)作为 popUtil 传递给回调,当前的 Route

What you can do instead is use Navigator.popUntil(callback) as popUtil pass to the callback the current Route, which includes it's name and stuff.

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);
}

这篇关于如何查看当前路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆