防止在颤动中点击抽屉项目时导航到与当前页面相同的页面

Vin*_*ent 5 dart flutter

假设我有一个抽屉,并且应用程序已经在“主页”页面中。

当点击抽屉中的“设置”项时,它将再次导航至“设置”。

如何防止它,防止导航到与当前页面相同的页面。

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')));
            },
          ),
        ],
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

rmt*_*zie 4

一个相当简单的解决方案是简单地禁用当前页面的按钮。您可以对所有页面进行枚举并传入“currentPage”,然后当您构建列表图块时,您可以执行类似的操作onTap: currentPage == Pages.Search ? null : () { .... }

如果你想做一些更可配置的事情,你可以做一些像我为我的应用程序所做的事情 - 对于我的抽屉菜单,我有一个列出所有选项的枚举(即enum DrawerSections { home, profile, settings, about, logout }我的抽屉类需要一个部分列表来显示,因为我不希望在每种情况下显示所有菜单部分。您可以传入一个类似 DrawerSectionSpec 的列表,其中包含 aDrawerSection和 a bool enabled