getAlignmentFromGlobalPosition static method

double getAlignmentFromGlobalPosition(
  1. Offset globalPosition,
  2. BuildContext context,
  3. int itemCount
)

Converts a global drag position to horizontal alignment (-1 to 1).

Applies rubber band resistance when dragging beyond edges.

Parameters:

  • globalPosition: The global position from drag details
  • context: Build context to find the render box
  • itemCount: Total number of items

Returns: Alignment value with rubber band resistance applied.

Implementation

static double getAlignmentFromGlobalPosition(
  Offset globalPosition,
  BuildContext context,
  int itemCount,
) {
  final box = context.findRenderObject()! as RenderBox;
  final localPosition = box.globalToLocal(globalPosition);

  // Calculate the effective draggable range
  final indicatorWidth = 1.0 / itemCount;
  final draggableRange = 1.0 - indicatorWidth;
  final padding = indicatorWidth / 2;

  // Map drag position to 0-1 range
  final rawRelativeX = (localPosition.dx / box.size.width).clamp(0.0, 1.0);
  final normalizedX = (rawRelativeX - padding) / draggableRange;

  // Apply rubber band resistance for overdrag
  final adjustedRelativeX = applyRubberBandResistance(normalizedX);

  // Convert to -1 to 1 range
  return (adjustedRelativeX * 2) - 1;
}