determineLinkSegmentIndex method

int? determineLinkSegmentIndex(
  1. Offset position,
  2. Offset canvasPosition,
  3. double canvasScale
)

If a link is compound from more than one segments this returns an index of the link segment, which was tapped on.

Segments are indexed from 1. If there is no link segment on the tap location it returns null. It should take a localPosition from a onLinkTap function or similar.

Implementation

int? determineLinkSegmentIndex(
  Offset position,
  Offset canvasPosition,
  double canvasScale,
) {
  for (int i = 0; i < linkPoints.length - 1; i++) {
    var point1 = linkPoints[i] * canvasScale + canvasPosition;
    var point2 = linkPoints[i + 1] * canvasScale + canvasPosition;

    Path rect = VectorUtils.getRectAroundLine(
      point1,
      point2,
      canvasScale * (linkStyle.lineWidth + 5),
    );

    if (rect.contains(position)) {
      return i + 1;
    }
  }
  return null;
}