You need to enable JavaScript to run this app. Flutter 如何实现匹配参考效果图的自定义Tab Bar?
优惠活动
大模型
产品
解决方案
定价
更多

Flutter 如何实现匹配参考效果图的自定义Tab Bar?

参考样式TabBar实现方案

以下代码可直接复现对应参考图的全部视觉特征,核心是通过自定义指示器替换默认的下划线样式,无需从零实现手势滑动逻辑。

核心样式对应逻辑

  • 整体Tab容器使用浅灰色大圆角包裹,内边距统一留4dp空间给选中态的白色胶囊块
  • 选中指示器为纯白色胶囊形状,跟随Tab切换做平滑位移动画
  • 无分割线、无点击水波纹效果,未选中文字为中灰色,选中文字为纯黑色稍高字重

可直接运行的实现代码

首先定义自定义胶囊指示器,负责绘制选中态的白色块,支持切换时的平滑动画插值:

import 'package:flutter/material.dart';

class CapsuleTabIndicator extends Decoration {
  final BoxDecoration decoration;
  const CapsuleTabIndicator({required this.decoration});

  @override
  Decoration? lerpFrom(Decoration? a, double t) {
    if (a is CapsuleTabIndicator) {
      return CapsuleTabIndicator(
        decoration: BoxDecoration.lerp(a.decoration, decoration, t),
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
  Decoration? lerpTo(Decoration? b, double t) {
    if (b is CapsuleTabIndicator) {
      return CapsuleTabIndicator(
        decoration: BoxDecoration.lerp(decoration, b.decoration, t),
      );
    }
    return super.lerpTo(b, t);
  }

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _CapsulePainter(decoration, onChanged);
  }
}

class _CapsulePainter extends BoxPainter {
  final BoxDecoration decoration;
  _CapsulePainter(this.decoration, VoidCallback? onChanged) : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    // 内缩4dp保证选中块和外层容器的均匀边距
    final Rect rect = (offset & configuration.size!).deflate(const EdgeInsets.all(4));
    final Paint paint = Paint()..color = decoration.color ?? Colors.white;
    final RRect rrect = RRect.fromRectAndRadius(
      rect,
      Radius.circular(rect.height / 2),
    );
    canvas.drawRRect(rrect, paint);
  }
}

接着组装TabBar组件,复用Flutter原生TabBar的滑动、手势逻辑,仅调整样式配置:

class CustomTabBar extends StatelessWidget {
  final TabController controller;
  final List<String> tabTitles;
  const CustomTabBar({
    super.key,
    required this.controller,
    required this.tabTitles,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
      padding: const EdgeInsets.all(4),
      decoration: BoxDecoration(
        color: const Color(0xFFF2F3F7),
        borderRadius: BorderRadius.circular(24),
      ),
      child: TabBar(
        controller: controller,
        indicator: const CapsuleTabIndicator(
          decoration: BoxDecoration(color: Colors.white),
        ),
        indicatorPadding: EdgeInsets.zero,
        indicatorSize: TabBarIndicatorSize.tab,
        labelColor: Colors.black,
        labelStyle: const TextStyle(fontWeight: FontWeight.w600, fontSize: 14),
        unselectedLabelColor: const Color(0xFF666666),
        unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w400, fontSize: 14),
        splashFactory: NoSplash.splashFactory,
        dividerColor: Colors.transparent,
        tabs: tabTitles.map((title) => Tab(text: title)).toList(),
      ),
    );
  }
}

微调提示

  • 若需要调整选中块的边距,直接修改_CapsulePainter内的deflate数值即可
  • 若需要改变圆角大小,调整外层容器的borderRadius数值匹配视觉效果
  • 该实现自带平滑切换动画,和参考图的交互效果完全一致,无需额外手动维护滑动状态

内容的提问来源于stack exchange,提问作者Panit Hongpan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.05 02:06:06