getMaxChildIndexForScrollOffset method

  1. @visibleForTesting
  2. @protected
int getMaxChildIndexForScrollOffset(
  1. double scrollOffset,
  2. @Deprecated('The itemExtent is already available within the scope of this function. ' 'This feature was deprecated after v3.20.0-7.0.pre.') 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 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

@visibleForTesting
@protected
int getMaxChildIndexForScrollOffset(
  double scrollOffset,
  @Deprecated(
    'The itemExtent is already available within the scope of this function. '
    'This feature was deprecated after v3.20.0-7.0.pre.'
  )
  double itemExtent,
) {
  if (itemExtentBuilder == null) {
    itemExtent = this.itemExtent!;
    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!);
  }
}