Reputation: 3304
Suppose I have a drawer, and the app already in 'Home' page.
When tapping the 'Settings' item in drawer, it will navigate again to 'Settings'.
How to prevent it, prevent navigate to the page same with current page.
Widget build(BuildContext context) {
return new Drawer(
child: new ListView(
children: <Widget>[
new ListTile(
title: new Text("Home"),
trailing: new Icon(Icons.local_florist),
onTap: (){
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new HomePage()));
Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);
},
),
new ListTile(
title: new Text("Search"),
trailing: new Icon(Icons.search),
onTap: (){
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new SearchPage('Search Web')));
},
),
new Divider(),
new ListTile(
title: new Text("Settings"),
trailing: new Icon(Icons.settings),
onTap: (){
Navigator.of(context).pop();
Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new SettingsPage('Settings Center')));
},
),
],
),
);
}
Upvotes: 5
Views: 3498
Reputation: 40433
A fairly simple solution to this would be to simply make the button for the current page disabled. You could make an enum of all the pages and pass in a 'currentPage', and then when you're building the list tiles you could do something like onTap: currentPage == Pages.Search ? null : () { .... }
If you want to do something a bit more configurable, you could do something like what I do for my app - for my drawer menu, I have an enum that lists all of the options (i.e. enum DrawerSections { home, profile, settings, about, logout }
and my drawer class takes a list of sections to show, as I don't want all of the menu sections shown in each case. You could pass in a list of something like DrawerSectionSpec that has a DrawerSection
and a bool enabled
.
Upvotes: 4