import 'package:flutter/material.dart';

/// Widget untuk garis putus-putus ala struk toko
class DottedLine extends StatelessWidget {
  final Axis axis;
  final double length;
  final double dashLength;
  final double gapLength;
  final Color color;
  final double strokeWidth;

  const DottedLine({
    super.key,
    this.axis = Axis.horizontal,
    this.length = double.infinity,
    this.dashLength = 4.0,
    this.gapLength = 4.0,
    this.color = Colors.black,
    this.strokeWidth = 1.0,
  });

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      size: axis == Axis.horizontal
          ? Size(length, strokeWidth)
          : Size(strokeWidth, length),
      painter: _DottedLinePainter(
        dashLength: dashLength,
        gapLength: gapLength,
        color: color,
        strokeWidth: strokeWidth,
        axis: axis,
      ),
    );
  }
}

class _DottedLinePainter extends CustomPainter {
  final double dashLength;
  final double gapLength;
  final Color color;
  final double strokeWidth;
  final Axis axis;

  _DottedLinePainter({
    required this.dashLength,
    required this.gapLength,
    required this.color,
    required this.strokeWidth,
    required this.axis,
  });

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

    final path = Path();
    if (axis == Axis.horizontal) {
      double startX = 0;
      while (startX < size.width) {
        path.moveTo(startX, size.height / 2);
        path.lineTo(startX + dashLength, size.height / 2);
        startX += dashLength + gapLength;
      }
    } else {
      double startY = 0;
      while (startY < size.height) {
        path.moveTo(size.width / 2, startY);
        path.lineTo(size.width / 2, startY + dashLength);
        startY += dashLength + gapLength;
      }
    }

    canvas.drawPath(path, paint);
  }

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