flutter 自定义底部 BottomNavigationBar


前言

在日常开发中,很多时候flutter 自带的BottomNavigationBar 无法满足我们的需求,那么就需要自定义BottomNavigationBar 了,本篇文章将记录自定义 BottomNavigationBar 的封装,可以扩展成我们在日常开发中所有的需求。当然,本篇文章使用provider 来封装的,如果不使用provider,只需要看中间的widget 文件就可以了。


一、封装viewmodel

在 viewmodel 中,我们需要定义一个当前选中下标,所有tabItem 的数组,还需要一个选中下标的切换方法

int currentIndex = 0;

  List barItemList = [
    {
      "title": "首页",
      "normalIcon": 'assets/images/ic_error.png',
      "selectIcon": 'assets/images/ic_error.png'
    },
    {
      "title": "通讯录",
      "normalIcon": 'assets/images/ic_error.png',
      "selectIcon": 'assets/images/ic_error.png'
    },
    {
      "title": "发现",
      "normalIcon": 'assets/images/ic_error.png',
      "selectIcon": 'assets/images/ic_error.png'
    },
    {
      "title": "我",
      "normalIcon": 'assets/images/ic_error.png',
      "selectIcon": 'assets/images/ic_error.png'
    },
  ];

  // 选中下标的切换
  changeBottomTabIndex(int index) {
    currentIndex = index;
    notifyListeners();
  }
  

为了提升后期的可扩展性,我将使用PageController 来管理我的页面,因此我们还需要初始化一个 PageController

  PageController tabPageController = PageController();

二、封装 bottomNavigationWidget

前面我们封装了tabItem 的属性,交互等功能,现在我们就要来实现UI部分的封装了。

  1. bottomWidget 的封装,我这里直接封装成一个组件,并且在组件中自定义了整个样式
  static Widget bottomNavigationWidget(TabNavigationViewModel model) {
    return Container(
      height: 56.h + Get.mediaQuery.padding.bottom,
      decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(24.r),
            topLeft: Radius.circular(24.r),
          ),
          boxShadow: [BoxShadow(color: Colors.green, blurRadius: 0.12.r)]),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: List<Widget>.generate(model.barItemList.length,
            (index) => _barItem(model.barItemList[index], model, index)),
      ),
    );
  }

从上面代码中,可以看到 height 的高度,我给了一个56的高度,再加上一个bottom 的高度,因为有些手机是有底部bottom 的,所以我们需要把这个高度给预留出来。

然后使用 List.generate 的方式,生成单独的itemBar ,接下来看一下 _barItem 组件的封装

  static Widget _barItem(item, TabNavigationViewModel model, int index) {
    return InkWell(
      onTap: () {
        if (model.currentIndex != index) {
          model.currentIndex = index;
          model.changeBottomTabIndex(index);
          model.tabPageController.jumpToPage(index);
        }
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            model.currentIndex == index
                ? item['selectIcon']
                : item['normalIcon'],
            width: 24,
            height: 24,
            fit: BoxFit.cover,
          ),
          const SizedBox(
            height: 10,
          ),
          Text(
            item['title'],
            style: TextStyle(
              color: model.currentIndex == index ? Colors.white : Colors.black,
            ),
          ),
        ],
      ),
    );
  }

代码写到这里,我们需要使用的整个BottomNavigationBar 就已经封装完成了。

三、自定义 BottomNavigationBar 的使用

前面封装完成了整个BottomNavigationBar 的自定义,那么我们该怎么使用呢?

Scaffold(
            body: PageView(
              controller: model.tabPageController,
              // physics: const NeverScrollableScrollPhysics(),
              children: [
                const HomePage(),
                Container(
                  color: Colors.grey,
                ),
                Container(
                  color: Colors.red,
                ),
                Container(
                  color: Colors.grey,
                ),
              ],
              onPageChanged: (index) {
                model.currentIndex = index;
                model.changeBottomTabIndex(index);
              },
            ),
            bottomNavigationBar:
              TabNavigationWidget.bottomNavigationWidget(model),
          )

前面我提到过PageController 页面管理器,这里我们当然也要使用了,不熟悉的可以单独去了解一下,现在我们来看一下运行效果。

在这里插入图片描述


总结

本篇文章通过少量的代码,轻轻松松就实现了BottomNavigationBar 的自定义封装,是不是很奈斯?

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
Flutter是一种跨平台的移动应用开发框架,它使用Dart语言进行编写。在Flutter中,可以通过自定义控件来实现特定的功能或者界面效果。下面是Flutter自定义控件开发的指南: 1. 创建一个新的控件类:在Flutter中,可以通过继承StatefulWidget或者StatelessWidget来创建一个新的控件类。StatefulWidget是有状态的控件,可以根据需要更新状态并重新渲染界面;StatelessWidget是无状态的控件,一旦创建就不会再改变。 2. 实现build方法:在控件类中,需要实现一个build方法,该方法返回一个Widget对象,用于描述控件的外观和行为。可以使用Flutter提供的丰富的Widget库来构建界面,也可以自定义绘制逻辑。 3. 添加属性:通过在控件类中定义属性,可以让用户在使用该控件时传入不同的参数,从而实现控件的可配置性。可以使用构造函数来接收属性,并在build方法中使用这些属性来构建界面。 4. 处理用户交互:如果需要处理用户的点击、滑动等交互操作,可以在控件类中添加相应的回调函数,并将其传递给子控件。子控件可以通过调用回调函数来通知父控件发生了交互事件。 5. 更新状态:如果创建的是有状态的控件,可以通过调用setState方法来更新控件的状态,并触发界面的重新渲染。在setState方法中,可以修改控件的属性值,然后Flutter会自动调用build方法来重新构建界面。 6. 使用自定义控件:在其他地方使用自定义控件时,只需要创建该控件的实例,并将其添加到界面中即可。可以通过设置属性来配置控件的外观和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

半身风雪

感谢打赏,你的鼓励,是我创作的

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

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

打赏作者

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

抵扣说明:

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

余额充值