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+ }
0 commit comments