resolve static method

IndicatorJelly resolve({
  1. required double lead,
  2. required double trail,
  3. required int itemCount,
  4. required double travelDirection,
  5. required bool dragging,
  6. double stretch = 0.0,
  7. double flatten = 0.0,
  8. double? dragDeformation,
  9. IndicatorJellyPhysics physics = IndicatorJellyPhysics.native,
})

Computes the deformation from the springs driven by IndicatorJellyBuilder.

  • lead / trail — current alignment (−1…1) of the position spring and of the trailing-edge spring.
  • itemCount — number of equal-width cells the indicator moves between.
  • travelDirection — +1 (rightwards) or −1 (leftwards): the direction of the most recent released travel.
  • dragging — while dragging the trailing edge only lags; it never squishes.
  • stretch — the signed travel stretch (positive pulls the right edge rightwards).
  • flatten — the travel flattening.
  • dragDeformation — optional signed horizontal deformation produced by IndicatorJellyBuilder's native acceleration-driven drag oscillator (positive = accelerating stretch, negative = decelerating/reversal squish & vertical bulge). When null while dragging, falls back to trailing-edge lag from lead and trail.

Implementation

static IndicatorJelly resolve({
  required double lead,
  required double trail,
  required int itemCount,
  required double travelDirection,
  required bool dragging,
  double stretch = 0.0,
  double flatten = 0.0,
  double? dragDeformation,
  IndicatorJellyPhysics physics = IndicatorJellyPhysics.native,
}) {
  if (!physics.enabled ||
      itemCount < 2 ||
      !lead.isFinite ||
      !trail.isFinite ||
      !stretch.isFinite ||
      !flatten.isFinite ||
      (dragDeformation != null && !dragDeformation.isFinite)) {
    return identity;
  }

  // Alignment → multiples of the cell width. One alignment unit spans
  // (itemCount − 1) / 2 cells.
  final separation = (lead - trail) * (itemCount - 1) / 2;

  // Movement of each edge, as a fraction of the width (positive = right).
  var right = math.max(stretch, 0.0);
  var left = math.min(stretch, 0.0);
  var scaleY = 1.0 - physics.travelThinning * stretch.abs() - flatten;

  void applyDragDeformation(double u) {
    if (u >= 0) {
      final dx = _softLimit(u, physics.maxDragStretch);
      left -= dx / 2;
      right += dx / 2;
      scaleY -= physics.dragThinning * dx;
    } else {
      // Native iOS 27 deceleration/reversal squish & vertical bulge
      // (measured peak scaleX ≈ 0.870, scaleY ≈ 1.149 -> bulge slope 1.55).
      final squish = _softLimit(-u, 0.13);
      left += squish / 2;
      right -= squish / 2;
      scaleY += 1.55 * squish;
    }
  }

  if (dragging) {
    if (dragDeformation != null) {
      applyDragDeformation(dragDeformation);
    } else {
      final lag = _softLimit(separation.abs(), physics.maxDragStretch);
      if (separation >= 0) {
        left -= lag;
      } else {
        right += lag;
      }
      scaleY -= physics.dragThinning * lag;
    }
  } else {
    final direction = travelDirection >= 0 ? 1.0 : -1.0;
    final overshoot = -separation * direction;
    if (overshoot > 0) {
      final squish = math.min(overshoot, physics.maxCompression);
      final anchor = physics.squishAnchor.clamp(0.0, 1.0);
      final trailing = squish * (1 + anchor) / 2;
      final leading = squish * (1 - anchor) / 2;
      if (direction > 0) {
        left += trailing;
        right -= leading;
      } else {
        right -= trailing;
        left += leading;
      }
      scaleY *= 1 + physics.compressionBulge * squish;
    }

    if (dragDeformation != null && dragDeformation.abs() > 1e-6) {
      applyDragDeformation(dragDeformation);
    }

    // Pin the leading edge at the end of the track, as UIKit does: the
    // position spring's overshoot and the relaxing travel stretch become
    // squish instead of pushing the pill past the last cell.
    final cells = (itemCount - 1) / 2;
    if (direction > 0) {
      final past = (lead - 1) * cells + right;
      if (past > 0) right -= past;
    } else {
      final past = (-1 - lead) * cells - left;
      if (past > 0) left += past;
    }
  }

  return IndicatorJelly(
    scaleX: math.max(1 + right - left, 0.05),
    scaleY: math.max(scaleY, 0.05),
    offsetX: (right + left) / 2,
  );
}