animate method
Animates a list of track animations.
This works like set but animates to the target values instead of
jumping. Only the tracks named in animations are (re)started; any other
tracks already animating keep running untouched. Use stop to halt
specific tracks. Passing an empty list is a no-op.
Per-track start values and velocities are carried on each
TrackAnimation (from: / withVelocity:). Loops live inside each
animation's steps via TrackStep.loop.
Returns a MotionFuture for this call's tracks. It completes when they
have all settled, and MotionFuture.ended when they have all ended.
Restarting one of them, or stop(canceled: true), cancels it. A
graceful stop completes it once the tracks rest. An infinitely looping
animation never completes, so don't await it.
A track whose animation is a single TrackStep.to that its motion finishes at once from where the track is, such as a spring at rest on its target, doesn't play: it doesn't tick or change status, and counts as settled right away.
Passing an empty list returns an already-complete future.
onStep is called once for every authored leaf step each track enters,
in order, including steps shorter than a frame. It is not called while
scrubbing or for implicit together loop barriers.
It belongs to the plans this call starts: a later call that restarts a
track replaces it for that track only, and tracks the later call
doesn't name keep reporting to it. An error it throws is reported to
FlutterError.reportError, and playback goes on.
Implementation
MotionFuture animate(
List<TrackAnimation> animations, {
void Function(Track track, int stepIndex)? onStep,
}) {
assert(
_ticker != null,
'Cannot animate a disposed TrackController.',
);
if (_ticker == null) return MotionFuture.complete();
assert(
() {
final seen = <Track>{};
for (final animation in animations) {
if (!seen.add(animation.track)) return false;
}
return true;
}(),
'animate received multiple animations for the same track. '
'To sequence steps on one track, use a single entry: '
'track([.to(a), .to(b)]).',
);
final timelineTracks =
animations.map((animation) => animation.track).toSet();
// Naming no tracks is a no-op: tracks not named in this call are left
// running untouched.
if (timelineTracks.isEmpty) return MotionFuture.complete();
_cancelFutures(timelineTracks);
for (final track in timelineTracks) {
if (onStep == null) {
_onSteps.remove(track);
} else {
_onSteps[track] = onStep;
}
}
final resting = <Track, List<double>>{};
for (final animation in animations) {
if (_restingValues(animation) case final values?) {
resting[animation.track] = values;
}
}
final moving = resting.isEmpty
? animations
: [
for (final animation in animations)
if (!resting.containsKey(animation.track)) animation,
];
final movingTracks = resting.isEmpty
? timelineTracks
: {for (final animation in moving) animation.track};
_mergeTokenParticipants(animations, timelineTracks);
if (resting.isNotEmpty) {
_rest(resting);
if (moving.isEmpty) {
if (_activeTracks.isEmpty) _ticker?.stop(canceled: true);
return MotionFuture.complete();
}
}
// Previously-running tracks stay active; the named tracks (re)start.
_joinRun(movingTracks);
_activeTracks.addAll(movingTracks);
final startOffset = _clock.now;
for (final animation in moving) {
_playAnimation(
_applyMotionOverride(animation),
startOffset: startOffset,
);
}
final inspecting = MotorInspectionRegistry.isInspecting;
if (inspecting) _recordPlan(moving, start: startOffset);
final future = _futureFor(movingTracks);
_startTicker();
if (inspecting) _tryEstimateDurations(movingTracks);
_updateStatus();
return future;
}