|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'dart:math' as math; |
| 3 | + |
| 4 | +class SpringAnimationsPage extends StatelessWidget { |
| 5 | + @override |
| 6 | + Widget build(BuildContext context) { |
| 7 | + return Scaffold( |
| 8 | + appBar: AppBar( |
| 9 | + title: Text('Spring Animations'), |
| 10 | + ), |
| 11 | + body: GridView.count( |
| 12 | + crossAxisCount: 2, |
| 13 | + padding: EdgeInsets.all(16), |
| 14 | + childAspectRatio: 3, |
| 15 | + mainAxisSpacing: 16, |
| 16 | + crossAxisSpacing: 16, |
| 17 | + children: [ |
| 18 | + SpringButton( |
| 19 | + text: 'Elastic Gentle', |
| 20 | + curve: Curves.easeOutBack, |
| 21 | + duration: Duration(milliseconds: 800), |
| 22 | + ), |
| 23 | + SpringButton( |
| 24 | + text: 'Bouncy Wobble', |
| 25 | + curve: _BouncyWobbleCurve(), |
| 26 | + duration: Duration(milliseconds: 1200), |
| 27 | + ), |
| 28 | + SpringButton( |
| 29 | + text: 'Quick Oscillation', |
| 30 | + curve: _QuickOscillationCurve(), |
| 31 | + duration: Duration(milliseconds: 600), |
| 32 | + ), |
| 33 | + SpringButton( |
| 34 | + text: 'Slow Rebound', |
| 35 | + curve: _SlowReboundCurve(), |
| 36 | + duration: Duration(milliseconds: 1500), |
| 37 | + ), |
| 38 | + SpringButton( |
| 39 | + text: 'Snappy Elastic', |
| 40 | + curve: _SnappyElasticCurve(), |
| 41 | + duration: Duration(milliseconds: 700), |
| 42 | + ), |
| 43 | + SpringButton( |
| 44 | + text: 'Soft Bounce', |
| 45 | + curve: _SoftBounceCurve(), |
| 46 | + duration: Duration(milliseconds: 900), |
| 47 | + ), |
| 48 | + SpringButton( |
| 49 | + text: 'Vibrating Spring', |
| 50 | + curve: _VibratingSpringCurve(), |
| 51 | + duration: Duration(milliseconds: 1000), |
| 52 | + ), |
| 53 | + SpringButton( |
| 54 | + text: 'Smooth Overshoot', |
| 55 | + curve: _SmoothOvershootCurve(), |
| 56 | + duration: Duration(milliseconds: 850), |
| 57 | + ), |
| 58 | + SpringButton( |
| 59 | + text: 'Elastic Snap', |
| 60 | + curve: _ElasticSnapCurve(), |
| 61 | + duration: Duration(milliseconds: 750), |
| 62 | + ), |
| 63 | + SpringButton( |
| 64 | + text: 'Gentle Wobble', |
| 65 | + curve: _GentleWobbleCurve(), |
| 66 | + duration: Duration(milliseconds: 1100), |
| 67 | + ), |
| 68 | + SpringButton( |
| 69 | + text: 'Bouncy', |
| 70 | + curve: Curves.bounceOut, |
| 71 | + duration: Duration(milliseconds: 500), |
| 72 | + ), |
| 73 | + SpringButton( |
| 74 | + text: 'Elastic', |
| 75 | + curve: Curves.elasticOut, |
| 76 | + duration: Duration(milliseconds: 1000), |
| 77 | + ), |
| 78 | + SpringButton( |
| 79 | + text: 'Quick Snap', |
| 80 | + curve: Curves.easeOutBack, |
| 81 | + duration: Duration(milliseconds: 300), |
| 82 | + ), |
| 83 | + SpringButton( |
| 84 | + text: 'Slow Spring', |
| 85 | + curve: Curves.elasticInOut, |
| 86 | + duration: Duration(milliseconds: 1500), |
| 87 | + ), |
| 88 | + SpringButton( |
| 89 | + text: 'Overshoot', |
| 90 | + curve: Curves.easeOutCirc, |
| 91 | + duration: Duration(milliseconds: 600), |
| 92 | + ), |
| 93 | + SpringButton( |
| 94 | + text: 'Gentle', |
| 95 | + curve: Curves.easeOutSine, |
| 96 | + duration: Duration(milliseconds: 400), |
| 97 | + ), |
| 98 | + SpringButton( |
| 99 | + text: 'Sharp', |
| 100 | + curve: Curves.easeOutExpo, |
| 101 | + duration: Duration(milliseconds: 350), |
| 102 | + ), |
| 103 | + SpringButton( |
| 104 | + text: 'Wobble', |
| 105 | + curve: _WobbleCurve(), |
| 106 | + duration: Duration(milliseconds: 800), |
| 107 | + ), |
| 108 | + SpringButton( |
| 109 | + text: 'Bouncy Elastic', |
| 110 | + curve: _BouncyElasticCurve(), |
| 111 | + duration: Duration(milliseconds: 1200), |
| 112 | + ), |
| 113 | + SpringButton( |
| 114 | + text: 'Damped', |
| 115 | + curve: _DampedSpringCurve(), |
| 116 | + duration: Duration(milliseconds: 1000), |
| 117 | + ), |
| 118 | + ], |
| 119 | + ), |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +class SpringButton extends StatefulWidget { |
| 125 | + final String text; |
| 126 | + final Curve curve; |
| 127 | + final Duration duration; |
| 128 | + |
| 129 | + SpringButton({ |
| 130 | + required this.text, |
| 131 | + required this.curve, |
| 132 | + required this.duration, |
| 133 | + }); |
| 134 | + |
| 135 | + @override |
| 136 | + _SpringButtonState createState() => _SpringButtonState(); |
| 137 | +} |
| 138 | + |
| 139 | +class _SpringButtonState extends State<SpringButton> |
| 140 | + with SingleTickerProviderStateMixin { |
| 141 | + late AnimationController _controller; |
| 142 | + late Animation<double> _animation; |
| 143 | + |
| 144 | + @override |
| 145 | + void initState() { |
| 146 | + super.initState(); |
| 147 | + _controller = AnimationController( |
| 148 | + vsync: this, |
| 149 | + duration: widget.duration, |
| 150 | + ); |
| 151 | + _animation = Tween<double>(begin: 0, end: 1).animate( |
| 152 | + CurvedAnimation(parent: _controller, curve: widget.curve), |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + @override |
| 157 | + void dispose() { |
| 158 | + _controller.dispose(); |
| 159 | + super.dispose(); |
| 160 | + } |
| 161 | + |
| 162 | + @override |
| 163 | + Widget build(BuildContext context) { |
| 164 | + return GestureDetector( |
| 165 | + onTap: () { |
| 166 | + _controller.forward(from: 0); |
| 167 | + }, |
| 168 | + child: AnimatedBuilder( |
| 169 | + animation: _animation, |
| 170 | + builder: (context, child) { |
| 171 | + return Transform.rotate( |
| 172 | + angle: math.sin(_animation.value * math.pi) * 10 * math.pi / 180, |
| 173 | + child: ElevatedButton( |
| 174 | + onPressed: () { |
| 175 | + _controller.forward(from: 0); |
| 176 | + }, |
| 177 | + child: Text(widget.text), |
| 178 | + ), |
| 179 | + ); |
| 180 | + }, |
| 181 | + ), |
| 182 | + ); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +class _WobbleCurve extends Curve { |
| 187 | + @override |
| 188 | + double transform(double t) { |
| 189 | + return math.sin(t * math.pi * 4) * (1 - t); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +class _BouncyElasticCurve extends Curve { |
| 194 | + @override |
| 195 | + double transform(double t) { |
| 196 | + return -math.pow(math.e, -8 * t) * math.cos(t * 12) + 1; |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +class _DampedSpringCurve extends Curve { |
| 201 | + @override |
| 202 | + double transform(double t) { |
| 203 | + return 1 - math.pow(math.e, -3 * t) * math.cos(t * 10); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +class _BouncyWobbleCurve extends Curve { |
| 208 | + @override |
| 209 | + double transform(double t) { |
| 210 | + return -math.pow(math.e, -5 * t) * math.cos(t * 15) + 1; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +class _QuickOscillationCurve extends Curve { |
| 215 | + @override |
| 216 | + double transform(double t) { |
| 217 | + return math.sin(t * math.pi * 6) * (1 - t) + t; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +class _SlowReboundCurve extends Curve { |
| 222 | + @override |
| 223 | + double transform(double t) { |
| 224 | + return (1 - math.pow(math.cos(t * math.pi / 2), 3)).toDouble(); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +class _SnappyElasticCurve extends Curve { |
| 229 | + @override |
| 230 | + double transform(double t) { |
| 231 | + return -math.pow(2, -10 * t) * math.sin((t - 0.1) * 5 * math.pi) + 1; |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +class _SoftBounceCurve extends Curve { |
| 236 | + @override |
| 237 | + double transform(double t) { |
| 238 | + return 1 - math.pow(1 - t, 3) * math.cos(t * math.pi * 2); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +class _VibratingSpringCurve extends Curve { |
| 243 | + @override |
| 244 | + double transform(double t) { |
| 245 | + return math.sin(t * 15) * math.pow(1 - t, 2) + t; |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +class _SmoothOvershootCurve extends Curve { |
| 250 | + @override |
| 251 | + double transform(double t) { |
| 252 | + return t * t * (3 - 2 * t) + math.sin(t * math.pi * 2) * 0.1; |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +class _ElasticSnapCurve extends Curve { |
| 257 | + @override |
| 258 | + double transform(double t) { |
| 259 | + return math.pow(2, -8 * t) * math.sin(t * 8 * math.pi) + 1; |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +class _GentleWobbleCurve extends Curve { |
| 264 | + @override |
| 265 | + double transform(double t) { |
| 266 | + return t + math.sin(t * math.pi * 3) * 0.1 * (1 - t); |
| 267 | + } |
| 268 | +} |
0 commit comments