Flutter 頂部TabBar

截屏2021-01-12 16.21.55.png

需求是這樣的,本來都想自定義TabBar床三,沒有想到TabBar原生的就支持這個(gè)方式,只需要設(shè)置以下式樣就能達(dá)到效果了

unselectedLabelStyle: TextStyle(fontSize: 16), //未選中樣式
labelStyle: TextStyle( fontSize: 24, height: 2), //選中樣式
isScrollable: true, // 是否可以滑動(dòng)
labelPadding: EdgeInsets.only(left: 16,top: 15, right: 10),  //文本偏移

完整代碼

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: _tabBar(),
        automaticallyImplyLeading: false,
        elevation: 0, //去掉Appbar底部陰影
        centerTitle: false,
        actions: <Widget>[
          IconButton(
              key: btnKey,
              icon: Image(
                image: AssetImage("assets/images/bokong/bokong_more.png"),
                alignment: Alignment.center,
              ),
              onPressed: () {

              }),
        ],
      ),
      body: _tabbarView(context),
    );
  }

TabBar _tabBar() {
    return TabBar(
      isScrollable: true, // 是否可以滑動(dòng)
      controller: _tabController,
      //tab文字類型
      unselectedLabelStyle: TextStyle(fontSize: 16), //未選擇樣式
      labelStyle: TextStyle( fontSize: 24, height: 2), //選擇樣式
      //選中的顏色
      labelColor: ColorsUtil.hexStringColor('111E36'),
      //未選中的顏色
      unselectedLabelColor: ColorsUtil.hexStringColor('666D7F'),
      //自定義指示器
      indicator: CustomUnderlineTabIndicator(
        borderSide: BorderSide(width: 2.0, color: ColorsUtil.hexStringColor('EC1B23')),
        // insets: EdgeInsets.only(left: 16, right: 16, top: 15),
      ),
      //文本偏移
      labelPadding: EdgeInsets.only(left: 16,top: 15, right: 10),
      tabs: _tabTitles.map((value) {
        return Center(child: Text(value));
      }).toList(),
    );
  }
//自定義指示器--實(shí)現(xiàn)方式:復(fù)制UnderlineTabIndicator類的源碼楼眷,新建類,修改StrokeCap.square為StrokeCap.round
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class CustomUnderlineTabIndicator extends Decoration {
  const CustomUnderlineTabIndicator({
    this.borderSide = const BorderSide(width: 2.0, color: Colors.white),
    this.insets = EdgeInsets.zero,
  }) : assert(borderSide != null),
        assert(insets != null);

  final BorderSide borderSide;

  final EdgeInsetsGeometry insets;

  @override
  Decoration lerpFrom(Decoration a, double t) {
    if (a is CustomUnderlineTabIndicator) {
      return CustomUnderlineTabIndicator(
        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 CustomUnderlineTabIndicator) {
      return CustomUnderlineTabIndicator(
        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);
  }
}

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;

  Rect _indicatorRectFor(Rect rect, TextDirection textDirection) {
    assert(rect != null);
    assert(textDirection != null);
    final Rect indicator = insets.resolve(textDirection).deflateRect(rect);

    //希望的寬度
    double wantWidth = 35;
    //取中間坐標(biāo)
    double cw = (indicator.left + indicator.right) / 2;
    return Rect.fromLTWH(cw - wantWidth / 2 ,
        indicator.bottom - borderSide.width, wantWidth, borderSide.width);
  }

  @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 = _indicatorRectFor(rect, textDirection).deflate(borderSide.width / 2.0);
    //設(shè)置為圓角
    final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.round;
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  }
}
截屏2021-01-12 16.27.20.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市高诺,隨后出現(xiàn)的幾起案子摘悴,更是在濱河造成了極大的恐慌峭梳,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹂喻,死亡現(xiàn)場(chǎng)離奇詭異葱椭,居然都是意外死亡捂寿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門孵运,熙熙樓的掌柜王于貴愁眉苦臉地迎上來秦陋,“玉大人,你說我怎么就攤上這事治笨□饴拢” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵大磺,是天一觀的道長(zhǎng)抡句。 經(jīng)常有香客問我,道長(zhǎng)杠愧,這世上最難降的妖魔是什么待榔? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮流济,結(jié)果婚禮上锐锣,老公的妹妹穿的比我還像新娘。我一直安慰自己绳瘟,他們只是感情好雕憔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著糖声,像睡著了一般斤彼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蘸泻,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天琉苇,我揣著相機(jī)與錄音,去河邊找鬼悦施。 笑死并扇,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的抡诞。 我是一名探鬼主播穷蛹,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼昼汗!你這毒婦竟也來了肴熏?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤乔遮,失蹤者是張志新(化名)和其女友劉穎扮超,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蹋肮,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡出刷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了坯辩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片馁龟。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖漆魔,靈堂內(nèi)的尸體忽然破棺而出坷檩,到底是詐尸還是另有隱情,我是刑警寧澤改抡,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布矢炼,位于F島的核電站,受9級(jí)特大地震影響阿纤,放射性物質(zhì)發(fā)生泄漏句灌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一欠拾、第九天 我趴在偏房一處隱蔽的房頂上張望胰锌。 院中可真熱鬧,春花似錦藐窄、人聲如沸资昧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽格带。三九已至,卻和暖如春刹枉,著一層夾襖步出監(jiān)牢的瞬間践惑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國打工嘶卧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留尔觉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓芥吟,卻偏偏與公主長(zhǎng)得像侦铜,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钟鸵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

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