How to check which the current Route is?

前端 未结 11 603
栀梦
栀梦 2021-02-03 17:49

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

11条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 18:01

    This is my solution. Based on the Rémi Rousselet solution. The only difference is that prevent te pop of the "last" route remaining.

    bool routeFound = false;
    String routeName = "/myRoute";
    
    //remove all the routes and stops if find the routeName or if is the last one
    Navigator.popUntil(context, (route) {
        if (route.settings.name == routeName) {
            expenseRouteFound = true;
        }
        //print("route " + routeName + " founded in stack: " + routeFound.toString());
        //print("last remaining route into the stack: " + route.isFirst.toString());
    
        return routeFound || route.isFirst;
    });
    
    //print("route " + routeName + " founded in stack: " + routeFound.toString());
    if (!routeFound) {
        //print("time to push the route: " + routeName + "!");
        Navigator.of(context).pushNamedAndRemoveUntil(routeName, (_)=>false);
    }
    

提交回复
热议问题