【问题标题】:How do I make a ReorderableListView ListTile draggable on button/icon press for Flutter?如何使 ReorderableListView ListTile 在 Flutter 的按钮/图标按下时可拖动?
【发布时间】:2019-04-11 14:12:51
【问题描述】:

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

“次要: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
    );

谢谢

【问题讨论】:

    标签: flutter draggable


    【解决方案1】:

    我认为Icon(Icons.drag_handle) 只是为了在那里看起来,要将项目拖到ReorderableListView 中,您必须长按它。

    您可以使用flutter_reorderable_list 来实现这一目标。正如您在其演示中所见,此插件可以按照您想要的方式工作。

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

    如果它适合您的用例,请查看并使用它。

    【讨论】:

    • 美丽。谢谢!
    【解决方案2】:

    2021/05/29

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 2022-01-09
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      相关资源
      最近更新 更多