import 'package:flutter/material.dart';

class VerticalDashedLine extends StatelessWidget {
  final double height; // 虚线总高度
  final Color color; // 颜色
  final double strokeWidth; // 线条粗细
  final double dashLength; // 虚线线段长度
  final double dashSpace; // 虚线间隔长度

  const VerticalDashedLine({
    this.height = 100,
    this.color = Colors.grey,
    this.strokeWidth = 1,
    this.dashLength = 5,
    this.dashSpace = 3,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: Size(strokeWidth, height), // 宽度为线条粗细，高度为总高度
      painter: _VerticalDashedLinePainter(
        color: color,
        strokeWidth: strokeWidth,
        dashLength: dashLength,
        dashSpace: dashSpace,
      ),
    );
  }
}

class _VerticalDashedLinePainter extends CustomPainter {
  final Color color;
  final double strokeWidth;
  final double dashLength;
  final double dashSpace;

  _VerticalDashedLinePainter({
    required this.color,
    required this.strokeWidth,
    required this.dashLength,
    required this.dashSpace,
  });

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    double startY = 0;
    while (startY < size.height) {
      // 绘制垂直方向的虚线线段
      canvas.drawLine(
        Offset(0, startY), // 起点 (x=0, y=startY)
        Offset(0, startY + dashLength), // 终点 (x=0, y=startY+dashLength)
        paint,
      );
      startY += dashLength + dashSpace;
    }
  }

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