|
1 |
| -# animation_tutorial_code |
| 1 | +# Animation Tutorial |
2 | 2 |
|
3 |
| -A new Flutter project. |
| 3 | +This repository has all the code that has been done in live. |
4 | 4 |
|
5 |
| -## Getting Started |
| 5 | +## Which type of animation to choose? |
6 | 6 |
|
7 |
| -This project is a starting point for a Flutter application. |
| 7 | +1. If you want to animate any parameter of TextStyle the go with `AnimatedDefaultTextStyle` |
| 8 | +2. If you want basic animations and the implicit animation widget is available like AnimatedContainer, then go with Implicit Animations |
| 9 | +3. If implicit animation widget is not available, then go with Explicit animation. |
| 10 | +4. If you want different animations to start and end at different points, then go with Staggered Animations. |
| 11 | +5. And if you want animations in `Custom Painter` then go with Custom Painter Animations. This will allow you to animation any pixel of your app with any kind of animation you want. |
8 | 12 |
|
9 |
| -A few resources to get you started if this is your first Flutter project: |
| 13 | +## Heres how each part works |
10 | 14 |
|
11 |
| -- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) |
12 |
| -- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) |
| 15 | +### Animated Text Style |
13 | 16 |
|
14 |
| -For help getting started with Flutter, view our |
15 |
| -[online documentation](https://flutter.dev/docs), which offers tutorials, |
16 |
| -samples, guidance on mobile development, and a full API reference. |
| 17 | +Heres example code of it |
| 18 | +```dart |
| 19 | +AnimatedDefaultTextStyle( |
| 20 | + style: isButtonPressed |
| 21 | + ? const TextStyle( |
| 22 | + fontSize: 25, |
| 23 | + color: Colors.red, |
| 24 | + backgroundColor: Colors.cyan, |
| 25 | + ) |
| 26 | + : const TextStyle( |
| 27 | + fontSize: 30, |
| 28 | + color: Colors.green, |
| 29 | + backgroundColor: Colors.yellow, |
| 30 | + ), |
| 31 | + duration: const Duration(seconds: 1), |
| 32 | + child: const Text('Hello World'), |
| 33 | +), |
| 34 | +``` |
| 35 | + |
| 36 | +To use `AnimatedDefaultTextStyle`, we need to update the value of any parameter which we want to animate. For example, here we are changing font size from 25 to 30 with setState when isButtonPressed bool is changed. We can animate all the parameters of TextStyle in same way. |
| 37 | + |
| 38 | +### Implicit Animation |
| 39 | + |
| 40 | +Implicit animations are kind of similar to AnimatedDefaultTextStyle. In this, we would replace normal widgets with animated widgets one. |
| 41 | +For Example: |
| 42 | +- Container -> AnimatedContainer |
| 43 | +- Padding -> AnimatedPadding |
| 44 | +- Opacity -> AnimatedOpacity |
| 45 | +and so on... |
| 46 | + |
| 47 | +Then after that, we would set duration of animation by |
| 48 | +```dart |
| 49 | +const Duration(seconds: 1) |
| 50 | +``` |
| 51 | +And finally we would update the parameter that we want to animate with setState and variable. |
| 52 | +Heres full code explaining that |
| 53 | +```dart |
| 54 | +AnimatedContainer( |
| 55 | + height: isButtonPressed ? 200 : 100, |
| 56 | + width: isButtonPressed ? 200 : 100, |
| 57 | + duration: const Duration(seconds: 1), |
| 58 | + color: isButtonPressed ? Colors.red : Colors.blue, |
| 59 | +), |
| 60 | +``` |
| 61 | +For example, the height is swapped between 200 and 100 depending on isButtonPressed bool value. |
| 62 | + |
| 63 | +### Explicit Animation |
| 64 | + |
| 65 | +Explicit Animations allows one to create much advanced animations, and also can animate preety much any parameter!!! |
| 66 | +For this we need to first declare Animation and AnimationController. |
| 67 | + |
| 68 | +```dart |
| 69 | +late AnimationController controller; |
| 70 | +late Animation sizeAnimation; |
| 71 | +late Animation colorAnimation; |
| 72 | +``` |
| 73 | + |
| 74 | +Now we need to extend our widget with `SingleTickerProviderStateMixin` like this: |
| 75 | + |
| 76 | +```dart |
| 77 | +class _MainScreenState extends State<MainScreen> with SingleTickerProviderStateMixin { |
| 78 | +``` |
| 79 | + |
| 80 | +After this, we would initialize variables in initState like this: |
| 81 | + |
| 82 | +```dart |
| 83 | +@override |
| 84 | +void initState() { |
| 85 | + controller = AnimationController(vsync: this, duration: const Duration(seconds: 2)); |
| 86 | + sizeAnimation = Tween(begin: 100.0, end: 200.0).animate(controller); |
| 87 | + colorAnimation = ColorTween(begin: Colors.blue, end: Colors.red).animate(controller); |
| 88 | +
|
| 89 | + super.initState(); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +As the controller is initialized, it also needs to be disposed so that memory leak in app doesn't occur and also increases performance of app |
| 94 | + |
| 95 | +```dart |
| 96 | +@override |
| 97 | +void dispose() { |
| 98 | + controller.dispose(); |
| 99 | +
|
| 100 | + super.dispose(); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +And now we just need to wrap the widget that we need to animate with AnimatedBuilder and then animate the parameter by passing Animation widget with .value method called |
| 105 | + |
| 106 | +```dart |
| 107 | +AnimatedBuilder( |
| 108 | + animation: controller, |
| 109 | + builder: (context, child) => Container( |
| 110 | + height: sizeAnimation.value, |
| 111 | + width: sizeAnimation.value, |
| 112 | + color: colorAnimation.value, |
| 113 | + ), |
| 114 | +), |
| 115 | +``` |
| 116 | + |
| 117 | +### Staggered Animation |
| 118 | + |
| 119 | +Staggered Animations are very similar to Explicit Animation. The ony difference is that in this, we are defining different animations with different start and end points. So the only change in the code is as like that we are not directly passing controller in `.animate()`, but passing `CurvedAnimation` and then in `curve` we can use `Interval()` and pass curve accordingly (default is `Curves.linear`). Heres a little example of it: |
| 120 | + |
| 121 | +```dart |
| 122 | +sizeAnimation = sizeTween.animate( |
| 123 | + CurvedAnimation( |
| 124 | + parent: controller, |
| 125 | + curve: Interval(0.0, 0.6, curve: Curves.easeInOutCubic), |
| 126 | + ), |
| 127 | +); |
| 128 | +colorAnimation = colorTween.animate( |
| 129 | + CurvedAnimation( |
| 130 | + parent: controller, |
| 131 | + curve: Interval(0.4, 1, curve: Curves.ease), |
| 132 | + ), |
| 133 | +); |
| 134 | +``` |
| 135 | + |
| 136 | +### Animated Custom Painter |
| 137 | + |
| 138 | +In this one, we are using Custom Painter to draw any kind of shapes, patterns, and so on. And then we are animating different aspects of it. In the given below example, I am animating a arc so that it sweeps full rotation. All the code will be same as that of Explicit Animation. The change is in the Painter. In it, we pass the controller with the help of constructor and then use that controller to create Animation variables and finally use them to animate different parts of custom painter. Heres the example of it: |
| 139 | + |
| 140 | +```dart |
| 141 | +class AnimatedCustomPainter extends CustomPainter { |
| 142 | + Paint _paint = Paint(); |
| 143 | + final Animation<double> _sweep; |
| 144 | + final Animation _color; |
| 145 | +
|
| 146 | + AnimatedCustomPainter(AnimationController controller) |
| 147 | + : _sweep = Tween<double>(begin: 0, end: 2 * pi).animate(controller), |
| 148 | + _color = ColorTween(begin: Colors.red, end: Colors.blue).animate(controller), |
| 149 | + super(repaint: controller); |
| 150 | +
|
| 151 | + @override |
| 152 | + void paint(Canvas canvas, Size size) { |
| 153 | + _paint.color = _color.value; |
| 154 | +
|
| 155 | + canvas.drawArc( |
| 156 | + Rect.fromPoints(Offset(50, 50), Offset(250, 250)), |
| 157 | + -pi / 2, |
| 158 | + _sweep.value, |
| 159 | + true, |
| 160 | + _paint, |
| 161 | + ); |
| 162 | + } |
| 163 | +
|
| 164 | + @override |
| 165 | + bool shouldRepaint(covariant CustomPainter oldDelegate) => false; |
| 166 | +} |
| 167 | +``` |
0 commit comments