isPointOnPath function

bool isPointOnPath(
  1. Offset point,
  2. Path path,
  3. double pathWidth
)

Checks if the given point lies on the specified path. Returns true if the point is on the path, otherwise returns false.

Implementation

bool isPointOnPath(Offset point, Path path, double pathWidth) {
  PathMetric? pathMetric = path.computeMetrics().firstOrNull;
  if (pathMetric == null) return false;
  double totalLength = pathMetric.length;
  double strokeWidth = pathWidth.clamp(4, 100);

  for (double d = 0.0; d <= totalLength; d += 1) {
    Tangent? tangent = pathMetric.getTangentForOffset(d);
    if (tangent == null) continue;

    // Check if the point is within stroke the width of the path
    if ((tangent.position - point).distance <= strokeWidth) {
      return true;
    }
  }

  return false;
}