flutter-實(shí)現(xiàn)一個(gè)圓角指示器(下劃線)的TabBar

image.png

查看UnderlineTabIndicator發(fā)現(xiàn),底部的指示器是通過(guò)_UnderlinePainter畫筆劃了一條線.
下面我們直接把這個(gè)畫筆改成圓角的就可以咯~


///參考UnderlineTabIndicator,僅僅修改畫筆StrokeCap.square 為 StrokeCap.round
class CustomUnderlineTabIndicator extends Decoration {
  /// Create an underline style selected tab indicator.
  ///
  /// The [borderSide] and [insets] arguments must not be null.
  const CustomUnderlineTabIndicator({
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero,
  })  : assert(borderSide != null),
        assert(insets != null);

  /// The color and weight of the horizontal line drawn below the selected tab.
  final BorderSide borderSide;

  /// Locates the selected tab's underline relative to the tab's boundary.
  ///
  /// The [TabBar.indicatorSize] property can be used to define the tab
  /// indicator's bounds in terms of its (centered) tab widget with
  /// [TabBarIndicatorSize.label], or the entire tab with
  /// [TabBarIndicatorSize.tab].
  final EdgeInsetsGeometry insets;

  @override
  Decoration? lerpFrom(Decoration? a, double t) {
    if (a is UnderlineTabIndicator) {
      return UnderlineTabIndicator(
        borderSide: BorderSide.lerp(a.borderSide, borderSide, t),
        insets: EdgeInsetsGeometry.lerp(a.insets, insets, t)!,
      );
    }
    return super.lerpFrom(a, t);
  }

  @override
  Decoration? lerpTo(Decoration? b, double t) {
    if (b is UnderlineTabIndicator) {
      return UnderlineTabIndicator(
        borderSide: BorderSide.lerp(borderSide, b.borderSide, t),
        insets: EdgeInsetsGeometry.lerp(insets, b.insets, t)!,
      );
    }
    return super.lerpTo(b, t);
  }

  @override
  _UnderlinePainter createBoxPainter([VoidCallback? onChanged]) {
    return _UnderlinePainter(this, onChanged);
  }

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    assert(rect != null);
    assert(textDirection != null);
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);
    return Rect.fromLTWH(
      indicator.left,
      indicator.bottom - borderSide.width,
      indicator.width,
      borderSide.width,
    );
  }

  @override
  Path getClipPath(Rect rect, TextDirection textDirection) {
    return Path()..addRect(_indicatorRectFor(rect, textDirection));
  }
}

class _UnderlinePainter extends BoxPainter {
  _UnderlinePainter(this.decoration, VoidCallback? onChanged)
      : assert(decoration != null),
        super(onChanged);

  final CustomUnderlineTabIndicator? decoration;

  BorderSide? get borderSide => decoration?.borderSide;

  EdgeInsetsGeometry? get insets => decoration?.insets;

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration? configuration) {
    assert(configuration != null);
    assert(configuration!.size != null);
    final Rect rect = offset & configuration!.size!;
    final TextDirection textDirection = configuration.textDirection!;
    final Rect indicator = decoration!._indicatorRectFor(rect, textDirection)
        .deflate(decoration!.borderSide.width / 2.0);
    final Paint paint = decoration!.borderSide.toPaint();
    paint.strokeWidth = 5;
    paint.strokeCap = StrokeCap.round; //主要是修改此處  圓角
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  }
}

然后再Tabbar中指定這個(gè)CustomUnderlineTabIndicator

TabBar(
      indicatorWeight: 5,
      indicatorSize: TabBarIndicatorSize.label,
      indicator: CustomUnderlineTabIndicator(
        insets: EdgeInsets.only(top: 10.w, bottom: 2.w),
        borderSide: BorderSide(width: 5.w, color: Colors.red),
      ),
      // indicator: BoxDecoration(
      //     borderRadius: BorderRadius.circular(3.5.w),
      //     gradient: LinearGradient(
      //       begin: Alignment.centerLeft,
      //       end: Alignment.centerRight,
      //       colors: [Color(0xff54C9FF), Colors.blue],
      //     )),
      controller: _tabController,
      tabs: _tabList.map((str) {
        return Container(
          height: 50,
          color: Colors.white,
          child: Center(
            child: new Text(
              str,
              style: TextStyle(color: Colors.black, fontSize: 25.sp),
            ),
          ),
        );
      }).toList(),
    )

喜歡的話,幫忙點(diǎn)贊哦~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末毫目,一起剝皮案震驚了整個(gè)濱河市兑宇,隨后出現(xiàn)的幾起案子明垢,更是在濱河造成了極大的恐慌梅猿,老刑警劉巖模聋,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件坑傅,死亡現(xiàn)場(chǎng)離奇詭異筹淫,居然都是意外死亡皱炉,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門溪厘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)胡本,“玉大人,你說(shuō)我怎么就攤上這事畸悬〔喔Γ” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵蹋宦,是天一觀的道長(zhǎng)披粟。 經(jīng)常有香客問(wèn)我,道長(zhǎng)冷冗,這世上最難降的妖魔是什么守屉? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮贾惦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘怀吻。我一直安慰自己痊项,他們只是感情好乞封,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著习瑰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秽荤。 梳的紋絲不亂的頭發(fā)上甜奄,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天柠横,我揣著相機(jī)與錄音,去河邊找鬼课兄。 笑死牍氛,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的烟阐。 我是一名探鬼主播搬俊,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼蜒茄!你這毒婦竟也來(lái)了唉擂?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤檀葛,失蹤者是張志新(化名)和其女友劉穎玩祟,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屿聋,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡空扎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了胜臊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片勺卢。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖象对,靈堂內(nèi)的尸體忽然破棺而出黑忱,到底是詐尸還是另有隱情,我是刑警寧澤勒魔,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布甫煞,位于F島的核電站,受9級(jí)特大地震影響冠绢,放射性物質(zhì)發(fā)生泄漏抚吠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一弟胀、第九天 我趴在偏房一處隱蔽的房頂上張望楷力。 院中可真熱鬧,春花似錦孵户、人聲如沸萧朝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)检柬。三九已至,卻和暖如春竖配,著一層夾襖步出監(jiān)牢的瞬間何址,已是汗流浹背里逆。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留用爪,地道東北人原押。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像项钮,于是被迫代替她去往敵國(guó)和親班眯。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容