resolve static method

int resolve({
  1. required int currentIndex,
  2. required double velocity,
  3. required double displacement,
  4. required double cardWidth,
  5. required int itemCount,
  6. double velocityThreshold = 600.0,
  7. double distanceThreshold = 0.3,
})

Resolves which index to snap to after a horizontal gesture.

Implementation

static int resolve({
  required int currentIndex,
  required double velocity,
  required double displacement,
  required double cardWidth,
  required int itemCount,
  double velocityThreshold = 600.0,
  double distanceThreshold = 0.3,
}) {
  if (itemCount <= 1) return 0;

  final normalizedDisplacement = displacement / math.max(cardWidth, 1.0);
  int targetIndex = currentIndex;

  if (velocity.abs() > velocityThreshold) {
    targetIndex = velocity > 0 ? currentIndex - 1 : currentIndex + 1;
  } else if (normalizedDisplacement.abs() > distanceThreshold) {
    targetIndex = normalizedDisplacement > 0
        ? currentIndex - 1
        : currentIndex + 1;
  }

  return targetIndex.clamp(0, itemCount - 1);
}