void insertItem(int index, { Duration duration = _kDuration }); void removeItem(int index, AnimatedListRemovedItemBuilder builder, { Duration duration = _kDuration }) ;
class HomePage extends StatefulWidget { const HomePage({super.key}); @override State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { final globalKey = GlobalKey<AnimatedListState>(); List<String> list = ["第一条数据", "第二条数据"]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Title'), ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { list.add("这是一个新增数据"); globalKey.currentState!.insertItem(list.length - 1); //索引值为n-1 }); }, child: Icon(Icons.abc), ), body: AnimatedList( key: globalKey, initialItemCount: list.length, itemBuilder: (context, index, animation) { return FadeTransition( opacity: animation, child: ListTile( title: Text(list[index]), trailing: Icon(Icons.delete)), ); }), ); } }
class HomePage extends StatefulWidget { const HomePage({super.key}); @override State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { final globalKey = GlobalKey<AnimatedListState>(); List<String> list = ["第一条数据", "第二条数据"]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Title'), ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { list.add("这是一个新增数据"); globalKey.currentState!.insertItem(list.length - 1); //索引值为n-1 }); }, child: Icon(Icons.abc), ), body: AnimatedList( key: globalKey, initialItemCount: list.length, itemBuilder: (context, index, animation) { return ScaleTransition( scale: animation, child: ListTile( title: Text(list[index]), trailing: Icon(Icons.delete)), ); }), ); } }
class HomePage extends StatefulWidget { const HomePage({super.key}); @override State<HomePage> createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { final globalKey = GlobalKey<AnimatedListState>(); bool flag = true; List<String> list = ["第一条数据", "第二条数据"]; //删除 Widget _buildItem(context, index) { return ListTile( key: ValueKey(index), title: Text(list[index]), //显示的数据 trailing: IconButton( icon: Icon(Icons.delete), //删除的图标 // 点击时删除 onPressed: () => _deleteItem(context, index), )); } _deleteItem(context, index) { if (flag == true) { flag = false; print(index); //注意:删除后需要重新setState setState(() { // 删除过程执行的是反向动画,animation.value 会从1变为0 globalKey.currentState!.removeItem( index, (context, animation) { //执行删除 //注意先build然后再去删除 var item = _buildItem(context, index); list.removeAt(index); //数组中删除数据 return FadeTransition( //动画 opacity: animation, child: item, //执行动画的元素 ); }, ); }); //解决快速删除bug 重置flag const timeout = Duration(milliseconds: 2000); //每2秒才删除一个 Timer.periodic(timeout, (timer) { flag = true; timer.cancel(); }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Title'), ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { list.add("这是一个新增数据"); globalKey.currentState!.insertItem(list.length - 1); //索引值为n-1 }); }, child: const Icon(Icons.abc), ), body: AnimatedList( key: globalKey, initialItemCount: list.length, itemBuilder: (context, index, animation) { return ScaleTransition( scale: animation, child: _buildItem(context, index), ); }), ); } }