getMaxChildIndexForScrollOffset method

  1. @protected
int getMaxChildIndexForScrollOffset(
  1. double scrollOffset,
  2. double itemExtent
)

The maximum child index that is visible at the given scroll offset.

This function uses the returned value of itemExtentBuilder or the itemExtent as an argument to avoid recomputing item size repeatedly during layout.

By default, returns a value consistent with the children being placed in order, without gaps, starting from layout offset zero.

Implementation

@protected
int getMaxChildIndexForScrollOffset(double scrollOffset, double itemExtent) {
  if (itemExtentBuilder == null) {
    if (itemExtent > 0.0) {
      final double actual = scrollOffset / itemExtent - 1;
      final int round = actual.round();
      if ((actual * itemExtent - round * itemExtent).abs() < precisionErrorTolerance) {
        return math.max(0, round);
      }
      return math.max(0, actual.ceil());
    }
    return 0;
  } else {
    return _getChildIndexForScrollOffset(scrollOffset, itemExtentBuilder!);
  }
}