scrollUntilVisible method

Future<PatrolFinder> scrollUntilVisible({
  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. Alignment alignment = Alignment.center,
  10. bool enablePatrolLog = true,
})
inherited

Scrolls view in scrollDirection until this finders finds at least one existing widget.

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

This is a reimplementation of WidgetController.scrollUntilVisible that doesn't throw when finder finds more than one widget.

See also:

  • PatrolTester.scrollUntilExists.

Implementation

Future<PatrolFinder> scrollUntilVisible({
  required Finder finder,
  Finder? view,
  double delta = defaultScrollDelta,
  AxisDirection? scrollDirection,
  int maxScrolls = defaultScrollMaxIteration,
  Duration? settleBetweenScrollsTimeout,
  Duration? dragDuration,
  SettlePolicy? settlePolicy,
  Alignment alignment = Alignment.center,
  bool enablePatrolLog = true,
}) {
  assert(maxScrolls > 0, 'maxScrolls must be positive number');
  return wrapWithPatrolLog(
    action: 'scrollUntilVisible',
    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 {
        Offset moveStep;
        switch (direction) {
          case AxisDirection.up:
            moveStep = Offset(0, delta);
          case AxisDirection.down:
            moveStep = Offset(0, -delta);
          case AxisDirection.left:
            moveStep = Offset(delta, 0);
          case AxisDirection.right:
            moveStep = Offset(-delta, 0);
        }

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

        return resolvedFinder;
      });
    },
  );
}