@@ -11,6 +11,9 @@ import 'utils.dart';
11
11
12
12
export 'tolerance.dart' show Tolerance;
13
13
14
+ // Examples can assume:
15
+ // late AnimationController _controller;
16
+
14
17
/// Structure that describes a spring's constants.
15
18
///
16
19
/// Used to configure a [SpringSimulation] .
@@ -25,9 +28,13 @@ class SpringDescription {
25
28
});
26
29
27
30
/// 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.
31
38
///
32
39
/// See [mass] and [stiffness] for the units for those arguments. The damping
33
40
/// ratio is unitless.
@@ -37,17 +44,32 @@ class SpringDescription {
37
44
double ratio = 1.0 ,
38
45
}) : damping = ratio * 2.0 * math.sqrt (mass * stiffness);
39
46
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.
42
54
final double mass;
43
55
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.
47
65
final double stiffness;
48
66
49
67
/// The damping coefficient (c).
50
68
///
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
+ ///
51
73
/// Do not confuse the damping _coefficient_ (c) with the damping _ratio_ (ζ).
52
74
/// To create a [SpringDescription] with a damping ratio, use the [
53
75
/// SpringDescription.withDampingRatio] constructor.
@@ -80,6 +102,30 @@ enum SpringType {
80
102
/// A spring simulation.
81
103
///
82
104
/// 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.
83
129
class SpringSimulation extends Simulation {
84
130
/// Creates a spring simulation from the provided spring description, start
85
131
/// distance, end distance, and initial velocity.
0 commit comments