Skip to content

Commit cfeb76e

Browse files
authored
Improved documentation for SpringSimulation (flutter#146674)
Add a code snippet demonstrating the usage of SpringSimulation in the docs. Improve the descriptions of each argument in SpringDescription.
1 parent 0c7a514 commit cfeb76e

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

packages/flutter/lib/src/physics/spring_simulation.dart

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import 'utils.dart';
1111

1212
export 'tolerance.dart' show Tolerance;
1313

14+
// Examples can assume:
15+
// late AnimationController _controller;
16+
1417
/// Structure that describes a spring's constants.
1518
///
1619
/// Used to configure a [SpringSimulation].
@@ -25,9 +28,13 @@ class SpringDescription {
2528
});
2629

2730
/// Creates a spring given the mass (m), stiffness (k), and damping ratio (ζ).
28-
/// The damping ratio is especially useful trying to determining the type of
29-
/// spring to create. A ratio of 1.0 creates a critically damped spring, > 1.0
30-
/// creates an overdamped spring and < 1.0 an underdamped one.
31+
/// The damping ratio describes a gradual reduction in a spring oscillation.
32+
/// By using the damping ratio, you can define how rapidly the oscillations
33+
/// decay from one bounce to the next.
34+
///
35+
/// The damping ratio is especially useful when trying to determining the type
36+
/// of spring to create. A ratio of 1.0 creates a critically damped
37+
/// spring, > 1.0 creates an overdamped spring and < 1.0 an underdamped one.
3138
///
3239
/// See [mass] and [stiffness] for the units for those arguments. The damping
3340
/// ratio is unitless.
@@ -37,17 +44,32 @@ class SpringDescription {
3744
double ratio = 1.0,
3845
}) : damping = ratio * 2.0 * math.sqrt(mass * stiffness);
3946

40-
/// The mass of the spring (m). The units are arbitrary, but all springs
41-
/// within a system should use the same mass units.
47+
/// The mass of the spring (m).
48+
///
49+
/// The units are arbitrary, but all springs within a system should use
50+
/// the same mass units.
51+
///
52+
/// The greater the mass, the larger the amplitude of oscillation,
53+
/// and the longer the time to return to the equilibrium position.
4254
final double mass;
4355

44-
/// The spring constant (k). The units of stiffness are M/T², where M is the
45-
/// mass unit used for the value of the [mass] property, and T is the time
46-
/// unit used for driving the [SpringSimulation].
56+
/// The spring constant (k).
57+
///
58+
/// The units of stiffness are M/T², where M is the mass unit used for the
59+
/// value of the [mass] property, and T is the time unit used for driving
60+
/// the [SpringSimulation].
61+
///
62+
/// Stiffness defines the spring constant, which measures the strength of
63+
/// the spring. A stiff spring applies more force to the object that is
64+
/// attached for some deviation from the rest position.
4765
final double stiffness;
4866

4967
/// The damping coefficient (c).
5068
///
69+
/// It is a pure number without physical meaning and describes the oscillation
70+
/// and decay of a system after being disturbed. The larger the damping,
71+
/// the fewer oscillations and smaller the amplitude of the elastic motion.
72+
///
5173
/// Do not confuse the damping _coefficient_ (c) with the damping _ratio_ (ζ).
5274
/// To create a [SpringDescription] with a damping ratio, use the [
5375
/// SpringDescription.withDampingRatio] constructor.
@@ -80,6 +102,30 @@ enum SpringType {
80102
/// A spring simulation.
81103
///
82104
/// Models a particle attached to a spring that follows Hooke's law.
105+
///
106+
/// {@tool snippet}
107+
///
108+
/// This method triggers an [AnimationController] (a previously constructed
109+
/// `_controller` field) to simulate a spring oscillation.
110+
///
111+
/// ```dart
112+
/// void _startSpringMotion() {
113+
/// _controller.animateWith(SpringSimulation(
114+
/// const SpringDescription(
115+
/// mass: 1.0,
116+
/// stiffness: 300.0,
117+
/// damping: 15.0,
118+
/// ),
119+
/// 0.0, // starting position
120+
/// 1.0, // ending position
121+
/// 0.0, // starting velocity
122+
/// ));
123+
/// }
124+
/// ```
125+
/// {@end-tool}
126+
///
127+
/// This [AnimationController] could be used with an [AnimatedBuilder] to
128+
/// animate the position of a child as if it were attached to a spring.
83129
class SpringSimulation extends Simulation {
84130
/// Creates a spring simulation from the provided spring description, start
85131
/// distance, end distance, and initial velocity.

0 commit comments

Comments
 (0)