computeTargetIndex static method
Computes target index based on drag position and velocity.
Implementation
static int computeTargetIndex({
required double currentRelativeX,
required double velocityX,
required double itemWidth,
required int itemCount,
double velocityThreshold = 0.5,
double projectionTime = 0.3,
}) {
if (currentRelativeX < 0) return 0;
if (currentRelativeX > 1) return itemCount - 1;
if (velocityX.abs() > velocityThreshold) {
final projectedX =
(currentRelativeX + velocityX * projectionTime).clamp(0.0, 1.0);
var targetIndex =
(projectedX / itemWidth).round().clamp(0, itemCount - 1);
final currentIndex =
(currentRelativeX / itemWidth).round().clamp(0, itemCount - 1);
if (velocityX > velocityThreshold &&
targetIndex <= currentIndex &&
currentIndex < itemCount - 1) {
targetIndex = currentIndex + 1;
} else if (velocityX < -velocityThreshold &&
targetIndex >= currentIndex &&
currentIndex > 0) {
targetIndex = currentIndex - 1;
}
return targetIndex;
}
return (currentRelativeX / itemWidth).round().clamp(0, itemCount - 1);
}