1
1
import 'package:flutter/material.dart' ;
2
2
import 'dart:math' as math;
3
+ import 'dart:math' ;
3
4
4
5
class SpringAnimationsDemo extends StatelessWidget {
5
6
@override
@@ -12,30 +13,37 @@ class SpringAnimationsDemo extends StatelessWidget {
12
13
child: Column (
13
14
mainAxisAlignment: MainAxisAlignment .spaceEvenly,
14
15
children: [
16
+ // SpringButton(
17
+ // onPressed: () => _showAnimation(context, 'Springy'),
18
+ // curve: SpringyCurve(),
19
+ // child: Text('Springy', style: TextStyle(fontSize: 18)),
20
+ // ),
21
+ // SpringButton(
22
+ // onPressed: () => _showAnimation(context, 'Shock Absorber'),
23
+ // curve: ShockAbsorberCurve(),
24
+ // child: Text('Shock Absorber', style: TextStyle(fontSize: 18)),
25
+ // ),
26
+ // SpringButton(
27
+ // onPressed: () => _showAnimation(context, 'Quick Bounce'),
28
+ // curve: QuickBounceCurve(),
29
+ // child: Text('Quick Bounce', style: TextStyle(fontSize: 18)),
30
+ // ),
31
+ // SpringButton(
32
+ // onPressed: () => _showAnimation(context, 'Snappy Spring'),
33
+ // curve: SnappySpringCurve(),
34
+ // child: Text('Snappy Spring', style: TextStyle(fontSize: 18)),
35
+ // ),
36
+ // SpringButton(
37
+ // onPressed: () => _showAnimation(context, 'Elastic Snap'),
38
+ // curve: ElasticSnapCurve(),
39
+ // child: Text('Elastic Snap', style: TextStyle(fontSize: 18)),
40
+ // ),
41
+
15
42
SpringButton (
16
- onPressed: () => _showAnimation (context, 'Springy' ),
17
- curve: SpringyCurve (),
18
- child: Text ('Springy' , style: TextStyle (fontSize: 18 )),
19
- ),
20
- SpringButton (
21
- onPressed: () => _showAnimation (context, 'Shock Absorber' ),
22
- curve: ShockAbsorberCurve (),
23
- child: Text ('Shock Absorber' , style: TextStyle (fontSize: 18 )),
24
- ),
25
- SpringButton (
26
- onPressed: () => _showAnimation (context, 'Quick Bounce' ),
27
- curve: QuickBounceCurve (),
28
- child: Text ('Quick Bounce' , style: TextStyle (fontSize: 18 )),
29
- ),
30
- SpringButton (
31
- onPressed: () => _showAnimation (context, 'Snappy Spring' ),
32
- curve: SnappySpringCurve (),
33
- child: Text ('Snappy Spring' , style: TextStyle (fontSize: 18 )),
34
- ),
35
- SpringButton (
36
- onPressed: () => _showAnimation (context, 'Elastic Snap' ),
37
- curve: ElasticSnapCurve (),
38
- child: Text ('Elastic Snap' , style: TextStyle (fontSize: 18 )),
43
+ onPressed: () => _showAnimation (context, 'High Velocity Spring' ),
44
+ curve: HighVelocitySpringCurve (),
45
+ child:
46
+ Text ('High Velocity Spring' , style: TextStyle (fontSize: 18 )),
39
47
),
40
48
],
41
49
),
@@ -152,3 +160,126 @@ class ElasticSnapCurve extends Curve {
152
160
return t * t * (3 - 2 * t) - math.sin (t * math.pi * 2 ) * 0.1 * (1 - t);
153
161
}
154
162
}
163
+
164
+ //----
165
+
166
+ // 2. Less springy version
167
+ class LessSpringySnappyCurve extends Curve {
168
+ @override
169
+ double transform (double t) {
170
+ return t * t * (3 - 2 * t) + sin (t * pi * 2 ) * 0.05 * (1 - t);
171
+ }
172
+ }
173
+
174
+ // 3. Heavier mass simulation
175
+ class HeavyMassSnappyCurve extends Curve {
176
+ @override
177
+ double transform (double t) {
178
+ return t * t * (3 - 2 * t) + sin (t * pi * 1.5 ) * 0.08 * (1 - t * t);
179
+ }
180
+ }
181
+
182
+ // 4. Quick start, slow end
183
+ class QuickStartSlowEndCurve extends Curve {
184
+ @override
185
+ double transform (double t) {
186
+ return t * t * (3 - 2 * t) + sin (t * pi) * 0.1 * (1 - pow (t, 3 ));
187
+ }
188
+ }
189
+
190
+ // 5. Damped oscillation
191
+ class DampedOscillationCurve extends Curve {
192
+ @override
193
+ double transform (double t) {
194
+ return t - sin (t * pi * 2 ) * 0.1 * exp (- 2 * t);
195
+ }
196
+ }
197
+
198
+ // 6. Soft bounce
199
+ class SoftBounceCurve extends Curve {
200
+ @override
201
+ double transform (double t) {
202
+ return t * t * (3 - 2 * t) - sin (t * pi) * 0.05 * (1 - t);
203
+ }
204
+ }
205
+
206
+ // 7. Delayed snap
207
+ class DelayedSnapCurve extends Curve {
208
+ @override
209
+ double transform (double t) {
210
+ return t < 0.5
211
+ ? 2 * t * t
212
+ : 1 - pow (- 2 * t + 2 , 2 ) / 2 + sin (t * pi) * 0.05 ;
213
+ }
214
+ }
215
+
216
+ // 8. Gradual acceleration
217
+ class GradualAccelerationCurve extends Curve {
218
+ @override
219
+ double transform (double t) {
220
+ return t * t * t + sin (t * pi * 2 ) * 0.03 * (1 - t);
221
+ }
222
+ }
223
+
224
+ // 9. Elastic-like with less oscillation
225
+ class MildElasticCurve extends Curve {
226
+ @override
227
+ double transform (double t) {
228
+ return pow (2 , - 8 * t) * sin ((t - 0.1 ) * pi * 2 ) * 0.5 + 1 ;
229
+ }
230
+ }
231
+
232
+ // 10. Smooth overshoot
233
+
234
+ class HeavyMassBaseCurve extends Curve {
235
+ @override
236
+ double transform (double t) {
237
+ return t * t * (3 - 2 * t) + sin (t * pi * 2 ) * 0.1 * (1 - t);
238
+ }
239
+ }
240
+
241
+ // Slow Start Heavy Mass
242
+ class SlowStartHeavyMassCurve extends Curve {
243
+ @override
244
+ double transform (double t) {
245
+ return pow (t, 3 ) + sin (t * pi) * 0.1 * (1 - t);
246
+ }
247
+ }
248
+
249
+ // Overshoot Heavy Mass
250
+ class OvershootHeavyMassCurve extends Curve {
251
+ @override
252
+ double transform (double t) {
253
+ return t < 0.6
254
+ ? 1.3 * t
255
+ : 1 + sin ((t - 0.6 ) * pi * 2.5 ) * 0.15 * pow (1 - t, 2 );
256
+ }
257
+ }
258
+
259
+ // Bouncy Heavy Mass
260
+ class BouncyHeavyMassCurve extends Curve {
261
+ @override
262
+ double transform (double t) {
263
+ return t < 0.5
264
+ ? 4 * t * t * t
265
+ : 1 - pow (- 2 * t + 2 , 3 ) / 2 + sin (t * pi * 3 ) * 0.1 * (1 - t);
266
+ }
267
+ }
268
+
269
+ class HighVelocitySpringCurve extends Curve {
270
+ final double a;
271
+ final double w;
272
+
273
+ HighVelocitySpringCurve ({this .a = 0.3 , this .w = 20 });
274
+
275
+ @override
276
+ double transform (double t) {
277
+ // Rapid initial movement
278
+ double initialMovement = (1 - pow (1 - t, 3 )).toDouble ();
279
+
280
+ // Small spring back effect
281
+ double springBack = sin (t * w) * a * pow (1 - t, 2 );
282
+
283
+ return initialMovement - springBack;
284
+ }
285
+ }
0 commit comments