-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicAnimationStructureAbout.dart
206 lines (180 loc) · 5.69 KB
/
BasicAnimationStructureAbout.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import 'package:flutter/material.dart';
import 'package:flutter_starter_notes/component/CommonTitle.dart';
class BasicAnimationStructureAbout extends StatelessWidget {
BasicAnimationStructureAbout({Key key, this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
CommonTitle('动画的基本结构 - 没有 Curve,默认是匀速'),
ScaleAnimationRoute(
useCurve: false,
),
CommonTitle('动画的基本结构 - 有 Curve'),
ScaleAnimationRoute(
useCurve: true,
),
CommonTitle('使用AnimatedWidget简化'),
ScaleAnimationRouteWithAnimatedWidget(),
CommonTitle('用AnimatedBuilder重构'),
ScaleAnimationRouteWithAnimatedBuilder(),
],
),
),
);
}
}
class ScaleAnimationRoute extends StatefulWidget {
ScaleAnimationRoute({Key key, this.useCurve});
final bool useCurve;
@override
_ScaleAnimationRouteState createState() => new _ScaleAnimationRouteState();
}
//需要继承TickerProvider,如果有多个AnimationController,则应该使用TickerProviderStateMixin。
class _ScaleAnimationRouteState extends State<ScaleAnimationRoute>
with SingleTickerProviderStateMixin {
Animation<double> animation;
AnimationController controller;
initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(seconds: 3), vsync: this);
if (this.widget.useCurve) {
//使用弹性曲线
CurvedAnimation curvedAnimation =
CurvedAnimation(parent: controller, curve: Curves.bounceIn);
//图片宽高从0变到300
animation = new Tween(begin: 0.0, end: 100.0).animate(curvedAnimation)
..addListener(() {
setState(() => {});
});
} else {
//图片宽高从0变到300
animation = new Tween(begin: 0.0, end: 100.0).animate(controller)
..addListener(() {
setState(() => {});
});
}
//启动动画
controller.forward();
}
@override
Widget build(BuildContext context) {
return new Center(
child: Image.asset("imgs/avatar.png",
width: animation.value, height: animation.value),
);
}
dispose() {
//路由销毁时需要释放动画资源
controller.dispose();
super.dispose();
}
}
// 使用AnimatedWidget简化
class AnimatedImage extends AnimatedWidget {
AnimatedImage({Key key, Animation<double> animation})
: super(key: key, listenable: animation);
Widget build(BuildContext context) {
final Animation<double> animation = listenable;
return new Center(
child: Image.asset("imgs/avatar.png",
width: animation.value, height: animation.value),
);
}
}
class ScaleAnimationRouteWithAnimatedWidget extends StatefulWidget {
@override
_ScaleAnimationRouteWithAnimatedWidgetState createState() =>
new _ScaleAnimationRouteWithAnimatedWidgetState();
}
class _ScaleAnimationRouteWithAnimatedWidgetState
extends State<ScaleAnimationRouteWithAnimatedWidget>
with SingleTickerProviderStateMixin {
Animation<double> animation;
AnimationController controller;
initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(seconds: 3), vsync: this);
//图片宽高从0变到300
animation = new Tween(begin: 0.0, end: 100.0).animate(controller);
//启动动画
controller.forward();
}
@override
Widget build(BuildContext context) {
return AnimatedImage(
animation: animation,
);
}
dispose() {
//路由销毁时需要释放动画资源
controller.dispose();
super.dispose();
}
}
class GrowTransition extends StatelessWidget {
GrowTransition({this.child, this.animation});
final Widget child;
final Animation<double> animation;
Widget build(BuildContext context) {
return new Center(
child: new AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget child) {
return new Container(
height: animation.value, width: animation.value, child: child);
},
child: child),
);
}
}
class ScaleAnimationRouteWithAnimatedBuilder extends StatefulWidget {
@override
_ScaleAnimationRouteWithAnimatedBuilderState createState() =>
new _ScaleAnimationRouteWithAnimatedBuilderState();
}
class _ScaleAnimationRouteWithAnimatedBuilderState
extends State<ScaleAnimationRouteWithAnimatedBuilder>
with SingleTickerProviderStateMixin {
Animation<double> animation;
AnimationController controller;
initState() {
super.initState();
controller = new AnimationController(
duration: const Duration(seconds: 3), vsync: this);
//图片宽高从0变到300
animation = new Tween(begin: 0.0, end: 100.0).animate(controller);
// 通过 动画状态监听 实现循环动画
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//动画执行结束时反向执行动画
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
//动画恢复到初始状态时执行动画(正向)
controller.forward();
}
});
//启动动画
controller.forward();
}
@override
Widget build(BuildContext context) {
return GrowTransition(
child: Image.asset("imgs/avatar.png"),
animation: animation,
);
}
dispose() {
//路由销毁时需要释放动画资源
controller.dispose();
super.dispose();
}
}