getDashedPath method

Path getDashedPath({
  1. required Point<double> a,
  2. required Point<double> b,
  3. required dynamic gap,
})

Implementation

Path getDashedPath({
  required math.Point<double> a,
  required math.Point<double> b,
  required gap,
}) {
  Size size = Size(b.x - a.x, b.y - a.y);
  Path path = Path();
  path.moveTo(a.x, a.y);
  bool shouldDraw = true;
  math.Point currentPoint = math.Point(a.x, a.y);

  num radians = math.atan(size.height / size.width);

  num dx = math.cos(radians) * gap < 0
      ? math.cos(radians) * gap * -1
      : math.cos(radians) * gap;

  num dy = math.sin(radians) * gap < 0
      ? math.sin(radians) * gap * -1
      : math.sin(radians) * gap;

  while (currentPoint.x <= b.x && currentPoint.y <= b.y) {
    shouldDraw
        ? path.lineTo(currentPoint.x as double, currentPoint.y as double)
        : path.moveTo(currentPoint.x as double, currentPoint.y as double);
    shouldDraw = !shouldDraw;
    currentPoint = math.Point(
      currentPoint.x + dx,
      currentPoint.y + dy,
    );
  }
  return path;
}