我尝试使用 ReorderableListView 小部件创建一个在 Flutter 中可重新排序的列表:
return ReorderableListView(
onReorder: (int index, int targetPosition) {
print(index.toString() + " -> " + targetPosition.toString());
}
...
我找不到关于 onReorder 中的两个参数的确切解释。我找到了一些“oldIndex”、“newIndex”工作人员 - 但看起来不正确。
我构建了一个包含三个项目的列表示例。当我拖动项目时,我得到以下(让我感到困惑的)结果:
Position 1 -> Position 2 results in 0 -> 2
Position 1 -> Position 3 results in 0 -> 3
Position 3 -> Position 1 results in 2 -> 1
对我来说,它看起来像是索引和位置的混合...
也许有人知道我的错误是什么?
最佳答案
根据有关 ReorderCallback 的文档, oldIndex
和 newIndex
被传递给回调。
甚至还给出了一个示例,说明如何使用回调对 List
中的项目进行实际排序:
final List<MyDataObject> backingList = <MyDataObject>[/* ... */];
void handleReorder(int oldIndex, int newIndex) {
if (oldIndex < newIndex) {
// removing the item at oldIndex will shorten the list by 1.
newIndex -= 1;
}
final MyDataObject element = backingList.removeAt(oldIndex);
backingList.insert(newIndex, element);
}
关于flutter - Flutter ReorderableListView 中的 onReorder() 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54162721/