import 'package:flutter/material.dart';

class GradientLine extends StatelessWidget {
  final double height;
  final Color color;

  const GradientLine({
    Key? key,
    this.height = 4.0,
    required this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return CustomPaint(
          size: Size(constraints.maxWidth, height),
          painter: _GradientLinePainter(color: color),
        );
      },
    );
  }
}

class _GradientLinePainter extends CustomPainter {
  final Color color;

  _GradientLinePainter({required this.color});

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..shader = LinearGradient(
        colors: [
          color.withOpacity(0.0), // 左端透明
          color.withOpacity(1.0), // 中间最深
          color.withOpacity(0.0), // 右端透明
        ],
        stops: [0.0, 0.5, 1.0],
      ).createShader(Rect.fromLTWH(0, 0, size.width, size.height));

    canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
