bottomnavigationbar rebuilds page on change of tab

Solutions on MaxInterview for bottomnavigationbar rebuilds page on change of tab by the best coders in the world

showing results for - "bottomnavigationbar rebuilds page on change of tab"
Isabella
19 Jan 2020
1None of the previous answers worked out for me.
2
3The solution to keep the pages alive when switching the tabs is wrapping your Pages in an IndexedStack.
4
5class Tabbar extends StatefulWidget {
6  Tabbar({this.screens});
7
8  static const Tag = "Tabbar";
9  final List<Widget> screens;
10  @override
11  State<StatefulWidget> createState() {
12  return _TabbarState();
13  }
14}
15
16class _TabbarState extends State<Tabbar> {
17  int _currentIndex = 0;
18  Widget currentScreen;
19
20  @override
21  Widget build(BuildContext context) {
22    var _l10n = PackedLocalizations.of(context);
23
24    return Scaffold(
25  body: IndexedStack(
26    index: _currentIndex,
27    children: widget.screens,
28  ),
29  bottomNavigationBar: BottomNavigationBar(
30    fixedColor: Colors.black,
31    type: BottomNavigationBarType.fixed,
32    onTap: onTabTapped,
33    currentIndex: _currentIndex,
34    items: [
35      BottomNavigationBarItem(
36        icon: new Icon(Icons.format_list_bulleted),
37        title: new Text(_l10n.tripsTitle),
38      ),
39      BottomNavigationBarItem(
40        icon: new Icon(Icons.settings),
41        title: new Text(_l10n.settingsTitle),
42      )
43    ],
44  ),
45);
46  }
47
48  void onTabTapped(int index) {
49    setState(() {
50      _currentIndex = index;
51    });
52  }
53}