diff --git a/packages/flutter/lib/src/animation/animation.dart b/packages/flutter/lib/src/animation/animation.dart index 37d14b3557..10001e5818 100644 --- a/packages/flutter/lib/src/animation/animation.dart +++ b/packages/flutter/lib/src/animation/animation.dart @@ -62,25 +62,85 @@ typedef AnimationStatusListener = void Function(AnimationStatus status); /// Signature for method used to transform values in [Animation.fromValueListenable]. typedef ValueListenableTransformer = 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]) +/// 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] and an [Animation] 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`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]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 extends Listenable implements ValueListenable { /// 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 extends Listenable implements ValueListenable { /// 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. /// diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index be8fa73610..c5b44d2d54 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -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 /// 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 }); } - /// 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 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 /// 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) { diff --git a/packages/flutter/lib/src/animation/tween.dart b/packages/flutter/lib/src/animation/tween.dart index b9eb74d651..fe0f635afe 100644 --- a/packages/flutter/lib/src/animation/tween.dart +++ b/packages/flutter/lib/src/animation/tween.dart @@ -24,7 +24,7 @@ export 'curves.dart' show Curve; /// from a callback. typedef AnimatableCallback = T Function(double value); -/// An object that can produce a value of type `T` given an [Animation] +/// An object that can produce a value of type [T] given an [Animation] /// as input. /// /// Typically, the values of the input animation are nominally in the range 0.0 @@ -85,9 +85,15 @@ abstract class Animatable { } /// 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 chain(Animatable parent) { return _ChainedEvaluation(parent, this); } @@ -152,11 +158,12 @@ class _ChainedEvaluation extends Animatable { /// [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} /// diff --git a/packages/flutter/lib/src/scheduler/ticker.dart b/packages/flutter/lib/src/scheduler/ticker.dart index affd145f0c..58b2d75029 100644 --- a/packages/flutter/lib/src/scheduler/ticker.dart +++ b/packages/flutter/lib/src/scheduler/ticker.dart @@ -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