当子组件变化时执行动画,需要指定子组件的key进行标识。动画方式可以自定义,能指定动画时长、动画曲线等属性。
相关组件
AnimatedSwitcher基本使用
<br />【child】 : 孩子组件 【Widget】<br />【duration】 : 动画时长 【Duration】<br />【switchOutCurve】 : 切出曲线 【Curves】<br />【switchInCurve】 : 切入曲线 【Curves】<br />【switchInCurve】 : 切入曲线 【Curves】<br />【transitionBuilder】 : 动画构造器 【Widget Function(Widget, Animation<double>)】<br />![108.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589449310712-630a1245-5047-4df5-ab20-560687248523.gif#align=left&display=inline&height=119&margin=%5Bobject%20Object%5D&name=108.gif&originHeight=119&originWidth=403&size=164995&status=done&style=none&width=403)
import 'package:flutter/material.dart';
class CustomAnimatedSwitcher extends StatefulWidget {
@override
_CustomAnimatedSwitcherState createState() => _CustomAnimatedSwitcherState();
}
class _CustomAnimatedSwitcherState extends State<CustomAnimatedSwitcher> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Container(
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: <Widget>[
_buildMinusBtn(),
SizedBox(width:80,child: _buildAnimatedSwitcher(context)),
_buildAddBtn()
],
),
);
}
Widget _buildAnimatedSwitcher(BuildContext context) =>
AnimatedSwitcher(
duration: const Duration(milliseconds: 400),
transitionBuilder: (Widget child, Animation<double> animation) =>
ScaleTransition(
child: RotationTransition(turns: animation, child: child),
scale: animation),
child: Text(
'$_count',
key: ValueKey<int>(_count),
style: Theme.of(context).textTheme.display3,
),
);
Widget _buildMinusBtn() {
return MaterialButton(
padding: EdgeInsets.all(0),
textColor: Color(0xffFfffff),
elevation: 3,
color: Colors.red,
highlightColor: Color(0xffF88B0A),
splashColor: Colors.red,
child: Icon(
Icons.remove,
color: Colors.white,
),
shape: CircleBorder(
side: BorderSide(width: 2.0, color: Color(0xFFFFDFDFDF)),
),
onPressed: () => setState(() => _count -= 1));
}
Widget _buildAddBtn() => MaterialButton(
padding: EdgeInsets.all(0),
textColor: Color(0xffFfffff),
elevation: 3,
color: Colors.blue,
highlightColor: Color(0xffF88B0A),
splashColor: Colors.red,
child: Icon(
Icons.add,
color: Colors.white,
),
shape: CircleBorder(
side: BorderSide(width: 2.0, color: Color(0xFFFFDFDFDF)),
),
onPressed: () => setState(() => _count += 1));
}