0
点赞
收藏
分享

微信扫一扫

Flutter 动画 AnimationController AnimatedWidget

晚熟的猫 2023-03-10 阅读 81


 

import 'package:flutter/material.dart';

class AnimationDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("AnimationDone"),
elevation: 0.0,
),
body: AnimationHome(),
);
}
}

class AnimationHome extends StatefulWidget {
@override
_AnimationHomeState createState() => _AnimationHomeState();
}

class _AnimationHomeState extends State<AnimationHome>
with TickerProviderStateMixin {
AnimationController animationDemoController;
Animation _animation;
Animation _animationColor;
int _animValue = 32;
var _curve;

@override
void initState() {
super.initState();
animationDemoController = AnimationController(
duration: Duration(milliseconds: 3000),
vsync: this,
// lowerBound: 32.0,
// value: 32.0,
// upperBound: 100.0
);
_curve = CurvedAnimation(
parent: animationDemoController, curve: Curves.bounceInOut);
_animation = Tween(begin: 32.0, end: 100.0).animate(_curve);
_animationColor = ColorTween(begin: Colors.red, end: Colors.red[900])
.animate(animationDemoController);

animationDemoController.addListener(() {
setState(() {});
});

animationDemoController.addStatusListener((status) {
print(status);
});
}

@override
void dispose() {
animationDemoController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Center(
child: AnimatedHeart(
animations: [_animation, _animationColor],
controller: animationDemoController),
);
}
}

class AnimatedHeart extends AnimatedWidget {
final List<Animation> animations;
final AnimationController controller;

AnimatedHeart({this.animations, this.controller})
: super(listenable: controller);

@override
Widget build(BuildContext context) {
return IconButton(
icon: Icon(Icons.favorite),
iconSize: animations[0].value,
color: animations[1].value,
onPressed: () {
controller.forward();
switch (controller.status) {
case AnimationStatus.completed:
controller.reverse();
break;
defalut:
controller.forward();
}
},
);
}
}

 

举报

相关推荐

0 条评论