indexToLayoutOffset method

  1. @override
double indexToLayoutOffset(
  1. double itemExtent,
  2. int index
)
override

The layout offset for the child with the given index.

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

By default, places the children in order, without gaps, starting from layout offset zero.

Implementation

@override
double indexToLayoutOffset(double itemExtent, int index) {
  // itemExtent is deprecated in the super class, we ignore it because we use
  // the builder anyways.
  double position = 0.0;
  int currentIndex = 0;
  double totalAnimationOffset = 0.0;
  double? itemExtent;
  final int? childCount = childManager.estimatedChildCount;
  while (currentIndex < index) {
    if (childCount != null && currentIndex > childCount - 1) {
      break;
    }

    itemExtent = itemExtentBuilder(currentIndex, _currentLayoutDimensions);
    if (itemExtent == null) {
      break;
    }
    if (_animationLeadingIndices.keys.contains(currentIndex)) {
      final UniqueKey animationKey = _animationLeadingIndices[currentIndex]!;
      assert(_animationOffsets[animationKey] != null);
      // We add the offset accounting for the animation value.
      totalAnimationOffset += _animationOffsets[animationKey]! * (1 - _activeAnimations[animationKey]!.value);
    }
    position += itemExtent;
    currentIndex++;
  }
  return position - totalAnimationOffset;
}