getDashedLinePath method

Path getDashedLinePath(
  1. Offset point1,
  2. Offset point2,
  3. double scale,
  4. double dashLength,
  5. double dashSpace,
)

Implementation

Path getDashedLinePath(
  Offset point1,
  Offset point2,
  double scale,
  double dashLength,
  double dashSpace,
) {
  Path path = Path();

  Offset normalized = VectorUtils.normalizeVector(
    VectorUtils.getDirectionVector(point1, point2),
  );
  double lineDistance = (point2 - point1).distance;
  Offset currentPoint = Offset(point1.dx, point1.dy);

  double dash = dashLength * scale;
  double space = dashSpace * scale;
  double currentDistance = 0;
  while (currentDistance < lineDistance) {
    path.moveTo(currentPoint.dx, currentPoint.dy);
    currentPoint = currentPoint + normalized * dash;

    if (currentDistance + dash > lineDistance) {
      path.lineTo(point2.dx, point2.dy);
    } else {
      path.lineTo(currentPoint.dx, currentPoint.dy);
    }
    currentPoint = currentPoint + normalized * space;

    currentDistance += dash + space;
  }

  path.moveTo(
    point2.dx - normalized.dx * lineWidth * scale,
    point2.dy - normalized.dy * lineWidth * scale,
  );
  path.lineTo(point2.dx, point2.dy);
  return path;
}