firstVisuallyActionableElement method

Element? firstVisuallyActionableElement(
  1. Finder finder, {
  2. bool requireHitTestable = false,
})

First matching element that is on the current route and visually actionable.

Used by screenshot highlights so we never annotate off-route / covered widgets while another screen is painted.

Implementation

Element? firstVisuallyActionableElement(
  Finder finder, {
  bool requireHitTestable = false,
}) {
  final roots = finder.evaluate().toList();
  for (final root in roots) {
    final candidates = <Element>[root];
    void collect(Element element) {
      element.visitChildren((child) {
        candidates.add(child);
        collect(child);
      });
    }

    // Test IDs are often attached to KeyedSubtree/Builder wrappers, which
    // have no RenderObject of their own. Resolve visibility and hit testing
    // against the actual rendered descendants so screenshot proof and action
    // dispatch can identify the same on-screen control.
    collect(root);
    for (final element in candidates) {
      if (!isElementVisuallyActionable(element)) continue;
      if (requireHitTestable) {
        final exactElement = find.byElementPredicate(
          (candidate) => identical(candidate, element),
          skipOffstage: false,
        );
        if (exactElement.hitTestable().evaluate().isEmpty) continue;
      }
      return element;
    }
  }
  return null;
}