ScaleTransition 是Flutter提供的实现按尺寸比例缩放的一个动画Widget,需要参数 Animation
可使用 补间动画
/// 缩小至 0.2倍大小,放大至3倍大小
late final Animation<double> _animation =
Tween<double>(begin: 0.2, end: 3).animate(_controller);
或者 非线性动画
late final Animation<double> _animation =
Tween<double>(begin: 0.2, end: 3).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInCirc,
),
);
class ScaleTransitionPage extends StatefulWidget {
const ScaleTransitionPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _ScaleTransitionPageState();
}
class _ScaleTransitionPageState extends State<ScaleTransitionPage>
with TickerProviderStateMixin {
/// 动画控制器,动画持续时间5秒,可重复播放
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 5),
vsync: this,
)..repeat(reverse: true);
/// 缩小至 0.2倍大小,放大至3倍大小 非线性动画
late final Animation<double> _animation =
Tween<double>(begin: 0.2, end: 3).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInCirc,
),
);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ScaleTransition(
scale: _animation,
child: const FlutterLogo(size: 150.0),
),
),
);
}
}
DecoratedBoxTransition 是BoxDecoration的动画实现,可实现Widget的装饰属性 如形状、颜色、阴影等变换效果。需要参数 Tween
class DecoratedBoxTransitionPage extends StatefulWidget {
const DecoratedBoxTransitionPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _DecoratedBoxTransitionPageState();
}
class _DecoratedBoxTransitionPageState extends State<DecoratedBoxTransitionPage>
with TickerProviderStateMixin {
/// 动画控制器,设置动画持续时间为5秒,可重复播放
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 3),
)..repeat(reverse: true);
/// 在两种不同的装饰属性中变换,从圆形变成方形,红色变成白色背景,无阴影变成有阴影
late final Animation<Decoration> _animation = DecorationTween(
begin: BoxDecoration(
color: Colors.red,
border: Border.all(style: BorderStyle.none),
borderRadius: BorderRadius.circular(100),
boxShadow: null,
),
end: BoxDecoration(
color: Colors.white,
border: Border.all(style: BorderStyle.none),
borderRadius: BorderRadius.zero,
boxShadow: const <BoxShadow>[
BoxShadow(
color: Colors.grey,
blurRadius: 10.0,
spreadRadius: 3.0,
offset: Offset(0, 6.0),
)
],
),
).animate(_controller);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: DecoratedBoxTransition(
decoration: _animation,
child: Container(
width: 200,
height: 200,
padding: const EdgeInsets.all(20),
child: const FlutterLogo(),
),
),
),
);
}
}