Flutter: Mark ListTile as selected in drawer

后端 未结 2 408
心在旅途
心在旅途 2021-01-21 10:03

I want to mark the ListTile of the current page as selected but 2 days ago I\'m looking for a general way to do it.

I saw examples like this one where you h

2条回答
  •  渐次进展
    2021-01-21 10:26

    Simple create enum class like below.

    enum DrawerSelection { home, favorites, settings}
    

    Create enum object and pass pre-defined value if you want, in my case i pass home as selected ListTile item. Like below code.

    class _MyHomePage extends State {
    DrawerSelection _drawerSelection = DrawerSelection.home;
    

    Then in ListTile use selected property and change enum onTap() like below code.

     ListTile(
                  selected: _drawerSelection == DrawerSelection.home,
                  title: Text('Home'),
                  leading: Icon(Icons.home),
                  onTap: () {
                    Navigator.pop(context);
                    setState(() {
                        _drawerSelection = DrawerSelection.home;
                        _currentWidget = MainWidget();                    
                        _appBarTitle = Text("Home");
                    });
                  },
                ),
     ListTile(
                selected: _drawerSelection == DrawerSelection.favorites,
                title: Text('Favorites'),
                leading: Icon(Icons.favorite),
                onTap: () {
                  Navigator.pop(context);                 
                  setState(() {
                      _drawerSelection = DrawerSelection.favorites;                 
                      _currentWidget = FavoritesWidget();                  
                      _appBarTitle = Text("Favorites");
                  });
                },
              ),
    ListTile(
                selected: _drawerSelection == DrawerSelection.settings,
                title: Text('Settings'),
                leading: Icon(Icons.settings),
                onTap: () {
                  Navigator.pop(context);
                  setState(() {
                      _drawerSelection = DrawerSelection.settings;                 
                      _currentWidget = SettingsWidget();
                      _appBarTitle = Text("Settings");
                  });
                },
              ),
    

提交回复
热议问题