hitTest method

bool hitTest(
  1. HitTestResult result, {
  2. required double localX,
  3. required double localY,
})

Performs hit-testing at (localX, localY) in this object's coordinate space. Adds matching entries to result, deepest first.

Returns true if this object or a descendant was hit.

Implementation

bool hitTest(
  HitTestResult result, {
  required double localX,
  required double localY,
}) {
  if (localX < 0 ||
      localY < 0 ||
      localX >= size.width ||
      localY >= size.height) {
    return false;
  }

  // Test children in reverse order (last painted = on top).
  for (var i = children.length - 1; i >= 0; i--) {
    final child = children[i];
    final childX = localX - child.offset.dx;
    final childY = localY - child.offset.dy;
    if (child.hitTest(result, localX: childX, localY: childY)) {
      break; // deepest hit found via this child
    }
  }

  // Add ourselves — callers walk the path from deepest to shallowest.
  result.add(HitTestEntry(this, localX: localX, localY: localY));
  return true;
}