FadeTransition 是Flutter提供的实现透明度变换的一个Widget,需要参数 Animation
可使用 补间动画
/// 0 为完全透明, 1为完全不透明
late final Animation<double> _animation = Tween<double>(
begin: 0,
end: 1,
).animate(_controller);
或者 非线性动画
late final Animation<double> _animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
class FadeTransitionPage extends StatefulWidget {
const FadeTransitionPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _FadeTransitionPageState();
}
class _FadeTransitionPageState extends State<FadeTransitionPage>
with TickerProviderStateMixin {
/// 动画时间 3秒,可重复播放
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..repeat(reverse: true);
/// 0 为完全透明, 1为完全不透明
late final Animation<double> _animation = Tween<double>(
begin: 0,
end: 1,
).animate(_controller);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: FadeTransition(
opacity: _animation,
child: const FlutterLogo(size: 500),
),
),
);
}
}
SizeTransition 是一个裁减自身大小的动画Wdiget,可实现逐渐出现的动画效果。
注意:SizeTransition 不能限定它的大小,否则动画不会生效
所需参数与一致,需要参数 Animation
可使用 补间动画
/// 0 为完全不可见, 1为完全显示
late final Animation<double> _animation = Tween<double>(
begin: 0,
end: 1,
).animate(_controller);
或者 非线性动画
late final Animation<double> _animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
class SizeTransitionPage extends StatefulWidget {
const SizeTransitionPage({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _SizeTransitionPageState();
}
class _SizeTransitionPageState extends State<SizeTransitionPage>
with TickerProviderStateMixin {
/// 持续时间为3秒的动画控制器, forward() 设置默认直接开始播放
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 3),
vsync: this,
)..forward();
/// 补间动画,0代表完全不显示,1代表完全显示
late final Animation<double> _animation =
Tween<double>(begin: 0, end: 1).animate(_controller);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
/// axis 可控制方向(横向or竖向) axisAlignment可控制从头或尾开始显示
body: SizeTransition(
sizeFactor: _animation,
axis: Axis.vertical,
axisAlignment: -1,
child: const Center(child: FlutterLogo(size: 300)),
),
);
}
}
SizeTransition(
sizeFactor: _animation,
axis: Axis.vertical,
axisAlignment: -1, /// -1 代表 从头(此处即顶部)开始
child: const Center(child: FlutterLogo(size: 300)),
),
SizeTransition(
sizeFactor: _animation,
axis: Axis.horizontal,
axisAlignment: -1, /// -1 左侧开始出现
child: const Center(child: FlutterLogo(size: 300)),
),