user9436741
user9436741

Reputation:

Close Flutter drawer when bottom navigation bar is click

I have bottom navigation bar and drawer. I want the drawer to close automatically when ever the user click any button navigation icon. I'm not really sure how to do that.

I can close the drawer immediately when ever the user click inside the drawer like below


    void _onSelectItem(int index) {
    setState(() => _currentSelected= index);
    Navigator.of(context).pop(); // close the drawer
  }

But the drawer stuck there forever until I slide back. I also want the drawer to close immediately when ever the user click bottom navigation bar.

  void onTappedBar(int index){
    index == 3
    ? _drawerKey.currentState.openDrawer()
    : setState((){
      _currentSelected = index;

    });
  }

And this are my bottom navigation bar

        bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.blueGrey[900],

        type: BottomNavigationBarType.fixed,
        onTap: onTappedBar,
        currentIndex: _currentSelected,
        showUnselectedLabels: true,
        unselectedItemColor: Colors.white,
        selectedItemColor: Color.fromRGBO(10, 135, 255, 1),
        items: <BottomNavigationBarItem> [ 
          BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text('Home')),
          BottomNavigationBarItem(icon: new Icon(Icons.search), title: new Text('Explore')),
          BottomNavigationBarItem(icon: new Icon(Icons.device_hub), title: new Text('Channels')),
          BottomNavigationBarItem(icon: new Icon(Icons.dehaze), title: new Text('More')),

        ],

      ),

I'm current learning Flutter so any suggestion will be very appreciated. thanks

Upvotes: 1

Views: 1276

Answers (1)

Tanay Neotia
Tanay Neotia

Reputation: 515

Add onTap: (int index) {} to your Bottom Nav Bar scaffold, and within that method add Navigator.of(context).pop();. Hope this helps.

Upvotes: 1

Related Questions