中心按钮凸起的底部导航栏

一、效果

二、核心思路

利用floatingActionButton结合BottomAppBar,使得floatingActionButton悬浮在BottomAppBar之上,以达到中心凸起的效果。

三、BottomAppBar部分代码参考

Widget myBottomAppBar() {
    return BottomAppBar(
      height: 80,
      color: Colors.blue.withOpacity(0.5),
      // 底部导航栏的形状,可以让中间按钮凸出
      shape: const CircularNotchedRectangle(), 
      notchMargin: 10,
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          InkWell(
            // 去除InkWell的点击效果
            splashFactory: NoSplash.splashFactory,
            onTap: () {
             // 使用的页面控制器切换页面
              _pageController.jumpToPage(0);
              setState(() {
                currentPage = 0;
              });
            },
            child: Column(
              children: [
                Icon(
                  Icons.home,
                  color: Colors.white,
                  size: currentPage == 0 ? 45 : 24,
                ),
                const SizedBox(
                  height: 3,
                ),
                Visibility(
                  visible: currentPage != 0,
                  child: const Text(
                    "HOME",
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ),
              ],
            ),
          ),
          // 空间占位符,为了让两侧的图标和中间的按钮保持间隔
          // width设置数值越大,两侧的导航图标离中间的按钮越远
          const SizedBox(
            width: 0,
          ),
          InkWell(
            // 去除InkWell的点击效果
            splashFactory: NoSplash.splashFactory,
            onTap: () {
              _pageController.jumpToPage(1);
              setState(() {
                currentPage = 1;
              });
            },
            child: Column(
              children: [
                Icon(
                  Icons.message,
                  color: Colors.white,
                  size: currentPage == 1 ? 45 : 24,
                ),
                const SizedBox(
                  height: 3,
                ),
                Visibility(
                  visible: currentPage != 1,
                  child: const Text(
                    "MESSAGE",
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

四、floatingActionButton部分代码参考

floatingActionButton要求的属性为Widget,我这里写了SizedBox嵌套FloatingActionButton是为了控制按钮的大小。

 floatingActionButton: SizedBox(
        width: 66,
        height: 66,
        child: FloatingActionButton(
          onPressed: () {
            // MyToast是我写的自定义Toast
            MyToast.showShortToast("floatingActionButton");
          },
          // 设置按钮的背景颜色
          backgroundColor: Colors.white, 
          // 确保按钮是圆形的
          shape: const CircleBorder(), 
          elevation: 10.0,
          child: const Icon(Icons.center_focus_weak),
        ),
      ),
  // 使得按钮居中显示
  floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

五、引用代码参考

  List<Widget> tabs = [const PageA(), const PageB()];
  final PageController _pageController = PageController();
  int currentPage = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: myBottomAppBar(),
      floatingActionButton: SizedBox(
        width: 66,
        height: 66,
        child: FloatingActionButton(
          onPressed: () {
            MyToast.showShortToast("floatingActionButton");
          },
          // 设置按钮的背景颜色
          backgroundColor: Colors.white, 
          // 确保按钮是圆形的
          shape: const CircleBorder(), 
          elevation: 10.0,
          child: const Icon(Icons.center_focus_weak),
        ),
      ),
      // 使得按钮居中显示
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: PageView(
        physics: const NeverScrollableScrollPhysics(), 
        controller: _pageController,
        children: tabs,
        onPageChanged: (int index) {
          setState(() {
            currentPage = index;
          });
        },
      ),
    );
  }

六、总结

需要注意的是,BottomAppBar中的占位符是一定要写的,哪怕SizedBox的width为0,我尝试了删掉SizedBox,无法达到显示效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HQL_seven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值