【发布时间】:2020-06-19 22:31:49
【问题描述】:
我目前在我的 Flutter 应用程序中使用 AnimatedList,并且在删除列表项的动画方式方面存在问题。
动画本身按预期工作,但一旦删除的项目完成动画,它就会消失,导致其他小部件跳到它的位置。我曾预计其他项目会过渡到已删除项目的位置......
我尝试用ScaleTransition 包装我的列表项,但这没有帮助 - 其他列表项在完成动画之前仍然不会对删除的项做出反应。
这种方式违背了 AnimatedList 的目的吧?还是我做错了什么?关于 AnimatedList 的“本周小部件”视频清楚地表明,列表项通过更改它们的位置来对新插入的项做出反应......
这是我的代码:
@override
Widget build(BuildContext context) {
return AnimatedList(
padding: EdgeInsets.only(top: REGULAR_DIM,
bottom: REGULAR_DIM + kBottomNavigationBarHeight),
initialItemCount: data.length,
itemBuilder: (context, index, animation) {
return MyCustomWidget(
data: data[index],
animation: animation,
disabled: false
);
},
);
}
class MyCustomWidget extends AnimatedWidget {
final MyModel data;
final bool disabled;
MyCustomWidget({
@required this.data,
@required Animation<double> animation,
this.disabled = false
}) : super(listenable: animation);
Animation<double> get animation => listenable;
@override
Widget build(BuildContext context) {
final content = ... ;
return ScaleTransition(
scale: CurvedAnimation(
parent: animation,
curve: Interval(0, 0.25)
).drive(Tween(begin: 0, end: 1)),
child: FadeTransition(
opacity: animation,
child: SlideTransition(
position: animation.drive(
Tween(begin: Offset(-1, 0), end: Offset(0, 0))
.chain(CurveTween(curve: Curves.easeOutCubic))),
child: content,
),
),
);
}
}
然后在 MyCustomWidget 的某个地方调用这个函数:
void _remove(BuildContext context) async {
final animatedList = AnimatedList.of(context);
// obtain myModel asynchronously
myModel.removeData(data);
animatedList.removeItem(index, (context, animation) => MyCustomWidget(
data: data,
animation: animation,
disabled: true,
), duration: Duration(milliseconds: 350));
}
【问题讨论】: