calculateTargetSnap function
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;
});
}
}