BouncingScrollSimulation constructor

BouncingScrollSimulation(
  1. {required double position,
  2. required double velocity,
  3. required double leadingExtent,
  4. required double trailingExtent,
  5. required SpringDescription spring,
  6. double constantDeceleration = 0,
  7. Tolerance tolerance = Tolerance.defaultTolerance}
)

Creates a simulation group for scrolling on iOS, with the given parameters.

The position and velocity arguments must use the same units as will be expected from the x and dx methods respectively (typically logical pixels and logical pixels per second respectively).

The leading and trailing extents must use the unit of length, the same unit as used for the position argument and as expected from the x method (typically logical pixels).

The units used with the provided SpringDescription must similarly be consistent with the other arguments. A default set of constants is used for the spring description if it is omitted; these defaults assume that the unit of length is the logical pixel.

Implementation

BouncingScrollSimulation({
  required double position,
  required double velocity,
  required this.leadingExtent,
  required this.trailingExtent,
  required this.spring,
  double constantDeceleration = 0,
  super.tolerance,
}) : assert(leadingExtent <= trailingExtent) {
  if (position < leadingExtent) {
    _springSimulation = _underscrollSimulation(position, velocity);
    _springTime = double.negativeInfinity;
  } else if (position > trailingExtent) {
    _springSimulation = _overscrollSimulation(position, velocity);
    _springTime = double.negativeInfinity;
  } else {
    // Taken from UIScrollView.decelerationRate (.normal = 0.998)
    // 0.998^1000 = ~0.135
    _frictionSimulation = FrictionSimulation(0.135, position, velocity, constantDeceleration: constantDeceleration);
    final double finalX = _frictionSimulation.finalX;
    if (velocity > 0.0 && finalX > trailingExtent) {
      _springTime = _frictionSimulation.timeAtX(trailingExtent);
      _springSimulation = _overscrollSimulation(
        trailingExtent,
        math.min(_frictionSimulation.dx(_springTime), maxSpringTransferVelocity),
      );
      assert(_springTime.isFinite);
    } else if (velocity < 0.0 && finalX < leadingExtent) {
      _springTime = _frictionSimulation.timeAtX(leadingExtent);
      _springSimulation = _underscrollSimulation(
        leadingExtent,
        math.min(_frictionSimulation.dx(_springTime), maxSpringTransferVelocity),
      );
      assert(_springTime.isFinite);
    } else {
      _springTime = double.infinity;
    }
  }
}