floatingaction button show/hide on different tabs. You can set floatingActionButton to null when _selectedIndex == 0, then FAB is gone with animation, so cool. And remember to change _selectedIndex in BottomNavigationBar onTap method.
Here is the example code with some comments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | final _tabTitle = [ 'Title 1', 'Title 2' ]; // BottomNavigationBarItem title final _tabIcon = [ Icon(Icons.timer_off), Icon(Icons.timeline), ]; // BottomNavigationBarItem icon class _MyHomePageState extends State<MyHomePage> { int _selectedIndex = 0; String title = _tabTitle[0]; // tap BottomNavigationBar will invoke this method _onItemTapped(int index) { setState(() { // change _selectedIndex, fab will show or hide _selectedIndex = index; // change app bar title title = _tabTitle[index]; }); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text(title), ), body: Center( child: Text(_tabTitle[_selectedIndex]), ), // two tabs bottomNavigationBar: BottomNavigationBar( items: [ BottomNavigationBarItem(title: Text(_tabTitle[0]), icon: _tabIcon[0]), BottomNavigationBarItem(title: Text(_tabTitle[1]), icon: _tabIcon[1]) ], currentIndex: _selectedIndex, onTap: _onItemTapped, ), // key point, fab will show in Tab 0, and will hide in others. floatingActionButton: _selectedIndex == 0 ? FloatingActionButton( onPressed: () {}, child: Icon(Icons.add), ) : null, ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.