calculatePaintOffset method

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

Computes the portion of the region from from to to that is visible, assuming that only the region from the SliverConstraints.scrollOffset that is SliverConstraints.remainingPaintExtent high is visible, and that the relationship between scroll offsets and paint offsets is linear.

For example, if the constraints have a scroll offset of 100 and a remaining paint extent of 100, and the arguments to this method describe the region 50..150, then the returned value would be 50 (from scroll offset 100 to scroll offset 150).

This method is not useful if there is not a 1:1 relationship between consumed scroll offset and consumed paint extent. For example, if the sliver always paints the same amount but consumes a scroll offset extent that is proportional to the SliverConstraints.scrollOffset, then this function's results will not be consistent.

Implementation

// This could be a static method but isn't, because it would be less convenient
// to call it from subclasses if it was.
double calculatePaintOffset(SliverConstraints constraints, { @required double from, @required double to }) {
  assert(from <= to);
  final double a = constraints.scrollOffset;
  final double b = constraints.scrollOffset + constraints.remainingPaintExtent;
  // 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.remainingPaintExtent);
}