springForDuration function

SpringConfig springForDuration(
  1. Duration duration, {
  2. SpringPreset? preset,
  3. double? dampingRatio,
})

The spring that settles in duration while keeping a damping ratio of dampingRatio (default: the ratio of preset, or of snappy).

The result is accurate to the integrator's 1/240 s substep for anything in the range the physics can express. Outside it the duration clamps: a critically damped spring cannot settle in 20 ms no matter how stiff, so very short durations land on the fastest spring that damping ratio allows (~60 ms snappy, ~90 ms bouncy, ~120 ms smooth). Ask for a shorter morph than that and you get the floor, not a divergent spring. Check what you actually got with springSettleTime.

Implementation

SpringConfig springForDuration(
  Duration duration, {
  SpringPreset? preset,
  double? dampingRatio,
}) {
  final base = (preset ?? SpringPreset.snappy).config;
  final zeta = (dampingRatio ?? dampingRatioOf(base)).clamp(0.05, 5.0);
  final key = (duration.inMicroseconds, (zeta * 1e6).round());
  final hit = _cache[key];
  if (hit != null) return hit;

  final seconds = duration.inMicroseconds / 1e6;
  final result = _solve(seconds, zeta);
  if (_cache.length >= _cacheMax) _cache.clear();
  _cache[key] = result;
  return result;
}