- 網(wǎng)上有很多方案,試了一下,簡單的BottomNavigationBar+AutomaticKeepAliveClientMixin是不能實現(xiàn)頁面狀態(tài)保持的.同時BottomNavigationBar+IndexedStack+AutomaticKeepAliveClientMixin會出現(xiàn)進入首頁就把tababr的幾個頁面都加載的情況,也不是最優(yōu)方案.最終解決問題的方案就是:BottomNavigationBar+PageView+AutomaticKeepAliveClientMixin.直接上代碼
class _TabbarPageState extends State<TabbarPage> {
PageController _pageController;
int currentIndex = 0;
List<Widget> pagesLists = [
HomePageView(),
LeavePageView(),
MinePageView(),
];
List<String> navTitleLsit = ["首頁", "請假","我的"];
@override
void initState() {
_pageController = PageController(initialPage: 0);
super.initState();
}
@override
void dispose() {
super.dispose();
_pageController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors.kColor_ListViewbackGround,
body: PageView.builder(
controller: _pageController,
itemCount: pagesLists.length,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
return pagesLists[index];
},
onPageChanged: (index) {
setState(() {
currentIndex = index;
});
},
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
//選中和未選中設(shè)置一樣的字體大小,就不會出現(xiàn)類似動畫的情況
selectedFontSize: 14,
unselectedFontSize: 14,
//不設(shè)置顯示不出文字
type: BottomNavigationBarType.fixed,
//設(shè)置選中時的顏色
selectedItemColor: AppColors.kColor_NavgationBar,
currentIndex: currentIndex,
items: [
buliderBottomNavgationBarItem("home", navTitleLsit[0]),
buliderBottomNavgationBarItem("leave", navTitleLsit[1]),
buliderBottomNavgationBarItem("me", navTitleLsit[2])
],
onTap: (index) {
_pageController.jumpToPage(index);
},
),
);
}
BottomNavigationBarItem buliderBottomNavgationBarItem(
String imageNameStr, String titleStr) {
return BottomNavigationBarItem(
icon: Image.asset("images/tabimages/${imageNameStr}_close.png",
//下面兩行代碼解決點擊icon時會發(fā)生閃動的情況
excludeFromSemantics: true,
gaplessPlayback: true,
width: 23),
activeIcon: Image.asset(
"images/tabimages/${imageNameStr}_open.png",
excludeFromSemantics: true,
gaplessPlayback: true,
width: 23,
),
label: titleStr);
}
}
1.class _MinePageViewState extends State<MinePageView> with AutomaticKeepAliveClientMixin
2.@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
3.super.build(context);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者