flutter 自定义TabBar,实现 高度 和 标题与图标距离 可自定义的方案与实践

TabBar 是基本每个App都会使用到的一个控件,在官方内置的 TabBar 的高度只有两种规格且是不可修改的:

//未设置 Icon 时的高度
const double _kTabHeight = 46.0;
//设置 Icon 之后的高度
const double _kTextAndIconTabHeight = 72.0;

标题与Icon之间的距离也是写死的:

margin: const EdgeInsets.only(bottom: 10.0),

Tab高度/文本与Icon的距离 设置详见 class Tab 源码:

class Tab extends StatelessWidget {
  const Tab({
    Key key,
    this.text,
    this.icon,
    this.child,
  }) : assert(text != null || child != null || icon != null),
       assert(!(text != null && null != child)), 
       super(key: key);

  final String text;

  final Widget child;

  final Widget icon;

  Widget _buildLabelText() {
    return child ?? Text(text, softWrap: false, overflow: TextOverflow.fade);
  }

  @override
  Widget build(BuildContext context) {
    assert(debugCheckHasMaterial(context));

    double height;
    Widget label;
    if (icon == null) {//当没有设置 Icon 时,Tab 高度 height 取值 _kTabHeight
      height = _kTabHeight;
      label = _buildLabelText();
    } else if (text == null && child == null) {//当没有设置 text 和 child 时,Tab 高度 height 取值 _kTabHeight
      height = _kTabHeight;
      label = icon;
    } else {//其它情况, Tab 高度 height 取值 _kTextAndIconTabHeight
      height = _kTextAndIconTabHeight;
      label = Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Container(
            child: icon,
            margin: const EdgeInsets.only(bottom: 10.0),//这里设置的是 text 和 Icon 的距离
          ),
          _buildLabelText(),
        ],
      );
    }

    return SizedBox(
      height: height,
      c
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值