Skip to content

Commit e72cc6f

Browse files
committed
feat: AnimationController
1 parent 06b2db4 commit e72cc6f

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## **AnimationController**
2+
3+
>
4+
AnimationController是一个特殊的Animation对象
5+
* 默认情况下,AnimationController在给定的时间段内会线性的生成从0.0到1.0的数字
6+
* 创建一个AnimationController时需要传递一个vsync参数,存在vsync时会防止屏幕外动画(动画的UI不在当前屏幕时)消耗不必要的资源,通过将SingleTickerProviderStateMixin添加到类定义中
7+
* 实例化一个AnimationController不会启动它运行,可以通过 .forward()方法启动动画
8+
* AnimationController的addListener方法可以监听动画每一帧
9+
* AnimationController的addStatusListener方法可以监听动画的状态
10+
11+
12+
### 构造方法
13+
``` dart
14+
AnimationController({
15+
double value,
16+
this.duration,
17+
this.debugLabel,
18+
this.lowerBound = 0.0,
19+
this.upperBound = 1.0,
20+
this.animationBehavior = AnimationBehavior.normal,
21+
@required TickerProvider vsync,
22+
})
23+
```
24+
25+
### 属性介绍
26+
* value: 动画值
27+
* lowerBound: 动画最小值
28+
* upperBound: 动画最大值
29+
* duration: 动画持续时间
30+
* vsync: vsync对象会绑定动画的定时器到一个可视的widget,所以当widget不显示时,动画定时器将会暂停,当widget再次显示时,动画定时器重新恢复执行,这样就可以避免动画相关UI不在当前屏幕时消耗资源。 如果要使用自定义的State对象作为vsync时,请包含TickerProviderStateMixin,因为是当前Widget使用动画,所以一般绑定值为 this
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Index extends StatelessWidget {
4+
@override
5+
Widget build(BuildContext context) {
6+
return Scaffold(
7+
appBar: AppBar(
8+
title: Text('AnimationController'),
9+
),
10+
body: AnimationDemo()
11+
);
12+
}
13+
}
14+
15+
class AnimationDemo extends StatefulWidget {
16+
@override
17+
_AnimationDemoState createState() => _AnimationDemoState();
18+
}
19+
20+
class _AnimationDemoState extends State<AnimationDemo>
21+
with TickerProviderStateMixin{
22+
23+
AnimationController animationDemoController;
24+
Animation animation;
25+
Animation animationColor;
26+
CurvedAnimation curve;
27+
28+
@override
29+
void initState() {
30+
// TODO: implement initState
31+
super.initState();
32+
animationDemoController = AnimationController(
33+
// value: 32,
34+
// lowerBound: 32,
35+
// upperBound: 100,
36+
duration: Duration(milliseconds: 2500),
37+
vsync: this
38+
);
39+
40+
41+
curve = CurvedAnimation(parent: animationDemoController, curve: Curves.bounceOut);
42+
43+
animation = Tween(begin: 32.0, end: 100.0).animate(curve);
44+
animationColor = ColorTween(begin: Colors.pinkAccent, end: Colors.red).animate(curve);
45+
46+
animationDemoController.addListener(() {
47+
print('当前动画值:${animationDemoController.value}');
48+
});
49+
animationDemoController.addStatusListener((AnimationStatus status) {
50+
print('当前动画状态:${status}');
51+
});
52+
animationDemoController.forward();
53+
}
54+
55+
@override
56+
void dispose() {
57+
// TODO: implement dispose
58+
super.dispose();
59+
animationDemoController.dispose();
60+
}
61+
62+
63+
@override
64+
Widget build(BuildContext context) {
65+
return Center(
66+
child: AnimatedHeart(
67+
animations: [
68+
animation,
69+
animationColor
70+
],
71+
controller: animationDemoController,
72+
),
73+
);
74+
}
75+
}
76+
77+
78+
class AnimatedHeart extends AnimatedWidget {
79+
final List animations;
80+
final AnimationController controller;
81+
82+
AnimatedHeart({
83+
this.animations,
84+
this.controller
85+
}):super(listenable: controller);
86+
87+
@override
88+
Widget build(BuildContext context) {
89+
return IconButton(
90+
icon: Icon(Icons.favorite),
91+
iconSize: animations[0].value,
92+
color: animations[1].value,
93+
onPressed: (){
94+
switch (controller.status) {
95+
case AnimationStatus.completed:
96+
controller.reverse();
97+
break;
98+
default:
99+
controller.forward();
100+
}
101+
},
102+
);
103+
}
104+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:efox_flutter/components/widgetComp.dart' as WidgetComp;
3+
import 'demo.dart' as Demo;
4+
5+
class Index extends StatefulWidget {
6+
static String title = 'AnimationController';
7+
static String mdUrl = 'docs/widget/animate/animationcontroller/index.md';
8+
static String originCodeUrl = 'https://docs.flutter.io/flutter/animation/AnimationController-class.html';
9+
10+
@override
11+
_IndexState createState() => new _IndexState();
12+
}
13+
14+
class _IndexState extends State<Index> {
15+
@override
16+
Widget build(BuildContext context) {
17+
return WidgetComp.Index(
18+
title: Index.title,
19+
originCodeUrl: Index.originCodeUrl,
20+
mdUrl: Index.mdUrl,
21+
demoChild: [
22+
Demo.Index(),
23+
],
24+
);
25+
}
26+
}

lib/widget/animate/index.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:efox_flutter/store/objects/widget_info.dart';
2+
import 'animationcontroller/index.dart' as AnimationController;
23
import 'animatedopacity/index.dart' as animatedopacity;
34
import 'animatedphysicalmodel/index.dart' as animatedphysicalmodel;
45
import 'animatedpositioned/index.dart' as animatedpositioned;
@@ -23,6 +24,11 @@ import 'animatedbuilder/index.dart' as animatedbuilder;
2324
const nameSpaces = '/animate_';
2425

2526
List widgets = [
27+
ItemInfo(
28+
widget: AnimationController.Index(),
29+
code: 57685, // gesture
30+
title: AnimationController.Index.title,
31+
),
2632
ItemInfo(
2733
widget: animatedbuilder.Index(),
2834
code: 58295, // color_lens

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ flutter:
104104
- docs/widget/gestures/gesturedetector/
105105
- docs/widget/gestures/ignorepointer/
106106
- docs/widget/gestures/longpressdraggable/
107+
- docs/widget/animate/animationcontroller/
107108
- docs/widget/animate/animatedbuilder/
108109
- docs/widget/animate/animatedcontainer/
109110
- docs/widget/animate/animatedcrossfade/

0 commit comments

Comments
 (0)