Flutter 顶部TabBar

2021-01-14 10:36:14 浏览数 (1)

截屏2021-01-12 16.21.55.png

需求是这样的,本来都想自定义TabBar,没有想到TabBar原生的就支持这个方式,只需要设置以下式样就能达到效果了

代码语言:javascript复制
unselectedLabelStyle: TextStyle(fontSize: 16), //未选中样式
labelStyle: TextStyle( fontSize: 24, height: 2), //选中样式
isScrollable: true, // 是否可以滑动
labelPadding: EdgeInsets.only(left: 16,top: 15, right: 10),  //文本偏移

完整代码

代码语言:javascript复制
@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, // 是否可以滑动
      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(),
    );
  }
代码语言:javascript复制
//自定义指示器--实现方式:复制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;
    //取中间坐标
    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);
    //设置为圆角
    final Paint paint = borderSide.toPaint()..strokeCap = StrokeCap.round;
    canvas.drawLine(indicator.bottomLeft, indicator.bottomRight, paint);
  }
}

截屏2021-01-12 16.27.20.png

0 人点赞