Write more on Animation and related docs (#150727)
A few days ago I started reading up on how animations work, in preparation for starting to use them in a more complex way than I'd done before. I found it a bit difficult to get my head around; in particular the many different classes involved, how they relate to each other, and how to fit them together. So once I had worked that out, I sat down to express it in the form of documentation. The largest change here is an expansion of the docs on [Animation] itself, including a new section "Using animations" with several paragraphs laying out how one typically fits together AnimationController, TickerProvider, CurvedAnimation and/or Tween, and AnimatedWidget subclasses. [Animation] also gets an expanded "See also" list, a revised conceptual intro, and a new summary line. There are also revisions on [TickerProvider], [AnimatedController], and elsewhere; some new exposition, some revisions for clarity, and various small fixes.
This commit is contained in:
@@ -62,25 +62,85 @@ typedef AnimationStatusListener = void Function(AnimationStatus status);
|
||||
/// Signature for method used to transform values in [Animation.fromValueListenable].
|
||||
typedef ValueListenableTransformer<T> = T Function(T);
|
||||
|
||||
/// An animation with a value of type `T`.
|
||||
/// A value which might change over time, moving forward or backward.
|
||||
///
|
||||
/// An animation consists of a value (of type `T`) together with a status. The
|
||||
/// status indicates whether the animation is conceptually running from
|
||||
/// beginning to end or from the end back to the beginning, although the actual
|
||||
/// value of the animation might not change monotonically (e.g., if the
|
||||
/// animation uses a curve that bounces).
|
||||
/// An animation has a [value] (of type [T]) and a [status].
|
||||
/// The value conceptually lies on some path, and
|
||||
/// the status indicates how the value is currently moving along the path:
|
||||
/// forward, backward, or stopped at the end or the beginning.
|
||||
/// The path may double back on itself
|
||||
/// (e.g., if the animation uses a curve that bounces),
|
||||
/// so even when the animation is conceptually moving forward
|
||||
/// the value might not change monotonically.
|
||||
///
|
||||
/// Animations also let other objects listen for changes to either their value
|
||||
/// or their status. These callbacks are called during the "animation" phase of
|
||||
/// Consumers of the animation can listen for changes to either the value
|
||||
/// or the status, with [addListener] and [addStatusListener].
|
||||
/// The listener callbacks are called during the "animation" phase of
|
||||
/// the pipeline, just prior to rebuilding widgets.
|
||||
///
|
||||
/// To create a new animation that you can run forward and backward, consider
|
||||
/// using [AnimationController].
|
||||
/// An animation might move forward or backward on its own as time passes
|
||||
/// (like the opacity of a button that fades over a fixed duration
|
||||
/// once the user touches it),
|
||||
/// or it might be driven by the user
|
||||
/// (like the position of a slider that the user can drag back and forth),
|
||||
/// or it might do both
|
||||
/// (like a switch that snaps into place when released,
|
||||
/// or a [Dismissible] that responds to drag and fling gestures, etc.).
|
||||
/// The behavior is normally controlled by method calls on
|
||||
/// some underlying [AnimationController].
|
||||
/// When an animation is actively animating, it typically updates on
|
||||
/// each frame, driven by a [Ticker].
|
||||
///
|
||||
/// ## Using animations
|
||||
///
|
||||
/// For simple animation effects, consider using one of the
|
||||
/// [ImplicitlyAnimatedWidget] subclasses,
|
||||
/// like [AnimatedScale], [AnimatedOpacity], and many others.
|
||||
/// When an [ImplicitlyAnimatedWidget] suffices, there is
|
||||
/// no need to work with [Animation] or the rest of the classes
|
||||
/// discussed in this section.
|
||||
///
|
||||
/// Otherwise, typically an animation originates with an [AnimationController]
|
||||
/// (which is itself an [Animation<double>])
|
||||
/// created by a [State] that implements [TickerProvider].
|
||||
/// Further animations might be derived from that animation
|
||||
/// by using e.g. [Tween] or [CurvedAnimation].
|
||||
/// The animations might be used to configure an [AnimatedWidget]
|
||||
/// (using one of its many subclasses like [FadeTransition]),
|
||||
/// or their values might be used directly.
|
||||
///
|
||||
/// For example, the [AnimationController] may represent
|
||||
/// the abstract progress of the animation from 0.0 to 1.0;
|
||||
/// then a [CurvedAnimation] might apply an easing curve;
|
||||
/// and a [SizeTween] and [ColorTween] might each be applied to that
|
||||
/// to produce an [Animation<Size>] and an [Animation<Color>] that control
|
||||
/// a widget shrinking and changing color as the animation proceeds.
|
||||
///
|
||||
/// ## Performance considerations
|
||||
///
|
||||
/// Because the [Animation] keeps the same identity as the animation proceeds,
|
||||
/// it provides a convenient way for a [StatefulWidget] that orchestrates
|
||||
/// a complex animation to communicate the animation's progress to its
|
||||
/// various child widgets. Consider having higher-level widgets in the tree
|
||||
/// pass lower-level widgets the [Animation] itself, rather than its value,
|
||||
/// in order to avoid rebuilding the higher-level widgets on each frame
|
||||
/// even while the animation is active.
|
||||
/// If the leaf widgets also ignore [value] and pass the whole
|
||||
/// [Animation] object to a render object (like [FadeTransition] does),
|
||||
/// they too might be able to avoid rebuild and even relayout, so that the
|
||||
/// only work needed on each frame of the animation is to repaint.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [Tween], which can be used to create [Animation] subclasses that
|
||||
/// convert `Animation<double>`s into other kinds of [Animation]s.
|
||||
/// * [ImplicitlyAnimatedWidget] and its subclasses, which provide
|
||||
/// animation effects without the need to manually work with [Animation],
|
||||
/// [AnimationController], or even [State].
|
||||
/// * [AnimationController], an animation you can run forward and backward,
|
||||
/// stop, or set to a specific value.
|
||||
/// * [Tween], which can be used to convert [Animation<double>]s into
|
||||
/// other kinds of [Animation]s.
|
||||
/// * [AnimatedWidget] and its subclasses, which provide animation effects
|
||||
/// that can be controlled by an [Animation].
|
||||
abstract class Animation<T> extends Listenable implements ValueListenable<T> {
|
||||
/// Abstract const constructor. This constructor enables subclasses to provide
|
||||
/// const constructors so that they can be used in const expressions.
|
||||
@@ -88,7 +148,7 @@ abstract class Animation<T> extends Listenable implements ValueListenable<T> {
|
||||
|
||||
/// Create a new animation from a [ValueListenable].
|
||||
///
|
||||
/// The returned animation will always have an animations status of
|
||||
/// The returned animation will always have an animation status of
|
||||
/// [AnimationStatus.forward]. The value of the provided listenable can
|
||||
/// be optionally transformed using the [transformer] function.
|
||||
///
|
||||
|
||||
@@ -84,34 +84,29 @@ enum AnimationBehavior {
|
||||
/// * Create a [fling] animation effect using a physics simulation.
|
||||
///
|
||||
/// By default, an [AnimationController] linearly produces values that range
|
||||
/// from 0.0 to 1.0, during a given duration. The animation controller generates
|
||||
/// a new value whenever the device running your app is ready to display a new
|
||||
/// frame (typically, this rate is around 60 values per second).
|
||||
/// from 0.0 to 1.0, during a given duration.
|
||||
///
|
||||
/// When the animation is actively animating, the animation controller generates
|
||||
/// a new value each time the device running your app is ready to display a new
|
||||
/// frame (typically, this rate is around 60–120 values per second).
|
||||
/// If the animation controller is associated with a [State]
|
||||
/// through a [TickerProvider], then its updates will be silenced when that
|
||||
/// [State]'s subtree is disabled as defined by [TickerMode]; time will still
|
||||
/// elapse, and methods like [forward] and [stop] can still be called and
|
||||
/// will change the value, but the controller will not generate new values
|
||||
/// on its own.
|
||||
///
|
||||
/// ## Ticker providers
|
||||
///
|
||||
/// An [AnimationController] needs a [TickerProvider], which is configured using
|
||||
/// the `vsync` argument on the constructor.
|
||||
/// The constructor uses the [TickerProvider] to create a [Ticker], which
|
||||
/// the [AnimationController] uses to step through the animation it controls.
|
||||
///
|
||||
/// The [TickerProvider] interface describes a factory for [Ticker] objects. A
|
||||
/// [Ticker] is an object that knows how to register itself with the
|
||||
/// [SchedulerBinding] and fires a callback every frame. The
|
||||
/// [AnimationController] class uses a [Ticker] to step through the animation
|
||||
/// that it controls.
|
||||
///
|
||||
/// If an [AnimationController] is being created from a [State], then the State
|
||||
/// can use the [TickerProviderStateMixin] and [SingleTickerProviderStateMixin]
|
||||
/// classes to implement the [TickerProvider] interface. The
|
||||
/// [TickerProviderStateMixin] class always works for this purpose; the
|
||||
/// [SingleTickerProviderStateMixin] is slightly more efficient in the case of
|
||||
/// the class only ever needing one [Ticker] (e.g. if the class creates only a
|
||||
/// single [AnimationController] during its entire lifetime).
|
||||
///
|
||||
/// The widget test framework [WidgetTester] object can be used as a ticker
|
||||
/// provider in the context of tests. In other contexts, you will have to either
|
||||
/// pass a [TickerProvider] from a higher level (e.g. indirectly from a [State]
|
||||
/// that mixes in [TickerProviderStateMixin]), or create a custom
|
||||
/// [TickerProvider] subclass.
|
||||
/// For advice on obtaining a ticker provider, see [TickerProvider].
|
||||
/// Typically the relevant [State] serves as the ticker provider,
|
||||
/// after applying a suitable mixin (like [SingleTickerProviderStateMixin])
|
||||
/// to cause the [State] subclass to implement [TickerProvider].
|
||||
///
|
||||
/// ## Life cycle
|
||||
///
|
||||
@@ -223,7 +218,7 @@ enum AnimationBehavior {
|
||||
///
|
||||
/// ** See code in examples/api/lib/animation/animation_controller/animated_digit.0.dart **
|
||||
/// {@end-tool}
|
||||
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [Tween], the base class for converting an [AnimationController] to a
|
||||
@@ -241,12 +236,10 @@ class AnimationController extends Animation<double>
|
||||
/// debugging (used by [toString]).
|
||||
///
|
||||
/// * [lowerBound] is the smallest value this animation can obtain and the
|
||||
/// value at which this animation is deemed to be dismissed. It cannot be
|
||||
/// null.
|
||||
/// value at which this animation is deemed to be dismissed.
|
||||
///
|
||||
/// * [upperBound] is the largest value this animation can obtain and the
|
||||
/// value at which this animation is deemed to be completed. It cannot be
|
||||
/// null.
|
||||
/// value at which this animation is deemed to be completed.
|
||||
///
|
||||
/// * `vsync` is the required [TickerProvider] for the current context. It can
|
||||
/// be changed by calling [resync]. See [TickerProvider] for advice on
|
||||
@@ -580,7 +573,7 @@ class AnimationController extends Animation<double>
|
||||
});
|
||||
}
|
||||
|
||||
/// Drives the animation from its current value to target.
|
||||
/// Drives the animation from its current value to the given target, "forward".
|
||||
///
|
||||
/// Returns a [TickerFuture] that completes when the animation is complete.
|
||||
///
|
||||
@@ -617,7 +610,7 @@ class AnimationController extends Animation<double>
|
||||
return _animateToInternal(target, duration: duration, curve: curve);
|
||||
}
|
||||
|
||||
/// Drives the animation from its current value to target.
|
||||
/// Drives the animation from its current value to the given target, "backward".
|
||||
///
|
||||
/// Returns a [TickerFuture] that completes when the animation is complete.
|
||||
///
|
||||
@@ -629,6 +622,10 @@ class AnimationController extends Animation<double>
|
||||
/// regardless of whether `target` < [value] or not. At the end of the
|
||||
/// animation, when `target` is reached, [status] is reported as
|
||||
/// [AnimationStatus.dismissed].
|
||||
///
|
||||
/// If the `target` argument is the same as the current [value] of the
|
||||
/// animation, then this won't animate, and the returned [TickerFuture] will
|
||||
/// be already complete.
|
||||
TickerFuture animateBack(double target, { Duration? duration, Curve curve = Curves.linear }) {
|
||||
assert(() {
|
||||
if (this.duration == null && reverseDuration == null && duration == null) {
|
||||
|
||||
@@ -24,7 +24,7 @@ export 'curves.dart' show Curve;
|
||||
/// from a callback.
|
||||
typedef AnimatableCallback<T> = T Function(double value);
|
||||
|
||||
/// An object that can produce a value of type `T` given an [Animation<double>]
|
||||
/// An object that can produce a value of type [T] given an [Animation<double>]
|
||||
/// as input.
|
||||
///
|
||||
/// Typically, the values of the input animation are nominally in the range 0.0
|
||||
@@ -85,9 +85,15 @@ abstract class Animatable<T> {
|
||||
}
|
||||
|
||||
/// Returns a new [Animatable] whose value is determined by first evaluating
|
||||
/// the given parent and then evaluating this object.
|
||||
/// the given parent and then evaluating this object at the result.
|
||||
///
|
||||
/// This allows [Tween]s to be chained before obtaining an [Animation].
|
||||
/// This method represents function composition on [transform]:
|
||||
/// the [transform] method of the returned [Animatable] is the result of
|
||||
/// composing this object's [transform] method with
|
||||
/// the given parent's [transform] method.
|
||||
///
|
||||
/// This allows [Tween]s to be chained before obtaining an [Animation],
|
||||
/// without allocating an [Animation] for the intermediate result.
|
||||
Animatable<T> chain(Animatable<double> parent) {
|
||||
return _ChainedEvaluation<T>(parent, this);
|
||||
}
|
||||
@@ -152,11 +158,12 @@ class _ChainedEvaluation<T> extends Animatable<T> {
|
||||
/// [animate] method and pass it the [Animation] object that you want to
|
||||
/// modify.
|
||||
///
|
||||
/// You can chain [Tween] objects together using the [chain] method, so that a
|
||||
/// single [Animation] object is configured by multiple [Tween] objects called
|
||||
/// in succession. This is different than calling the [animate] method twice,
|
||||
/// which results in two separate [Animation] objects, each configured with a
|
||||
/// single [Tween].
|
||||
/// You can chain [Tween] objects together using the [chain] method,
|
||||
/// producing the function composition of their [transform] methods.
|
||||
/// Configuring a single [Animation] object by calling [animate] on the
|
||||
/// resulting [Tween] produces the same result as calling the [animate] method
|
||||
/// on each [Tween] separately in succession, but more efficiently because
|
||||
/// it avoids creating [Animation] objects for the intermediate results.
|
||||
///
|
||||
/// {@tool snippet}
|
||||
///
|
||||
|
||||
@@ -17,23 +17,28 @@ export 'package:flutter/foundation.dart' show DiagnosticsNode;
|
||||
|
||||
/// Signature for the callback passed to the [Ticker] class's constructor.
|
||||
///
|
||||
/// The argument is the time that the object had spent enabled so far
|
||||
/// at the time of the callback being called.
|
||||
/// The argument is the time elapsed from
|
||||
/// the frame timestamp when the ticker was last started
|
||||
/// to the current frame timestamp.
|
||||
typedef TickerCallback = void Function(Duration elapsed);
|
||||
|
||||
/// An interface implemented by classes that can vend [Ticker] objects.
|
||||
///
|
||||
/// To obtain a [TickerProvider], consider mixing in either
|
||||
/// [TickerProviderStateMixin] (which always works)
|
||||
/// or [SingleTickerProviderStateMixin] (which is more efficient when it works)
|
||||
/// to make a [State] subclass implement [TickerProvider].
|
||||
/// That [State] can then be passed to lower-level widgets
|
||||
/// or other related objects.
|
||||
/// This ensures the resulting [Ticker]s will only tick when that [State]'s
|
||||
/// subtree is enabled, as defined by [TickerMode].
|
||||
///
|
||||
/// In widget tests, the [WidgetTester] object is also a [TickerProvider].
|
||||
///
|
||||
/// Tickers can be used by any object that wants to be notified whenever a frame
|
||||
/// triggers, but are most commonly used indirectly via an
|
||||
/// [AnimationController]. [AnimationController]s need a [TickerProvider] to
|
||||
/// obtain their [Ticker]. If you are creating an [AnimationController] from a
|
||||
/// [State], then you can use the [TickerProviderStateMixin] and
|
||||
/// [SingleTickerProviderStateMixin] classes to obtain a suitable
|
||||
/// [TickerProvider]. The widget test framework [WidgetTester] object can be
|
||||
/// used as a ticker provider in the context of tests. In other contexts, you
|
||||
/// will have to either pass a [TickerProvider] from a higher level (e.g.
|
||||
/// indirectly from a [State] that mixes in [TickerProviderStateMixin]), or
|
||||
/// create a custom [TickerProvider] subclass.
|
||||
/// obtain their [Ticker].
|
||||
abstract class TickerProvider {
|
||||
/// Abstract const constructor. This constructor enables subclasses to provide
|
||||
/// const constructors so that they can be used in const expressions.
|
||||
@@ -46,9 +51,9 @@ abstract class TickerProvider {
|
||||
Ticker createTicker(TickerCallback onTick);
|
||||
}
|
||||
|
||||
// TODO(jacobr): make Ticker use Diagnosticable to simplify reporting errors
|
||||
// related to a ticker.
|
||||
/// Calls its callback once per animation frame.
|
||||
/// Calls its callback once per animation frame, when enabled.
|
||||
///
|
||||
/// To obtain a ticker, consider [TickerProvider].
|
||||
///
|
||||
/// When created, a ticker is initially disabled. Call [start] to
|
||||
/// enable the ticker.
|
||||
@@ -58,11 +63,17 @@ abstract class TickerProvider {
|
||||
/// are called.
|
||||
///
|
||||
/// By convention, the [start] and [stop] methods are used by the ticker's
|
||||
/// consumer, and the [muted] property is controlled by the [TickerProvider]
|
||||
/// that created the ticker.
|
||||
/// consumer (for example, an [AnimationController]), and the [muted] property
|
||||
/// is controlled by the [TickerProvider] that created the ticker (for example,
|
||||
/// a [State] that uses [TickerProviderStateMixin] to silence the ticker when
|
||||
/// the state's subtree is disabled as defined by [TickerMode]).
|
||||
///
|
||||
/// Tickers are driven by the [SchedulerBinding]. See
|
||||
/// [SchedulerBinding.scheduleFrameCallback].
|
||||
/// See also:
|
||||
///
|
||||
/// * [TickerProvider], for obtaining a ticker.
|
||||
/// * [SchedulerBinding.scheduleFrameCallback], which drives tickers.
|
||||
// TODO(jacobr): make Ticker use Diagnosticable to simplify reporting errors
|
||||
// related to a ticker.
|
||||
class Ticker {
|
||||
/// Creates a ticker that will call the provided callback once per frame while
|
||||
/// running.
|
||||
@@ -149,6 +160,8 @@ class Ticker {
|
||||
/// [isTicking].
|
||||
bool get isActive => _future != null;
|
||||
|
||||
/// The frame timestamp when the ticker was last started,
|
||||
/// as reported by [SchedulerBinding.currentFrameTimestamp].
|
||||
Duration? _startTime;
|
||||
|
||||
/// Starts the clock for this [Ticker]. If the ticker is not [muted], then this
|
||||
|
||||
Reference in New Issue
Block a user