Flutter基建 - 12种隐式动画小组件全解析
在Flutter中,隐式动画是通过小部件的动画化来实现的,而不是显式地编写动画代码。Flutter提供了一些内置的动画小部件,如AnimatedContainer
、SlideTransition
、FadeTransition
等,可以用来创建隐式动画。
以下是12种隐式动画小部件的简单示例:
AnimatedContainer
:
AnimatedContainer(
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
width: size,
height: size,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: color,
),
child: child,
)
SlideTransition
:
SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, 1),
end: const Offset(0, 0),
).animate(animation),
child: child,
)
FadeTransition
:
FadeTransition(
opacity: animation,
child: child,
)
RotationTransition
:
RotationTransition(
turns: Tween<double>(begin: 0, end: 1).animate(animation),
child: child,
)
ScaleTransition
:
ScaleTransition(
scale: Tween<double>(begin: 0, end: 1).animate(animation),
child: child,
)
AnimatedSwitcher
:
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
return FadeTransition(opacity: animation, child: child);
},
child: icon,
)
AnimatedList
:
AnimatedList(
key: listKey,
initialItemCount: items.length,
itemBuilder: (context, index, animation) {
return SlideTransition(
position: animation.drive(tween),
child: ListItem(
item: items[index],
onDismissed: () {
setState(() {
items.removeAt(index);
});
},
),
);
},
)
ImplicitlyAnimatedListState
:
ImplicitlyAnimatedList<int>(
items: List<int>.generate(1000, (i) => i),
itemBuilder: (context, index, animation) {
return SizeTransition(
sizeFactor: animation,
child: ListItem(item: index),
);
},
)
AnimatedCrossFade
:
AnimatedCrossFade(
firstChild: firstWidget,
secondChild: secondWidget,
crossFadeState: crossFadeState,
duration: const Duration(milliseconds: 300),
)
TweenAnimationBuilder
:
TweenAnimationBuilder(
tween: Tween<double>(begin: 0, end: 1),
duration: const Duration(seconds: 2),
builder: (BuildContext context, double value, Widget child) {
return Transform.translate(
offset: Offset.lerp(const Offset(0, 100), const Offset(0, 0), value),
评论已关闭