@@ -107,52 +107,41 @@ class BouncingScrollSimulation extends SimulationGroup {
107
107
// simplifications have been made.
108
108
class ClampingScrollSimulation extends Simulation {
109
109
/// Creates a scroll physics simulation that matches Android scrolling.
110
- //
111
- // TODO(ianh): The incoming `velocity` is used to determine the starting speed
112
- // and duration, but does not represent the exact velocity of the simulation
113
- // at t=0 as it should. This causes crazy scrolling irregularities when the
114
- // scroll dimensions change during a fling.
115
110
ClampingScrollSimulation ({
116
111
@required this .position,
117
112
@required this .velocity,
118
113
this .friction: 0.015 ,
119
114
Tolerance tolerance: Tolerance .defaultTolerance,
120
115
}) : super (tolerance: tolerance) {
121
- _scaledFriction = friction * _decelerationForFriction ( 0.84 ); // See mPhysicalCoeff
116
+ assert ( _flingVelocityPenetration ( 0.0 ) == _kInitialVelocityPenetration);
122
117
_duration = _flingDuration (velocity);
123
- _distance = _flingDistance (velocity);
118
+ _distance = (velocity * _duration / _kInitialVelocityPenetration). abs ( );
124
119
}
125
120
126
121
final double position;
127
122
final double velocity;
128
123
final double friction;
129
124
130
- double _scaledFriction;
131
125
double _duration;
132
126
double _distance;
133
127
134
128
// See DECELERATION_RATE.
135
- static final double _decelerationRate = math.log (0.78 ) / math.log (0.9 );
129
+ static final double _kDecelerationRate = math.log (0.78 ) / math.log (0.9 );
136
130
137
131
// See computeDeceleration().
138
- double _decelerationForFriction (double friction) {
132
+ static double _decelerationForFriction (double friction) {
139
133
return friction * 61774.04968 ;
140
134
}
141
135
142
- // See getSplineDeceleration().
143
- double _flingDeceleration (double velocity) {
144
- return math.log (0.35 * velocity.abs () / _scaledFriction);
145
- }
146
-
147
136
// See getSplineFlingDuration(). Returns a value in seconds.
148
137
double _flingDuration (double velocity) {
149
- return math.exp (_flingDeceleration (velocity) / (_decelerationRate - 1.0 ));
150
- }
138
+ // See mPhysicalCoeff
139
+ final double scaledFriction = friction * _decelerationForFriction (0.84 );
140
+
141
+ // See getSplineDeceleration().
142
+ final double deceleration = math.log (0.35 * velocity.abs () / scaledFriction);
151
143
152
- // See getSplineFlingDistance().
153
- double _flingDistance (double velocity) {
154
- final double rate = _decelerationRate / (_decelerationRate - 1.0 ) * _flingDeceleration (velocity);
155
- return _scaledFriction * math.exp (rate);
144
+ return math.exp (deceleration / (_kDecelerationRate - 1.0 ));
156
145
}
157
146
158
147
// Based on a cubic curve fit to the Scroller.computeScrollOffset() values
@@ -170,13 +159,14 @@ class ClampingScrollSimulation extends Simulation {
170
159
// Scale f(t) so that 0.0 <= f(t) <= 1.0
171
160
// f(t) = (1165.03 t^3 - 3143.62 t^2 + 2945.87 t) / 961.0
172
161
// = 1.2 t^3 - 3.27 t^2 + 3.065 t
173
- double _flingDistancePenetration (double t) {
174
- return (1.2 * t * t * t) - (3.27 * t * t) + (3.065 * t);
162
+ static const double _kInitialVelocityPenetration = 3.065 ;
163
+ static double _flingDistancePenetration (double t) {
164
+ return (1.2 * t * t * t) - (3.27 * t * t) + (_kInitialVelocityPenetration * t);
175
165
}
176
166
177
167
// The derivative of the _flingDistancePenetration() function.
178
- double _flingVelocityPenetration (double t) {
179
- return (3.63693 * t * t) - (6.5424 * t) + 3.06542 ;
168
+ static double _flingVelocityPenetration (double t) {
169
+ return (3.6 * t * t) - (6.54 * t) + _kInitialVelocityPenetration ;
180
170
}
181
171
182
172
@override
@@ -188,7 +178,7 @@ class ClampingScrollSimulation extends Simulation {
188
178
@override
189
179
double dx (double time) {
190
180
final double t = (time / _duration).clamp (0.0 , 1.0 );
191
- return _distance * _flingVelocityPenetration (t) * velocity.sign;
181
+ return _distance * _flingVelocityPenetration (t) * velocity.sign / _duration ;
192
182
}
193
183
194
184
@override
0 commit comments