flutter - Flutter ReorderableListView 中的 onReorder() 参数

标签 flutter dart reorderable-list

我尝试使用 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 的文档, oldIndexnewIndex 被传递给回调。 甚至还给出了一个示例,说明如何使用回调对 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/

相关文章:

android-studio - 使用 Android Studio 自动在右括号后插入注释

flutter - Flutter:ReorderableListView更改项目宽度,并忽略圆角边框

android - Flutter - 带有嵌套在 expansiontile 中的 Listview 的可重新排序列表

flutter - 在 Flutter 中恢复互联网连接时获取数据

使用 .aar 模块的 Flutter 插件在示例应用程序中构建和运行良好,但无法在其他应用程序中构建

flutter - 如何在flutter中找到map的最大值的key?

flutter - 自定义 flutter 工具提示

flutter - 如何在 android studio 的注释行中添加代码引用?

angular - 如何在 Angular Dart 中使用带有 ngModel 的自定义数据类型