setPixels method

double setPixels (double newPixels)

Update the scroll position (pixels) to a given pixel value.

This should only be called by the current ScrollActivity, either during the transient callback phase or in response to user input.

Returns the overscroll, if any. If the return value is 0.0, that means that pixels now returns the given value. If the return value is positive, then pixels is less than the requested value by the given amount (overscroll past the max extent), and if it is negative, it is greater than the requested value by the given amount (underscroll past the min extent).

The amount of overscroll is computed by applyBoundaryConditions.

The amount of the change that is applied is reported using didUpdateScrollPositionBy. If there is any overscroll, it is reported using didOverscrollBy.

Implementation

double setPixels(double newPixels) {
  assert(_pixels != null);
  assert(SchedulerBinding.instance.schedulerPhase.index <= SchedulerPhase.transientCallbacks.index);
  if (newPixels != pixels) {
    final double overscroll = applyBoundaryConditions(newPixels);
    assert(() {
      final double delta = newPixels - pixels;
      if (overscroll.abs() > delta.abs()) {
        throw FlutterError(
          '$runtimeType.applyBoundaryConditions returned invalid overscroll value.\n'
          'setPixels() was called to change the scroll offset from $pixels to $newPixels.\n'
          'That is a delta of $delta units.\n'
          '$runtimeType.applyBoundaryConditions reported an overscroll of $overscroll units.'
        );
      }
      return true;
    }());
    final double oldPixels = _pixels;
    _pixels = newPixels - overscroll;
    if (_pixels != oldPixels) {
      notifyListeners();
      didUpdateScrollPositionBy(_pixels - oldPixels);
    }
    if (overscroll != 0.0) {
      didOverscrollBy(overscroll);
      return overscroll;
    }
  }
  return 0.0;
}