import 'package:flutter/material.dart';

enum DottedLineDirection { horizontal, vertical }

class DottedLine extends StatelessWidget {
  final double width;
  final double height;
  final double strokeWidth;
  final DottedLineDirection direction;
  final Color color;

  DottedLine({
    required this.width,
    required this.height,
    this.strokeWidth = 2.0,
    this.color = Colors.black,
    this.direction = DottedLineDirection.horizontal,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: width,
      height: height,
      child: CustomPaint(
        painter: DottedLinePainter(
            direction: direction, color: color, strokeWidth: strokeWidth),
        size: Size(width, height),
      ),
    );
  }
}

class DottedLinePainter extends CustomPainter {
  final DottedLineDirection direction;
  final Color color;
  final double strokeWidth;

  DottedLinePainter(
      {required this.direction,
      required this.color,
      required this.strokeWidth});

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..shader = null
      ..blendMode = BlendMode.srcOver;

    final dashWidth = 5;
    final dashSpace = 5;
    final path = Path();
    double startX = 0;
    double startY = 0;

    while (direction == DottedLineDirection.horizontal && startX < size.width) {
      path.moveTo(startX, 0);
      path.lineTo(startX + dashWidth, 0);
      startX += dashWidth + dashSpace;
    }

    while (direction == DottedLineDirection.vertical && startY < size.height) {
      path.moveTo(0, startY);
      path.lineTo(0, startY + dashWidth);
      startY += dashWidth + dashSpace;
    }

    canvas.drawPath(path, paint);
  }

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