resolveTarget method

GlassSheetState resolveTarget(
  1. SheetSnapshot current, {
  2. required double snapThreshold,
  3. required double velocityThreshold,
})

Computes target state based on current position and velocity.

Implementation

GlassSheetState resolveTarget(
  SheetSnapshot current, {
  required double snapThreshold,
  required double velocityThreshold,
}) {
  // The ordered rest states for this sheet's config (peek / half / full are
  // each optional; hidden appears only for a dismissible, peek-less sheet).
  final states = orderedStates;

  final positions = states
      .map((s) => positionForState(s, current.screenSize.height))
      .toList();
  final velocity = current.velocity;

  // Fast flick handling
  if (velocity > velocityThreshold) {
    // Find the next state above current
    for (int i = 0; i < states.length - 1; i++) {
      if (current.position < positions[i + 1] - 0.001) return states[i + 1];
    }
    return states.last;
  }
  if (velocity < -velocityThreshold) {
    // Find the next state below current
    for (int i = states.length - 1; i > 0; i--) {
      if (current.position > positions[i - 1] + 0.001) return states[i - 1];
    }
    return states.first;
  }

  // Boundary safety: if outside the range of segments, snap to extremes
  if (current.position <= positions.first) return states.first;
  if (current.position >= positions.last) return states.last;

  // Static snap handling: find which segment we are in
  for (int i = 0; i < states.length - 1; i++) {
    final s1 = states[i];
    final s2 = states[i + 1];
    final p1 = positions[i];
    final p2 = positions[i + 1];

    if (current.position >= p1 && current.position <= p2) {
      final range = p2 - p1;
      if (range <= 0.0001) continue;

      final progress = (current.position - p1) / range;

      // If we were moving towards s2, check if we crossed threshold
      if (current.state == s1) {
        return progress >= snapThreshold ? s2 : s1;
      }
      // If we were moving towards s1, check if we crossed threshold
      if (current.state == s2) {
        return (1.0 - progress) >= snapThreshold ? s1 : s2;
      }

      // Default: snap to nearest
      return progress >= 0.5 ? s2 : s1;
    }
  }

  // Final fallback: return the closest state overall
  double minDistance = double.infinity;
  GlassSheetState closest = current.state;
  for (int i = 0; i < states.length; i++) {
    final dist = (current.position - positions[i]).abs();
    if (dist < minDistance) {
      minDistance = dist;
      closest = states[i];
    }
  }
  return closest;
}