Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BottomNavigationBar with Navigator?

The Flutter Gallery example of BottomNavigationBar uses a Stack of FadeTransitions in the body of the Scaffold.

I feel it would be cleaner (and easier to animate) if we could switch pages by using a Navigator.

Are there any examples of this?

like image 722
Paul Avatar asked Jul 21 '17 10:07

Paul


People also ask

How do I use BottomNavigationBar Flutter?

A bottom navigation bar is a material widget that is present at the bottom of an app for selecting or navigating to different pages of the app. It is usually used in conjunction with a Scaffold, where it is provided as the Scaffold. bottomNavigationBar argument.

How do you set up the same bottom navigation bar for all screens in Flutter?

You can use IndexedStack to persist State when you change the page. persistent_bottom_nav_bar manage the Navigation stack of individual tabs and using this library you can manage hide/show BottomNavigationBar.

What is bottom navigation bar?

It is an implementation of material design bottom navigation. Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. They should be used when an application has three to five top-level destinations.


2 Answers

int index = 0;  @override Widget build(BuildContext context) {   return new Scaffold(     body: new Stack(       children: <Widget>[         new Offstage(           offstage: index != 0,           child: new TickerMode(             enabled: index == 0,             child: new MaterialApp(home: new YourLeftPage()),           ),         ),         new Offstage(           offstage: index != 1,           child: new TickerMode(             enabled: index == 1,             child: new MaterialApp(home: new YourRightPage()),           ),         ),       ],     ),     bottomNavigationBar: new BottomNavigationBar(       currentIndex: index,       onTap: (int index) { setState((){ this.index = index; }); },       items: <BottomNavigationBarItem>[         new BottomNavigationBarItem(           icon: new Icon(Icons.home),           title: new Text("Left"),         ),         new BottomNavigationBarItem(           icon: new Icon(Icons.search),           title: new Text("Right"),         ),       ],     ),   ); } 

You should keep each page by Stack to keep their state. Offstage stops painting, TickerMode stops animation. MaterialApp includes Navigator.

like image 151
najeira Avatar answered Sep 20 '22 21:09

najeira


Output:

enter image description here

Code:

int _index = 0;  @override Widget build(BuildContext context) {   Widget child;   switch (_index) {     case 0:       child = FlutterLogo();       break;     case 1:       child = FlutterLogo(colors: Colors.orange);       break;     case 2:       child = FlutterLogo(colors: Colors.red);       break;   }    return Scaffold(     body: SizedBox.expand(child: child),     bottomNavigationBar: BottomNavigationBar(       onTap: (newIndex) => setState(() => _index = newIndex),       currentIndex: _index,       items: [         BottomNavigationBarItem(icon: Icon(Icons.looks_one), title: Text("Blue")),         BottomNavigationBarItem(icon: Icon(Icons.looks_two), title: Text("Orange")),         BottomNavigationBarItem(icon: Icon(Icons.looks_3), title: Text("Red")),       ],     ),   ); } 
like image 44
CopsOnRoad Avatar answered Sep 20 '22 21:09

CopsOnRoad