createBallisticSimulation method Null safety

  1. @override
Simulation? createBallisticSimulation(
  1. ScrollMetrics position,
  2. double velocity
)
override

Returns a simulation for ballistic scrolling starting from the given position with the given velocity.

This is used by ScrollPositionWithSingleContext in the ScrollPositionWithSingleContext.goBallistic method. If the result is non-null, ScrollPositionWithSingleContext will begin a BallisticScrollActivity with the returned value. Otherwise, it will begin an idle activity instead.

The given position is only valid during this method call. Do not keep a reference to it to use later, as the values may update, may not update, or may update to reflect an entirely unrelated scrollable.

Implementation

@override
Simulation? createBallisticSimulation(ScrollMetrics position, double velocity) {
  final Tolerance tolerance = this.tolerance;
  if (position.outOfRange) {
    double? end;
    if (position.pixels > position.maxScrollExtent) {
      end = position.maxScrollExtent;
    }
    if (position.pixels < position.minScrollExtent) {
      end = position.minScrollExtent;
    }
    assert(end != null);
    return ScrollSpringSimulation(
      spring,
      position.pixels,
      end!,
      math.min(0.0, velocity),
      tolerance: tolerance,
    );
  }
  if (velocity.abs() < tolerance.velocity) {
    return null;
  }
  if (velocity > 0.0 && position.pixels >= position.maxScrollExtent) {
    return null;
  }
  if (velocity < 0.0 && position.pixels <= position.minScrollExtent) {
    return null;
  }
  return ClampingScrollSimulation(
    position: position.pixels,
    velocity: velocity,
    tolerance: tolerance,
  );
}