Flutter 颤振:在对列表元素排序时,如何显示动画?
我正在构建一个小的Flutter 颤振:在对列表元素排序时,如何显示动画?,flutter,dart,flutter-animation,Flutter,Dart,Flutter Animation,我正在构建一个小的AnimatedListdemo,用户可以在其中添加/删除/排序列表,但我被困在排序部分。当使用sort函数对列表进行排序时,它会对列表进行排序,缩短列表并更改UI,但不会显示动画。 这是代码 class Home extends StatefulWidget { @override _HomeState createState() => _HomeState(); } class _HomeState extends State<Home> wi
AnimatedList
demo,用户可以在其中添加/删除/排序列表,但我被困在排序部分。当使用sort函数对列表进行排序时,它会对列表进行排序,缩短列表并更改UI,但不会显示动画。
这是代码
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
final _key = GlobalKey<FormState>();
final listKey = GlobalKey<AnimatedListState>();
List<String> text = ['banana', 'apple', 'peach', 'mangoes', 'adsd'];
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: tapAnywhere,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green[400],
title: Text('Animated List Demo'),
actions: [
IconButton(
icon: Icon(Icons.sort),
onPressed: () {
listKey.currentState.setState(() {
text = List.from(text) //<---sorting the list
..sort();
});
},
)
],
),
body: Form(
key: _key,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Column(
children: [
(other code...),
AnimatedList( //<--animated list
key: listKey,
initialItemCount: text.length,
shrinkWrap: true,
physics: ScrollPhysics(),
itemBuilder: (context, index, animation) {
return GestureDetector(
onTap: () => removeElement(index),
child: TextCard(
animation: animation,
text: text.elementAt(index),
),
);
}),
],
),
),
),
),
);
}
}
类Home扩展了StatefulWidget{
@凌驾
_HomeState createState()=>\u HomeState();
}
类_HomeState使用SingleTickerProviderStateMixin扩展状态{
final_key=GlobalKey();
最终列表键=GlobalKey();
列表文本=[“香蕉”、“苹果”、“桃子”、“芒果”、“adsd”];
@凌驾
小部件构建(构建上下文){
返回手势检测器(
onTap:tapAnywhere,
孩子:脚手架(
appBar:appBar(
背景颜色:颜色。绿色[400],
标题:文本(“动画列表演示”),
行动:[
图标按钮(
图标:图标(Icons.sort),
已按下:(){
listKey.currentState.setState((){
text=List.from(文本)//