flutter - 如何在动画开始之前和动画中间延迟动画?
问题描述
我试图在开始时和动画之间插入延迟时间,以便在延迟时间结束后,动画继续正常运行。
这是我所做的部分,这是一个带有旋转和缩放的动画。所以具体来说,我想要的是在动画开始旋转和缩放之前,它会延迟/暂停 0.3 秒。之后,当动画旋转 180 度并按比例放大时,它将再次延迟/暂停 0.3 秒。然后,它将完成最后的 180 度并按比例缩小。然后重复该过程。
动画.dart
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _rotateAnimation;
Animation<double> _scaleAnimation;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this, duration: const Duration(milliseconds: 3000));
_rotateAnimation = TweenSequence([
TweenSequenceItem(tween: Tween(begin: 2*pi, end: pi), weight: 1),
TweenSequenceItem(tween: Tween(begin: pi, end: 0.0), weight: 1)
]).animate(CurvedAnimation(parent: _animationController, curve: Curves.linear));
_scaleAnimation = TweenSequence([
TweenSequenceItem(tween: Tween(begin: 0.5, end: 1.0), weight: 1),
TweenSequenceItem(tween: Tween(begin: 1.0, end: 0.5), weight: 1),
]).animate(CurvedAnimation(parent: _animationController, curve: Curves.linear));
_animationController.repeat();
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
height: 100,
width: 100,
child: AnimatedBuilder(
animation: _animationController,
builder: (_, child) {
return Transform(
alignment: Alignment.center,
transform: Matrix4.identity()
..scale(_scaleAnimation.value)
..rotateZ(_rotateAnimation.value),
child: Container(
color: Colors.red
)
);
}
)
),
),
);
}
}
非常感谢任何帮助或建议。谢谢。
解决方案
要实现所需的动画,您需要:
- TweenSequenceItem 的 Chain() 和 ConstantTween
- 动画时 CurvedAnimation 中的间隔
像这样的东西:
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this, duration: const Duration(milliseconds: 3000));
_rotateAnimation = TweenSequence([
TweenSequenceItem(
tween: Tween(begin: 2 * pi, end: pi)
.chain(CurveTween(curve: Curves.linear)),
weight: 1),
TweenSequenceItem<double>(tween: ConstantTween<double>(pi), weight: 1),
TweenSequenceItem(
tween: Tween(begin: pi, end: 0.0)
.chain(CurveTween(curve: Curves.linear)),
weight: 1)
]).animate(
CurvedAnimation(
parent: _animationController,
curve: Interval(
0.3,
1,
curve: Curves.linear,
),
),
);
_scaleAnimation = TweenSequence([
TweenSequenceItem(
tween: Tween(begin: 0.5, end: 1.0)
.chain(CurveTween(curve: Curves.linear)),
weight: 1),
TweenSequenceItem<double>(
tween: ConstantTween<double>(1.0),
weight: 1,
),
TweenSequenceItem(
tween: Tween(begin: 1.0, end: 0.5)
.chain(CurveTween(curve: Curves.linear)),
weight: 1),
]).animate(
CurvedAnimation(
parent: _animationController,
curve: Interval(
0.3,
1,
curve: Curves.linear,
),
),
);
_animationController.repeat();
}
推荐阅读
- android - 从支持库重构为具有依赖项的 AndroidX
- loopbackjs - Loopback:如何在 afterRemote 中访问关系
- python - Keras/Tensorflow:单输出的组合损失函数
- sorting - 对 Collection 进行分页和排序
- opengl - OpenGL over Windows Remote 的当前状态和解决方案
- textbox - 根据文本长度减小字体大小
- cordova - 无法从流星科尔多瓦应用程序连接到服务器
- node.js - 如何根据用户类型使用 node/express 提供不同的 css 文件?
- javascript - ExtJS - 当用户点击行时显示新标签
- c# - 如何对等待异步任务进行单元测试?