drawGridLine function

void drawGridLine(
  1. Canvas canvas,
  2. Offset start,
  3. Offset end,
  4. Paint paint,
  5. List<double>? dashPattern,
)

Helper to draw a line (solid or dashed) on a canvas.

Implementation

void drawGridLine(
  Canvas canvas,
  Offset start,
  Offset end,
  Paint paint,
  List<double>? dashPattern,
) {
  if (dashPattern == null || dashPattern.isEmpty) {
    canvas.drawLine(start, end, paint);
  } else {
    final dx = end.dx - start.dx;
    final dy = end.dy - start.dy;
    final distance = math.sqrt(dx * dx + dy * dy);

    if (distance == 0) return;

    final unitDx = dx / distance;
    final unitDy = dy / distance;

    var current = 0.0;
    var dashIndex = 0;
    var isDrawing = true;

    while (current < distance) {
      final dashLength = dashPattern[dashIndex % dashPattern.length];
      final nextCurrent = (current + dashLength).clamp(0.0, distance);

      if (isDrawing) {
        canvas.drawLine(
          Offset(start.dx + unitDx * current, start.dy + unitDy * current),
          Offset(
              start.dx + unitDx * nextCurrent, start.dy + unitDy * nextCurrent),
          paint,
        );
      }

      current = nextCurrent;
      dashIndex++;
      isDrawing = !isDrawing;
    }
  }
}