calculateCacheOffset method

double calculateCacheOffset (SliverConstraints constraints, { @required double from, @required double to })

Computes the portion of the region from from to to that is within the cache extent of the viewport, assuming that only the region from the SliverConstraints.cacheOrigin that is SliverConstraints.remainingCacheExtent high is visible, and that the relationship between scroll offsets and paint offsets is linear.

This method is not useful if there is not a 1:1 relationship between consumed scroll offset and consumed cache extent.

Implementation

double calculateCacheOffset(SliverConstraints constraints, { @required double from, @required double to }) {
  assert(from <= to);
  final double a = constraints.scrollOffset + constraints.cacheOrigin;
  final double b = constraints.scrollOffset + constraints.remainingCacheExtent;
  // the clamp on the next line is to avoid floating point rounding errors
  return (to.clamp(a, b) - from.clamp(a, b)).clamp(0.0, constraints.remainingCacheExtent);
}