Flutter学习Flutter

Flutter中BottomNavigationBar+Page

2021-09-02  本文已影响0人  男人宫
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,
        //选中和未选中设置一样的字体大小,就不会出现类似动画的情况
        selectedFontSize: 14,
        unselectedFontSize: 14,
        //不设置显示不出文字
        type: BottomNavigationBarType.fixed,
        //设置选中时的颜色
        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时会发生闪动的情况
            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);
上一篇 下一篇

猜你喜欢

热点阅读