如何使Flutter上的按钮/图标按下可拖动的ReorderableListView ListTile?

时间:2018-11-08 04:16:36

标签: flutter draggable

所以我看到了ReorderableListView demo,看到他们有

“ secondary:const Icon(Icons.drag_handle)”

但是在查看reorderable_list.dart文件时,我注意到整个列表仍然可以在LongPressDraggable上拖动[第424行]。那么,如何明确对源代码或我自己的代码进行更改,以正确地使图标成为实际的拖动手柄?

CheckboxListTile(
      key: Key(item.value),
      isThreeLine: true,
      value: item.checkState ?? false,
      onChanged: (bool newValue) {
        setState(() {
          item.checkState = newValue;
        });
      },
      title: Text('This item represents ${item.value}.'),
      subtitle: secondary,
      secondary: const Icon(Icons.drag_handle),  // Make this Icon drag source
    );

谢谢

2 个答案:

答案 0 :(得分:1)

我认为twomics仅用于那里的外观,将项目拖到Icon(Icons.drag_handle)中,您必须长按它。

您可以使用flutter_reorderable_list来实现。正如您在其演示中看到的那样,该插件可以按您想要的方式工作。

但是,它与ReorderableListView的工作方式大不相同,因此代码更改可能会让人有些不知所措。我创建了一个小部件来简化该开关widget is here及其demo is here

看看并在适合您的用例的情况下使用它。

答案 1 :(得分:1)

2021/05/29

作为答案的更新,ReorderableListView 已经有一个可自定义的处理程序:

<块引用>

最近重构了 ReorderableListView(PR:#74299 和 #74697),我们在桌面上运行时添加了自动拖动手柄(使用 buildDefaultDragHandles 属性将其关闭)。如果这不是您想要的,您可以将自己的拖​​动手柄作为小部件添加到您的每个项目中,例如:

ReorderableDragStartListener(
  index: index,
  child: const Icon(Icons.drag_handle),
),

您可以在此处查看详细信息:https://github.com/flutter/flutter/issues/46805