How to check which the current Route is?

前端 未结 11 605
栀梦
栀梦 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 18:02

    for me i'm using the laziest easiest way since no answer here helped me in main route, i just use drawer class and pass the name of the current route to the drawer constructor like this :

    class MyDrawer extends StatelessWidget {
    final String currentRoute;
    MyDrawer(this.currentRoute);
    
    @override
    Widget build(BuildContext context) {
    return Drawer(
        child: ListView(
          children: [
            InkWell(
                child: Text('secondRoute'),
                onTap: () {
                  if (currentRoute != 'secondRoute')
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => secondRoute()));
                }),
            InkWell(
                child: Text('thirdRoute'),
                onTap: () {
                  if (currentRoute != 'thirdRoute')
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => thirdRoute()));
                }),
    

    and in the routes where i call MyDrawer class i pass the name of the current route

    drawer: MyDrawer('secondRoute'),
    

提交回复
热议问题