scrollUntilExists method

Future<PatrolFinder> scrollUntilExists({
  1. required Finder finder,
  2. Finder? view,
  3. double delta = defaultScrollDelta,
  4. AxisDirection? scrollDirection,
  5. int maxScrolls = defaultScrollMaxIteration,
  6. Duration? settleBetweenScrollsTimeout,
  7. Duration? dragDuration,
  8. SettlePolicy? settlePolicy,
  9. bool enablePatrolLog = true,
})
inherited

Scrolls view in its scrolling direction until this finders finds at least one existing widget.

If view is null, it defaults to the first found Scrollable.

See also:

  • PatrolTester.scrollUntilVisible.

Implementation

Future<PatrolFinder> scrollUntilExists({
  required Finder finder,
  Finder? view,
  double delta = defaultScrollDelta,
  AxisDirection? scrollDirection,
  int maxScrolls = defaultScrollMaxIteration,
  Duration? settleBetweenScrollsTimeout,
  Duration? dragDuration,
  SettlePolicy? settlePolicy,
  bool enablePatrolLog = true,
}) {
  assert(maxScrolls > 0, 'maxScrolls must be positive number');
  return wrapWithPatrolLog<PatrolFinder>(
    action: 'scrollUntilExists',
    finder: view,
    color: AnsiCodes.green,
    enablePatrolLog: enablePatrolLog,
    function: () async {
      final finderView = view ?? find.byType(Scrollable);

      final scrollablePatrolFinder = await PatrolFinder(
        finder: finderView,
        tester: this,
      ).waitUntilVisible(enablePatrolLog: false);

      AxisDirection direction;
      if (scrollDirection == null) {
        if (finderView.evaluate().first.widget is Scrollable) {
          direction = tester
              .firstWidget<Scrollable>(finderView)
              .axisDirection;
        } else {
          direction = AxisDirection.down;
        }
      } else {
        direction = scrollDirection;
      }

      return TestAsyncUtils.guard<PatrolFinder>(() async {
        final moveStep = switch (direction) {
          AxisDirection.up => Offset(0, delta),
          AxisDirection.down => Offset(0, -delta),
          AxisDirection.left => Offset(delta, 0),
          AxisDirection.right => Offset(-delta, 0),
        };

        final resolvedFinder = await dragUntilExists(
          finder: finder,
          view: scrollablePatrolFinder.first,
          moveStep: moveStep,
          maxIteration: maxScrolls,
          settleBetweenScrollsTimeout: settleBetweenScrollsTimeout,
          dragDuration: dragDuration,
          settlePolicy: settlePolicy,
          enablePatrolLog: false,
        );

        return resolvedFinder;
      });
    },
  );
}