我目前在我的Flutter应用中使用AnimatedList,但是我遇到了有关已删除的列表项退出动画方式的问题。虽然动画本身像预期的那样工作,但一旦已删除的项目完成动画,它就会消失,导致其他小部件跳入其位置。我原本希望其他项目过渡到已删除的项目的位置...
我尝试将列表项包装在
这有点违背AnimatedList的目的,对吗?还是我做错了什么? "Widget of the week"关于AnimatedList的视频清楚地显示,列表项通过更改其位置对新插入的项做出反应...
这是我的代码:
我尝试将列表项包装在
ScaleTransition
中,但这并没有帮助-其他列表项仍然不会对已删除的项目做出反应,直到它完成动画为止。这有点违背AnimatedList的目的,对吗?还是我做错了什么? "Widget of the week"关于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));
}