在Flutter里BottomNavigationBar、TabBar、还有Drawer,你就会发现,在切换页面之后,由于Widget的重绘,再返回原来的页面时状态丢失。
解决方法:
1、如果body 使用PageView包装,然后在各子 List<Widget> 里 实现AutomaticKeepAliveClientMixin类 里的 wantKeepAlive 返回 true ;
///add
final pageController = PageController();
TableBar里用的是PageView
return Scaffold(
body: PageView(
children: pages,
controller: pageController,
onPageChanged: (int index){
setState(() {
_selectedItem = index%3;
});
},
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
items: [
_getBottomNavBarItem(Icons.home, "首页", _barColor(0,context)),
_getBottomNavBarItem(Icons.video_library, "视频", _barColor(1,context)),
_getBottomNavBarItem(Icons.person, "我的", _barColor(2,context)),
],
curren