startOrUpdateAutoScroll method

void startOrUpdateAutoScroll({
  1. required AxisDirection direction,
  2. required double pointerDistance,
  3. double? maxToScroll,
})

Start or update a AutoScrollActivity in the given AxisDirection until maxToScroll pixels have been scrolled with a velocity calculated from the pointerDistance.

If maxToScroll is not specified it scrolls until it reaches the end of the scrollable area.

Implementation

void startOrUpdateAutoScroll({
  required AxisDirection direction,
  required double pointerDistance,
  double? maxToScroll,
}) {
  final axis = axisDirectionToAxis(direction);
  final controller = getScrollControllerFor(axis: axis)!;
  final autoScrollController = getAutoScrollControllerFor(axis: axis);
  final position = controller.position as ScrollPositionWithSingleContext;

  if (autoScrollController != null) {
    autoScrollController.pointerDistance = pointerDistance;
    return;
  }

  GrowthDirection growthDirection;
  if (direction == AxisDirection.right || direction == AxisDirection.down) {
    growthDirection = GrowthDirection.forward;
  } else {
    growthDirection = GrowthDirection.reverse;
  }

  final newAutoScrollController = AutoScrollController(
    pointerDistance: pointerDistance,
  );
  if (axis == Axis.vertical) {
    verticalAutoScrollController = newAutoScrollController;
  } else {
    horizontalAutoScrollController = newAutoScrollController;
  }

  position.beginActivity(
    AutoScrollActivity(
      delegate: position,
      position: position,
      direction: growthDirection,
      controller: newAutoScrollController,
      maxToScroll: maxToScroll,
    ),
  );
}