scrollToFind method

Future<SemanticsNode?> scrollToFind({
  1. required String label,
  2. int maxScrolls = _maxScrollAttempts,
})

Scroll to find an element with the given label.

Tries scrolling down first (most common), then scrolling up if not found. Returns the SemanticsNode if found, null otherwise.

Implementation

Future<SemanticsNode?> scrollToFind({
  required String label,
  int maxScrolls = _maxScrollAttempts,
}) async {
  // First, find the nearest scrollable on screen.
  final context = _walker.captureScreenContext();
  final scrollable = context.firstScrollable;
  if (scrollable == null) return null;

  final scrollableNode = _walker.findNodeById(scrollable.nodeId);
  if (scrollableNode == null) return null;
  final scrollableNodeId = scrollableNode.id;

  // Try scrolling down.
  final downResult = await _scrollAndSearch(
    label: label,
    scrollableNodeId: scrollableNodeId,
    action: SemanticsAction.scrollDown,
    maxScrolls: maxScrolls ~/ 2,
  );
  if (downResult != null) return downResult;

  // Scroll back up to where we started, then try scrolling up.
  final upResult = await _scrollAndSearch(
    label: label,
    scrollableNodeId: scrollableNodeId,
    action: SemanticsAction.scrollUp,
    maxScrolls: maxScrolls ~/ 2,
  );
  return upResult;
}