如何在 Flutter 中添加两个具有不同持续时间的不同动画?
How to add two different animation with different duration in Flutter?
我有以下代码动画任何 widget
import 'dart:async';
import 'package:flutter/material.dart';
class AnimElasticOut extends StatefulWidget {
final Widget child;
final int delay;
final Key key;
final startAnimation;
AnimElasticOut(
{@required this.key,
@required this.child,
this.delay,
this.startAnimation});
@override
AnimElasticOutState createState() => AnimElasticOutState();
}
class AnimElasticOutState extends State<AnimElasticOut>
with TickerProviderStateMixin {
AnimationController controller;
Animation<double> animation;
int duration = 1000;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this, duration: Duration(milliseconds: duration));
animation = CurvedAnimation(
parent: controller,
curve: Curves.elasticOut,
);
if (widget.startAnimation != null) {
if (widget.delay == null) {
controller.forward();
} else {
Timer(Duration(milliseconds: widget.delay), () {
controller.forward();
});
}
}
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: widget.child,
scale: animation,
);
}
}
示例用法
AnimElasticOut(
key: counterElasticOutKey,
child: Container(
height: 35,
padding: EdgeInsets.only(
left: 5, top: 5, bottom: 5, right: 15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.add_alarm,
color: Colors.amber,
),
SizedBox(
width: 10,
),
Counter(
key: counterKey,
),
],
),
),
),
我使用以下代码启动动画。
counterElasticOutKey.currentState.controller.forward(from: 0);
现在它可以很好地为小部件设置动画。
我还有一个反转动画的代码
counterElasticOutKey.currentState.controller.reverse();
我想要的是在倒车时使用另一个动画。此外,还有一些其他持续时间。
例如,我想要 Curves.easeInOutCubic
作为持续时间为 milliseconds: 500
的动画曲线
我该怎么做?
AnimationController
中有属性reverseDuration
,Animation<T>
中有reverseCurve
controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
reverseDuration: Duration(milliseconds: 250),
);
animation = CurvedAnimation(
parent: controller,
curve: Curves.elasticOut,
reverseCurve: Curves.easeInQuad,
);
我有以下代码动画任何 widget
import 'dart:async';
import 'package:flutter/material.dart';
class AnimElasticOut extends StatefulWidget {
final Widget child;
final int delay;
final Key key;
final startAnimation;
AnimElasticOut(
{@required this.key,
@required this.child,
this.delay,
this.startAnimation});
@override
AnimElasticOutState createState() => AnimElasticOutState();
}
class AnimElasticOutState extends State<AnimElasticOut>
with TickerProviderStateMixin {
AnimationController controller;
Animation<double> animation;
int duration = 1000;
@override
void initState() {
super.initState();
controller = AnimationController(
vsync: this, duration: Duration(milliseconds: duration));
animation = CurvedAnimation(
parent: controller,
curve: Curves.elasticOut,
);
if (widget.startAnimation != null) {
if (widget.delay == null) {
controller.forward();
} else {
Timer(Duration(milliseconds: widget.delay), () {
controller.forward();
});
}
}
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: widget.child,
scale: animation,
);
}
}
示例用法
AnimElasticOut(
key: counterElasticOutKey,
child: Container(
height: 35,
padding: EdgeInsets.only(
left: 5, top: 5, bottom: 5, right: 15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.add_alarm,
color: Colors.amber,
),
SizedBox(
width: 10,
),
Counter(
key: counterKey,
),
],
),
),
),
我使用以下代码启动动画。
counterElasticOutKey.currentState.controller.forward(from: 0);
现在它可以很好地为小部件设置动画。
我还有一个反转动画的代码
counterElasticOutKey.currentState.controller.reverse();
我想要的是在倒车时使用另一个动画。此外,还有一些其他持续时间。
例如,我想要 Curves.easeInOutCubic
作为持续时间为 milliseconds: 500
我该怎么做?
AnimationController
中有属性reverseDuration
,Animation<T>
中有reverseCurve
controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
reverseDuration: Duration(milliseconds: 250),
);
animation = CurvedAnimation(
parent: controller,
curve: Curves.elasticOut,
reverseCurve: Curves.easeInQuad,
);