Flutter自定义tabbar任意样式

场景描述

最近在使用遇到几组需要自定义的tabbar或者类似组件,在百度查询资料中通常,需要自定义

TabIndicator extends Decoration

比如上图中的带圆角的指示器这样实现

就很麻烦, 搜出来的相关也是在此之处上自己画,主要再遇到一个稍微特殊的,比如带背景切换的,带特殊图形或者path的,费事费力。

有经验同学可能就会相当那我干脆直接用radiogroup做单选然后去关联page切换。但再劳神之前,

不如点进去tabbar看一下,flutter精髓之一就是万物皆为widget,局部子的自定义正式优势所在, 

 思路描述

  /// The length of this list must match the [controller]'s [TabController.length]
  /// and the length of the [TabBarView.children] list.
  final List<Widget> tabs;

很明显这个是可以随意自定义的,

我们先定义好 两种切换状态的Widget

getAllTabs() {
      return <Tab>[
        for (int i = 0; i < controller.tabs.length; i++)
          Tab(
            child: Stack(
              children: [
                ImageUtils.assetImage(
                    isSelect
                        ? "bg_data_tab_select"
                        : "bg_data_tab_unselect",
                    width: 72.w,
                    height: 34.w,
                    fit: BoxFit.cover),
                // 这个是我自定义背景 文字widget 你可以使用普通text
                UIText(
                  widgetWidth: 72.w,
                  widgetHeight: 34.w,
                  fontWeight: FontWeight.w500,
                  text: controller.tabs[i],
                  fontSize: 18.w,
                  textAlign: TextAlign.center,
                  widgetAlignment: Alignment.center,
                  fontColor: isSelect
                      ? const Color(0xFF202437)
                      : Colors.white,
                  letterSpacing: -0.04,
                )
              ],
            ),
          ),
      ];

此时我们只需要得到isSelect值,改变的时候动态去刷新state即可 将前面的isSelect 改为controller.tabIndex =i即可

  TabBar(
                      onTap: (index) {
                        // 赋值
                        controller.tabIndex = index;
                        // 封装的刷新 一般对应setState
                        controller.update();
},

相关推荐

  1. uniapp定义tabBar

    2024-02-21 16:22:02       26 阅读
  2. uni-app定义tabbar(van-tabbar

    2024-02-21 16:22:02       49 阅读
  3. uni-app 定义tabbar

    2024-02-21 16:22:02       35 阅读
  4. Flutter中创建定义的左对齐TabBar组件

    2024-02-21 16:22:02       44 阅读
  5. Vue 定义菜单、tabBar效果

    2024-02-21 16:22:02       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-21 16:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-21 16:22:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-21 16:22:02       82 阅读
  4. Python语言-面向对象

    2024-02-21 16:22:02       91 阅读

热门阅读

  1. 时间片大小

    2024-02-21 16:22:02       51 阅读
  2. 开源软件:推动软件开发行业繁荣的关键力量

    2024-02-21 16:22:02       56 阅读
  3. Mysql全局级别修改SQL模式的详细教程

    2024-02-21 16:22:02       53 阅读
  4. Go的闭包理解

    2024-02-21 16:22:02       51 阅读
  5. 华为配置STA双栈业务覆盖业务示例

    2024-02-21 16:22:02       50 阅读
  6. SpringBoot整理-错误处理

    2024-02-21 16:22:02       54 阅读
  7. 前端构造树算法优化

    2024-02-21 16:22:02       47 阅读
  8. 正则表达式预查寻也称断言,限定左右相邻内容

    2024-02-21 16:22:02       53 阅读
  9. QT day2

    QT day2

    2024-02-21 16:22:02      50 阅读