Search code examples
dartflutterbottomnavigationview

Flutter update BottomNavigationBar


I am using a BottomNavigationBar together with a TabController. By clicking on the different Tabs of the BottomNavigationBar the TabView is changing the content. However if I swipe on the TabView to switch to another view/tab the BottomNavigationBar is not updating to the tab I swiped to. I already have added a listener to the TabController to detect changes. But how can I update BottomNavigationBar programmatically to reflect the change?


Solution

  • I think it is way more elegant to use PageView instead of TabBarView specially in your case.

    enter image description here

    class BottomBarExample extends StatefulWidget {
      @override
      _BottomBarExampleState createState() => new _BottomBarExampleState();
    }
    
    class _BottomBarExampleState extends State<BottomBarExample> {
      int _page = 0;
      PageController _c;
      @override
      void initState(){
        _c =  new PageController(
          initialPage: _page,
        );
        super.initState();
      }
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          bottomNavigationBar: new BottomNavigationBar(
            currentIndex: _page,
            onTap: (index){
              this._c.animateToPage(index,duration: const Duration(milliseconds: 500),curve: Curves.easeInOut);
            },
            items: <BottomNavigationBarItem>[
            new BottomNavigationBarItem(icon: new Icon(Icons.supervised_user_circle), title: new Text("Users")),
            new BottomNavigationBarItem(icon: new Icon(Icons.notifications), title: new Text("Alerts")),
            new BottomNavigationBarItem(icon: new Icon(Icons.email), title: new Text("Inbox")),
    
          ],
    
          ),
          body: new PageView(
            controller: _c,
            onPageChanged: (newPage){
              setState((){
                this._page=newPage;
              });
            },
            children: <Widget>[
              new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Icon(Icons.supervised_user_circle),
                    new Text("Users")
                  ],
                ),
              ),
              new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Icon(Icons.notifications),
                    new Text("Alerts")
                  ],
                ),
              ),
              new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Icon(Icons.mail),
                    new Text("Inbox")
                  ],
                ),
              ),
            ],
          ),
        );
      }
    }