dashPath function

Path dashPath(
  1. Path path,
  2. double length, [
  3. double? gap,
  4. double? distance = 0,
])

Converts a path to a dashed Path with a given length and gap. The distance parameter can be used to offset the dash pattern.

Implementation

Path dashPath(final Path path, double length,
    [double? gap, double? distance = 0]) {
  gap ??= length;
  PathMetrics pathMetrics = path.computeMetrics();
  Path dest = Path();
  for (var metric in pathMetrics) {
    bool draw = true;
    while (distance! < metric.length) {
      if (draw) {
        dest.addPath(
          metric.extractPath(distance, distance + length),
          Offset.zero,
        );
        distance += length;
      } else {
        distance += gap;
      }
      draw = !draw;
    }
  }
  return dest;
}