calculateTargetSnap function

  1. @visibleForTesting
double calculateTargetSnap({
  1. required double currentValue,
  2. required double velocity,
  3. required double velocityThreshold,
  4. required List<double> sortedSnaps,
})

Calculates the target snap point based on current value and velocity.

This is extracted as a top-level function for testability.

Implementation

@visibleForTesting
double calculateTargetSnap({
  required double currentValue,
  required double velocity,
  required double velocityThreshold,
  required List<double> sortedSnaps,
}) {
  // If velocity is high enough, fling to next snap point
  if (velocity.abs() > velocityThreshold) {
    if (velocity < 0) {
      // Swiping up - find next higher snap point
      return sortedSnaps.firstWhere(
        (s) => s > currentValue,
        orElse: () => sortedSnaps.last,
      );
    } else {
      // Swiping down - find next lower snap point
      return sortedSnaps.lastWhere(
        (s) => s < currentValue,
        orElse: () => sortedSnaps.first,
      );
    }
  } else {
    // Snap to nearest
    return sortedSnaps.reduce((a, b) {
      return (currentValue - a).abs() < (currentValue - b).abs() ? a : b;
    });
  }
}