How to preserve widget states in flutter, when navigating using BottomNavigationBar?

后端 未结 6 1454
青春惊慌失措
青春惊慌失措 2020-11-27 12:20

I\'m currently working on building a Flutter app that will preserve states when navigating from one screen, to another, and back again when utilizing BottomNavigationBar. Ju

6条回答
  •  没有蜡笔的小新
    2020-11-27 12:49

    For keeping state in BottomNavigationBar, you can use IndexedStack

        @override
          Widget build(BuildContext context) {
            return Scaffold(
              bottomNavigationBar: BottomNavigationBar(
                onTap: (index) {
                  setState(() {
                    current_tab = index;
                  });
                },
                currentIndex: current_tab,
                items: [
                  BottomNavigationBarItem(
                    ...
                  ),
                  BottomNavigationBarItem(
                    ...
                  ),
                ],
              ),
              body: IndexedStack(
                children: [
                  PageOne(),
                  PageTwo(),
                ],
                index: current_tab,
              ),
            );
          }
    

提交回复
热议问题