【问题标题】:onReorder() arguments in Flutter ReorderableListViewFlutter ReorderableListView 中的 onReorder() 参数
【发布时间】:2019-06-07 07:58:05
【问题描述】:

我尝试使用 ReorderableListView 小部件在 Flutter 中创建一个可重新排序的列表

return ReorderableListView(
    onReorder: (int index, int targetPosition) {
        print(index.toString() + " -> " + targetPosition.toString());
    }
...

我找不到关于 onReorder 中的两个参数是什么的确切解释。我找到了一些“oldIndex”、“newIndex”员工——但这看起来不正确。

我已经构建了一个包含三个项目的 List 示例。当我拖动项目时,我得到以下(让我感到困惑)结果:

Position 1 -> Position 2 results in 0 -> 2
Position 1 -> Position 3 results in 0 -> 3
Position 3 -> Position 1 results in 2 -> 1

对我来说,它看起来像是索引和位置的混合......

也许有人知道我的错误是什么?

【问题讨论】:

    标签: flutter dart reorderable-list


    【解决方案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);
    }
    

    【讨论】:

    • 是否可以在可重新排序的列表视图中实现refreshindicator?
    • RefreshIndicator 应该适用于任何 Scrollable 小部件,所以是的,您应该能够将可重新排序的列表视图包装在 RefreshIndicator 中。
    • 感谢您的回复,我尝试用RefreshIndicator 包装我的可重新排序的列表视图,它正在工作,但是当列表没有滚动的最小数据时RefreshIndicator 没有刷新我的可重新排序列表。
    【解决方案2】:

    是新旧索引,但都有问题。如您所见here,有一些未解决的问题。

    这是一个展示如何使用它们的示例:

    onReorder: (oldIndex, newIndex) {
      setState(() {
        // These two lines are workarounds for ReorderableListView problems
        if (newIndex > _sampleItems.length) newIndex = _sampleItems.length;
        if (oldIndex < newIndex) newIndex--;
    
        MyItem item = _sampleItems[oldIndex];
        _sampleItems.remove(item);
        _sampleItems.insert(newIndex, item);
      });
    },
    

    你可以看到一个演示here in this snippet

    根据我的经验(2019/1 月),我放弃了 Flutter 的 ReorderableListView 并开始使用knopp/flutter_reorderable_list。我什至做了一个Widget to make that switch easier,看看它对你是否有意义。

    【讨论】:

      【解决方案3】:

      onReorder 函数在大多数教程中都写错了。我认为我刚刚写的这个是有效的。

      void _onReorder(int oldIndex, int newIndex) {
          setState(() {
            newIndex > oldIndex ? (){newIndex--; int row = V755.removeAt(oldIndex);
            V755.insert(newIndex, row);}() : (){int row = V755.removeAt(oldIndex);V755.insert(newIndex, row);}();
          });
      }
      

      【讨论】:

      • V755 是一个列表顺便说一句,对不起
      猜你喜欢
      • 2022-11-15
      • 2021-08-08
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 2020-08-02
      • 1970-01-01
      • 2021-03-07
      相关资源
      最近更新 更多