Flutter设置TabBar indicator宽度(爆改UnderlineTabIndicator )

本文介绍了如何修改Flutter TabBar的indicator宽度。在默认情况下,indicator宽度是平分每个Tab的,但通过源码探索,我们可以创建自定义的UnderlineTabIndicator并调整其宽度。文章提供了详细的源码修改步骤和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在默认的TabBar中,indicator的宽度是不可以修改的,每个长度都是平分一个Item的,如下图:

在这里插入图片描述

这个是未修改的图,下面是修改的图,总的宽度为20

在这里插入图片描述

总的在源码里翻来翻去,发现并没有可以设置Indicator的字段,这时候,尝试着通过源码来修改indicator的宽度,同时也是借助了这篇文章,不过,给的有些随意,故在这记录之。

源码探索:

点进TabBar的源码

  const TabBar({
   
    Key key,
    @required this.tabs,
    this.controller,
    this.isScrollable = false,
    this.indicatorColor,
    this.indicatorWeight = 2.0,
    this.indicatorPadding = EdgeInsets.zero,
    this.indicator,  //这是我们要修改的indicator
    this.indicatorSize,
    this.labelColor,
    this.labelStyle,
    this.labelPadding,
    this.unselectedLabelColor,
    this.unselectedLabelStyle,
    this.dragStartBehavior = DragStartBehavior.start,
    this.onTap,
  }) : assert(tabs != null),
       assert(isScrollable != null),
       assert(dragStartBehavior != null),
       assert(indicator != null || (indicatorWeight != null && indicatorWeight > 0.0)),
       assert(indicator != null || (indicatorPadding != null)),
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值