hitTestAt method

List<HitTestElementEntry> hitTestAt(
  1. double x,
  2. double y
)

Hit-tests the render tree at the given terminal coordinates and returns a list of HitTestElementEntry objects, deepest first.

Each entry contains the Element whose render object was hit and the local coordinates within that render object.

Returns an empty list if nothing was hit (coordinates outside root).

Implementation

List<HitTestElementEntry> hitTestAt(double x, double y) {
  // Find the root render object.
  final rootRO = _findRootRenderObject(_root);
  if (rootRO == null) return const [];

  final result = HitTestResult();
  rootRO.hitTest(result, localX: x, localY: y);

  if (result.isEmpty) return const [];

  // Map render objects back to elements.
  final entries = <HitTestElementEntry>[];
  for (final hit in result.path) {
    final ro = hit.renderObject;
    if (ro is RenderObject && ro.element is Element) {
      entries.add(
        HitTestElementEntry(
          element: ro.element! as Element,
          localX: hit.localX,
          localY: hit.localY,
        ),
      );
    }
  }
  return entries;
}