// Author: fengshengxiong
// Date: 2024/6/4
// Description: 自定义TabBar指示器

import 'package:flutter/material.dart';

class MyCustomIndicator extends Decoration {
  final double indWidth;
  final double indHeight;
  final Color color;
  final double radius;

  const MyCustomIndicator({
    this.indWidth = 16.0,
    this.indHeight = 4.0,
    this.color = Colors.white,
    this.radius = 3.5,
  });

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

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

  _CustomBoxPainter(
    this.decoration,
    VoidCallback? onChanged,
    this.indWidth,
    this.indHeight,
    this.color,
    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 = color;
    paint.style = PaintingStyle.fill;
    canvas.drawRRect(
      RRect.fromRectAndRadius(rect, Radius.circular(radius)),
      paint,
    );
  }
}
