如何在Flutter中的AnimatedList中更新数据(添加,删除行)?我可以在ListView中通过更新后备数据并调用来做到这一点setState
。例如,
setState(() { _data.insert(2, 'pig'); });
不过,在AnimatedList中似乎更复杂。
下面演示了更新AnimatedList的各种方法。该过程每次包括两个主要步骤:
更新数据集
通知AnimatedList的全局密钥有关更改
在索引处添加“猪” 2
。
String item = "Pig"; int insertIndex = 2; _data.insert(insertIndex, item); _listKey.currentState.insertItem(insertIndex);插入多个项目
在索引2处插入三只动物。
final items = ['Pig', 'Chichen', 'Dog']; int insertIndex = 2; _data.insertAll(insertIndex, items); // This is a bit of a hack because currentState doesn't have // an insertAll() method. for (int offset = 0; offset < items.length; offset++) { _listKey.currentState.insertItem(insertIndex + offset); }删除单项
从列表中删除“猪”。
int removeIndex = 2; String removedItem = _data.removeAt(removeIndex); // This builder is just so that the animation has something // to work with before it disappears from view since the original // has already been deleted. AnimatedListRemovedItemBuilder builder = (context, animation) { // A method to build the Card widget. return _buildItem(removedItem, animation); }; _listKey.currentState.removeItem(removeIndex, builder);删除多个项目
从列表中删除“骆驼”和“绵羊”。
int removeIndex = 2; int count = 2; for (int i = 0; i < count; i++) { String removedItem = _data.removeAt(removeIndex); AnimatedListRemovedItemBuilder builder = (context, animation) { return _buildItem(removedItem, animation); }; _listKey.currentState.removeItem(removeIndex, builder); }补充代码
主镖
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar(title: Text('Update AnimatedList data')), body: BodyWidget(), ), ); } } class BodyWidget extends StatefulWidget { @override BodyWidgetState createState() { return new BodyWidgetState(); } } class BodyWidgetState extends State{ // the GlobalKey is needed to animate the list final GlobalKey _listKey = GlobalKey(); // backing data List _data = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat']; @override Widget build(BuildContext context) { return Column( children: [ SizedBox( height: 400, child: AnimatedList( key: _listKey, initialItemCount: _data.length, itemBuilder: (context, index, animation) { return _buildItem(_data[index], animation); }, ), ), RaisedButton( child: Text( 'Insert single item', style: TextStyle(fontSize: 20), ), onPressed: () { _onButtonPress(); }, ) ], ); } Widget _buildItem(String item, Animation animation) { return SizeTransition( sizeFactor: animation, child: Card( child: ListTile( title: Text( item, style: TextStyle(fontSize: 20), ), ), ), ); } void _onButtonPress() { // replace this with method choice below _insertSingleItem(); } void _insertSingleItem() { String item = "Pig"; int insertIndex = 2; _data.insert(insertIndex, item); _listKey.currentState.insertItem(insertIndex); } void _insertMultipleItems() { final items = ['Pig', 'Chichen', 'Dog']; int insertIndex = 2; _data.insertAll(insertIndex, items); // This is a bit of a hack because currentState doesn't have // an insertAll() method. for (int offset = 0; offset < items.length; offset++) { _listKey.currentState.insertItem(insertIndex + offset); } } void _removeSingleItems() { int removeIndex = 2; String removedItem = _data.removeAt(removeIndex); // This builder is just so that the animation has something // to work with before it disappears from view since the original // has already been deleted. AnimatedListRemovedItemBuilder builder = (context, animation) { // A method to build the Card widget. return _buildItem(removedItem, animation); }; _listKey.currentState.removeItem(removeIndex, builder); } void _removeMultipleItems() { int removeIndex = 2; int count = 2; for (int i = 0; i < count; i++) { String removedItem = _data.removeAt(removeIndex); AnimatedListRemovedItemBuilder builder = (context, animation) { return _buildItem(removedItem, animation); }; _listKey.currentState.removeItem(removeIndex, builder); } } }
注意
如果您的列表项包括任何有状态的小部件,那么您将需要为它们提供键,以便系统可以跟踪它们。