问题描述:使用 bottomNavigationBar +pageView切换页面,切换第二个页面后再次切换第一个页面,发现重复创建触发了网路请求,好坑!
Widget build(BuildContext context) {
return Scaffold(
body: PageView.builder(
controller: _pageController,
itemCount: _bodyList.length,
itemBuilder: (BuildContext context, int index) {
return _bodyList[index];
},
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
onTap: (index) {
//点击触发 切换 currentIndex;
setState(() {
_currentNavigationBarItem = index;
});
_pageController.animateToPage(index,
duration: Duration(microseconds: 1), curve: Curves.ease);
},
currentIndex: _currentNavigationBarItem,
items: _bottomNavigationBarItemList,
),
);
}
解决方法1: pageveiw的children 实现 AutomaticKeepAliveClientMixin
(官方推荐