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'); } ) ) ) }
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.
Thank you in advance!
There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.
We can use this. $router. currentRoute. path property in a vue router component to get the current path.
Navigator
doesn't expose the current 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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With