createScrollPosition method

  1. @override
ScrollPosition createScrollPosition(
  1. ScrollPhysics physics,
  2. ScrollContext context,
  3. ScrollPosition? oldPosition
)
override

Creates a ScrollPosition for use by a Scrollable widget.

Subclasses can override this function to customize the ScrollPosition used by the scrollable widgets they control. For example, PageController overrides this function to return a page-oriented scroll position subclass that keeps the same page visible when the scrollable widget resizes.

By default, returns a ScrollPositionWithSingleContext.

The arguments are generally passed to the ScrollPosition being created:

Implementation

@override
ScrollPosition createScrollPosition(ScrollPhysics physics,
    ScrollContext context, ScrollPosition? oldPosition) {
  assert(_carouselState != null);
  final List<int>? weights = _carouselState!._weights;
  final double? itemExtent = _carouselState!._itemExtent;
  int expandedItem = initialItem;

  if (weights != null && !_carouselState!.allowFullyExpand) {
    int smallerWeights = 0;
    for (final int weight in weights) {
      if (weight == weights.max) {
        break;
      }
      smallerWeights += 1;
    }
    expandedItem -= smallerWeights;
  }

  return _CarouselPosition(
    physics: physics,
    context: context,
    initialItem: expandedItem,
    itemExtent: itemExtent,
    layoutWeights: weights,
    oldPosition: oldPosition,
  );
}