Flutter底部导航栏之BottomNavigationBar

实现的方式

​ 因为Flutter的widget分为StatelessWidget和StatefulWidget.我们使用底部导航栏肯定是通过点击底部的导航按钮来实现页面的更改,所以包含BottomNavigationBar的Widget必须是一个StatefulWidget.

​ 因为Scaffold这个widget中默认对BottomNavigationBar支持.所以直接用Scaffold就可以了.

效果图

在这里插入图片描述

直接贴代码

class BottomNavigationWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => BottomNavigationWidgetState();
}

class BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  final _bottomNavigationColor = primaryColor;
  int _currentIndex = 0;
  List<Widget> bodyList = List();

  @override
  void initState() {
    super.initState();
    bodyList
      ..add(HomeScreen())
      ..add(DiscoverScreen())
      ..add(WorldScreen())
      ..add(MineScreen());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: bodyList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home, color: _bottomNavigationColor),
            title: Text(
              '主页',
              style: _bottomNavigationTextStyle(),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.camera, color: _bottomNavigationColor),
            title: Text(
              '发现',
              style: _bottomNavigationTextStyle(),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.language, color: _bottomNavigationColor),
            title: Text(
              '世界',
              style: _bottomNavigationTextStyle(),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(
              Icons.supervised_user_circle,
              color: _bottomNavigationColor,
            ),
            title: Text(
              '我的',
              style: _bottomNavigationTextStyle(),
            ),
          ),
        ],
        currentIndex: _currentIndex,
        onTap: (int index) {
          setState(() {
            _currentIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  //底部导航栏的样式
  TextStyle _bottomNavigationTextStyle() => TextStyle(color: primaryColor);
}

解析

​ 先在底部设置四个BottomNavigationBarItem,然后我们点击不同的item,更改_currentIndex的值,然后通过setState来使页面重绘.重绘以后bodyList就会根据改变过的 _currentIndex获取到不同body,这就完成了页面的更改

参数解析
Key key,
@required this.items,
this.onTap,
this.currentIndex = 0,
BottomNavigationBarType type,
this.fixedColor,
this.iconSize = 24.0,
  • item 就是底部的四个按钮

  • onTap 就是按钮的点击方法,会传递一个index参数,index就是点击了第几个按钮

  • iconSize 底部icon的大小

  • fixedColor:如果你的icon没有设置color,选中的时候就会用到fixedColor的颜色,当BottomNavigationBarType是BottomNavigationBarType.shifting的时候,这个属性是不起作用的

  • type BottomNavigationBarType.两个字段,一个是只有选中的时候才显示icon下面的text,另一个是不管选中不选中都显示text.

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值