在Flutter中一个List可以同时被两个AnimatedListView使用吗?修改List之后数据之后AnimatedListView会更新数据吗?该如何做呢?
可以同时被两个AnimatedListView使用,但是需要保证它们都在同一个页面上。
当你修改List数据之后,你需要调用setState()方法,刷新页面,这样AnimatedListView才能更新数据。具体做法如下:
- 创建一个List,例如:
List<String> itemList = ['item1', 'item2', 'item3'];
- 在页面中创建两个AnimatedListView,分别使用itemList作为数据源,例如:
AnimatedListView(
itemCount: itemList.length,
itemBuilder: (context, index, animation) {
return Text(itemList[index]);
},
);
AnimatedListView(
itemCount: itemList.length,
itemBuilder: (context, index, animation) {
return Text(itemList[index]);
},
);
- 当你修改itemList数据时,需要调用setState()方法,例如:
setState(() {
itemList.add('item4');
});
这样,当你添加一个新的item到itemList时,两个AnimatedListView都会更新数据。
原文地址: https://www.cveoy.top/t/topic/sHw 著作权归作者所有。请勿转载和采集!