我已经编码了一个ListView(父亲),其中包含一些listViews(孩子)。架构如下所示:
我添加了Dismissible
来删除子listView项目。但是出了点问题。
我删除了某些项目后,子级ListView项目的索引没有更新。因此错误出现如下:
父亲listView的代码如下:
@override
Widget build(BuildContext context) {
// TODO: implement build
return Consumer<Data>(
builder: (context, Data dataRecord, _){
return Expanded(
child: ReorderableListView(
children: widget.dataList.map((item) =>
ChildListView(dataItem: item,),
).toList(),
onReorder: (int oldIndex, int newIndex) {
print("oldIndex: $oldIndex, newIndex: $newIndex");
setState(() {
var tmpItem = widget.dataList.remove(oldIndex);
widget.dataList.insert(newIndex, tmpItem);
});
},
),
);
}
);
子ListView的代码如下:
@override
Widget build(BuildContext context) {
return ListView.builder(
controller: _scrollController,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
padding: const EdgeInsets.all(10.0),
itemCount: widget.items.length,
itemBuilder: (BuildContext context, int index) {
var _actionSet = widget.items[index];
return Dismissible(
key: ValueKey(widget.items[index].id.v1()),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
print("itemcount: ${widget.items.length}");
print("index is: $index");
_deleteItem(index);
},
confirmDismiss: _confirmDismiss,
background: Container(
color: Colors.red,
child: ListTile(
trailing: Icon(Icons.delete, color: Colors.white),
),
),
child: ExeRoutineDetailExeListRow(actionSet: _actionSet, index: index,),
);
});
}
void _deleteItem(int index) {
print("===>>>removeAt:$index");
widget.items.remove(index);
}
//It's items remove function
// the Class Items with the ChangeNotifier
item remove(int index) {
var tmpItem = items.removeAt(index);
notifyListeners();
return tmpItem;
}
希望您的帮助,非常感谢。
答案 0 :(得分:0)
问题没问题。关键是我没有{<1>可关闭”
中的setState()
return Dismissible(
key: ValueKey(widget.items[index].id.v1()),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
print("itemcount: ${widget.items.length}");
print("index is: $index");
setState((){_deleteItem(index);}); // <-- Add this line
},
confirmDismiss: _confirmDismiss,
);
希望获得帮助!