flutter tabbar设置indicator高度、宽度

dart 复制代码
import 'package:flutter/material.dart';

class MyCustomIndicator extends Decoration {

  final double indWidth;
  final double indHeight;
  final double radius;

  MyCustomIndicator({this.indWidth = 70.0, this.indHeight = 12.0, this.radius = 5});

  @override
  BoxPainter createBoxPainter([VoidCallback onChanged]) {
    return _CustomBoxPainter(this, onChanged, indWidth, indHeight, radius);
  }
}

class _CustomBoxPainter extends BoxPainter {
  final MyCustomIndicator decoration;
  final double indWidth;
  final double indHeight;
  final double radius;

  _CustomBoxPainter(this.decoration, VoidCallback onChanged, this.indWidth, this.indHeight, this.radius)
      : super(onChanged);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    final size = configuration.size;
    final newOffset = Offset(offset.dx + (size.width - indWidth) / 2, size.height - indHeight);
    final Rect rect = newOffset & Size(indWidth, indHeight);
    final Paint paint = Paint();
    paint.color = Colors.yellow;
    paint.style = PaintingStyle.fill;
    canvas.drawRRect(
      RRect.fromRectAndRadius(rect, Radius.circular(radius)), // 圆角半径
      paint,
    );
  }
}

使用

dart 复制代码
TabBar(
      isScrollable: false,
      labelPadding: EdgeInsets.symmetric(horizontal: 0),
      indicator: MyCustomIndicator(),
      labelColor: Color(0xff333333),
      labelStyle: TextStyle(
        fontSize: 30.sp,
        fontWeight: FontWeight.w600,
      ),
      unselectedLabelColor: JadeColors.grey,
      unselectedLabelStyle: TextStyle(
        fontSize: 30.sp,
      ),
      indicatorSize: TabBarIndicatorSize.label,
      controller: _tabController,
      tabs: _tabs
          .map((value) => Container(padding: EdgeInsets.symmetric(horizontal: 20.w),child: Text(value))).toList(),
      onTap: (index) {},
    )
相关推荐
Dragon Wu2 分钟前
React Native KeyChain完整封装
前端·javascript·react native·react.js·前端框架
晚霞的不甘5 分钟前
Flutter for OpenHarmony 布局探秘:从理论到实战构建交互式组件讲解应用
开发语言·前端·flutter·正则表达式·前端框架·firefox·鸿蒙
芙莉莲教你写代码6 分钟前
Flutter 框架跨平台鸿蒙开发 - 附近自助洗车点查询应用开发教程
flutter·华为·harmonyos
爱吃大芒果7 分钟前
Flutter for OpenHarmony核心组件学习: MaterialApp、Scaffold 两大基础组件以及有无状态组件
开发语言·学习·flutter
灰灰勇闯IT9 分钟前
Flutter for OpenHarmony:用 StatefulWidget 实现基础用户交互
flutter·microsoft·交互
运筹vivo@9 分钟前
BUUCTF: [极客大挑战 2019]BabySQL
前端·web安全·php·ctf
Beginner x_u2 小时前
前端八股文 Vue下
前端·vue.js·状态模式
时光慢煮3 小时前
打造跨端博客分类与标签组件:Flutter × OpenHarmony 实战
flutter·开源·openharmony
提笔了无痕9 小时前
Web中Token验证如何实现(go语言)
前端·go·json·restful
戌中横9 小时前
JavaScript——Web APIs DOM
前端·javascript·html