首页 文章

如何查看当前路线是哪条?

提问于
浏览
6

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

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的路径

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

虽然我不知道如何实现它 .

先感谢您!

3 回答

  • 0

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

    Rémi's answer真的对我很有帮助,但是如果你是我的版本,还有一些额外的检查和解释性文件:

    /// Navigates through the application. Only replaces the top Route if it is
    /// different from the new Route. Always keeps the home page as base of the
    /// Navigator stack. New screens are pushed on the Navigator stack. When the
    /// user switches between non-home screens, the new screen replaces the old
    /// screen. In this way, the stack of screens from the drawer is never higher
    /// than 2. Returning to the HomeScreen is done by just popping the current
    /// Route.
    void _changeRoute(BuildContext context, String newRouteName) {
      // Close drawer
      Navigator.pop(context);
    
      // Check current screen status
      bool currentRouteIsHome = false;
      bool currentRouteIsNewRoute = false;
    
      Navigator.popUntil(context, (currentRoute) {
        // This is just a way to access currentRoute; the top route in the 
        // Navigator stack.
        if (currentRoute.settings.name == HomeScreen.ROUTE_NAME) {
          currentRouteIsHome = true;
        }
        if (currentRoute.settings.name == newRouteName) {
          currentRouteIsNewRoute = true;
        }
    
        // Return true so popUntil() pops nothing.
        return true;
      });
    
      // Switch screen
      if (!currentRouteIsNewRoute) {
        // Only switch screen if new route is different from current route.
        if (currentRouteIsHome) {
          // Navigate from home to non-home screen.
          Navigator.pushNamed(context, newRouteName);
        } else {
          if (newRouteName == HomeScreen.ROUTE_NAME) {
            // Navigate from non-home screen to home.
            Navigator.pop(context);
          } else {
            // Navigate from non-home screen to non-home screen.
            Navigator.popAndPushNamed(context, newRouteName);
          }
        }
      }
    }
    

    请注意,使用 pushNamedpopAndPushNamed 的此实现要求您在 routes: 参数的顶级 MaterialApp 中定义 Route 名称,如下所示:

    new MaterialApp(
      routes: <String, WidgetBuilder>{
        // define the routes
        YOUR_SCREEN_ROUTE_NAME: (BuildContext context) => new YourScreen(),
      },
    )
    
  • 5

    使用以下代码检查路由是否是最顶层的...

    Route route = MaterialPageRoute(builder: (context) => WidgetName());
     if(route.isCurrent){
     }
    

相关问题