butry

flutter中BottomNavigationBar切换重置页面的问题

1.不要相信AutomaticKeepAliveClientMixin,纯属误人子弟

2.非常简单

3.上代码:

var _pageList;//定义页面列表
int _currentIndex;//定义bottomNavigationBar点击的坐标

@override
void initState() {

//设置初始数据
_pageList       = [HomePage(), DiscoverPage(), UserCenterPage()];
_currentIndex   = 0;
}


return Scaffold(
  bottomNavigationBar: BottomNavigationBar(
    currentIndex: _currentIndex,
    items: [
BottomNavigationBarItem(
            icon: Icon(Icons.home,),
            title: Text('首页'),),

BottomNavigationBarItem(
            icon: Icon(Icons.looks,),
            title: Text('发现')),

BottomNavigationBarItem(
            icon: Icon(Icons.supervised_user_circle,),
            title: Text('我的')),
      ],

//点击bottomNavigationBar修改状态
    onTap: (int index){
      setState(() {
_currentIndex = index;
      });
    },
  ),
//重点,重点是使用IndexedStack来创建body
  body: IndexedStack(
    index: _currentIndex,
    children: _pageList,
  ),

);

③大功告成!



评论