84 lines
2.4 KiB
Dart
84 lines
2.4 KiB
Dart
// 自定义固定宽度指示器(带圆角),支持渐变色
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
class ATFixedWidthTabIndicator extends Decoration {
|
|
final double width;
|
|
final Color? color; // 改为可选参数
|
|
final Gradient? gradient; // 新增渐变参数
|
|
final double height;
|
|
final double borderRadius;
|
|
|
|
const ATFixedWidthTabIndicator({
|
|
required this.width,
|
|
this.color, // 颜色和渐变至少提供一个
|
|
this.gradient,
|
|
this.height = 3.0,
|
|
this.borderRadius = 2.0,
|
|
}) : assert(color != null || gradient != null,
|
|
'必须提供color或gradient参数');
|
|
|
|
@override
|
|
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
|
|
return _FixedWidthTabIndicatorPainter(
|
|
width: width,
|
|
color: color,
|
|
gradient: gradient,
|
|
height: height,
|
|
borderRadius: borderRadius,
|
|
onChange: onChanged,
|
|
);
|
|
}
|
|
}
|
|
|
|
class _FixedWidthTabIndicatorPainter extends BoxPainter {
|
|
final double width;
|
|
final Color? color;
|
|
final Gradient? gradient;
|
|
final double height;
|
|
final double borderRadius;
|
|
|
|
_FixedWidthTabIndicatorPainter({
|
|
required this.width,
|
|
required this.color,
|
|
required this.gradient,
|
|
required this.height,
|
|
required this.borderRadius,
|
|
VoidCallback? onChange,
|
|
}) : assert(color != null || gradient != null),
|
|
super(onChange);
|
|
|
|
@override
|
|
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
|
|
final Paint paint = Paint()..style = PaintingStyle.fill;
|
|
|
|
// 设置颜色或渐变
|
|
if (gradient != null) {
|
|
final Rect rect = offset & configuration.size!;
|
|
final double left = rect.left + (rect.width - width) / 2;
|
|
final double top = rect.bottom - height;
|
|
final Rect indicatorRect = Rect.fromLTWH(left, top, width, height);
|
|
|
|
// 创建渐变着色器
|
|
paint.shader = gradient!.createShader(indicatorRect);
|
|
} else {
|
|
paint.color = color!;
|
|
}
|
|
|
|
final Rect rect = offset & configuration.size!;
|
|
|
|
// 计算固定宽度的指示器位置(居中)
|
|
final double left = rect.left + (rect.width - width) / 2;
|
|
final double top = rect.bottom - height;
|
|
|
|
final Rect indicatorRect = Rect.fromLTWH(left, top, width, height);
|
|
|
|
// 创建圆角矩形
|
|
final RRect roundedRect = RRect.fromRectAndRadius(
|
|
indicatorRect,
|
|
Radius.circular(borderRadius),
|
|
);
|
|
|
|
// 绘制圆角矩形
|
|
canvas.drawRRect(roundedRect, paint);
|
|
}
|
|
} |