onHorizontalDragEnd method

void onHorizontalDragEnd(
  1. double velocity,
  2. double cardWidth
)

Implementation

void onHorizontalDragEnd(double velocity, double cardWidth) {
  if (!_state.canSwipeHorizontally) {
    _state = _state.copyWith(horizontalDragOffset: 0.0);
    notifyListeners();
    return;
  }

  final targetIndex = CarouselSnapResolver.resolve(
    currentIndex: _state.currentIndex,
    velocity: velocity,
    displacement: _state.horizontalDragOffset,
    cardWidth: cardWidth,
    itemCount: _state.itemCount,
  );

  final indexDelta = targetIndex - _state.currentIndex;
  const gap = 16.0;
  final targetOffset = indexDelta == 0
      ? 0.0
      : -(indexDelta * (cardWidth + gap));

  _horizontalTargetIndex = targetIndex;
  _horizontalSnapFrom = _state.horizontalDragOffset;
  _horizontalSnapTo = targetOffset;
  _horizontalSnapController.value = 0.0;
  _horizontalSnapController.animateTo(
    1.0,
    curve: Curves.easeOutCubic,
    duration: Duration(milliseconds: indexDelta == 0 ? 120 : 180),
  );
}