Flutter Sortable拖放ListView

时间:2018-12-23 23:45:49

标签: listview drag-and-drop flutter flutter-animation

因此,我开始学习Flutter,并希望像在材料指南网站上看到的那样使用材料设计拖放列表。

https://storage.googleapis.com/spec-host-backup/mio-design%2Fassets%2F1dtprsH4jZ2nOnjBCJeJXd7n4U-jmWyas%2F03-list-reorder.mp4

到目前为止,我尝试过的所有库看上去都像垃圾。是否有一个我缺少的良好库或本机FLutter小部件?谢谢!

5 个答案:

答案 0 :(得分:2)

选中knopp/flutter_reorderable_list。它就是这样做的。 确实很流畅,并且没有性能问题,能够处理数千个项目。

但是,它的实现并不像通常的flutter小部件那样容易。

如果您为此感到困扰,建议您使用我创建的小部件将flutter/ReorderableListView移植到knopp/ReorderableList

此小部件确实非常易于使用,但是却没有提供相同的灵活性,并且与children List一起使用时,其可伸缩性不如原始版本。

这里是code for ReorderableListSimple,现在是the demo

答案 1 :(得分:2)

Flutter本身提供了(材料)ReorderableListView class

答案 2 :(得分:1)

我尝试过flutter_reorderable_listdragable_flutter_list,但它们都无法正常工作-拖动过程中出现了一些不需要的伪像。 因此,我尝试制定自己的解决方案:

ListView.builder(
  itemBuilder: (context, index) => buildRow(index),
  itemCount: trackList.length,
),

Widget buildRow(int index) {
  final track = trackList[index];
  ListTile tile = ListTile(
    title: Text('${track.getName()}'),
  );
  Draggable draggable = LongPressDraggable<Track>(
    data: track,
    axis: Axis.vertical,
    maxSimultaneousDrags: 1,
    child: tile,
    childWhenDragging: Opacity(
      opacity: 0.5,
      child: tile,
    ),
    feedback: Material(
      child: ConstrainedBox(
        constraints:
            BoxConstraints(maxWidth: MediaQuery.of(context).size.width),
        child: tile,
      ),
      elevation: 4.0,
    ),
  );

  return DragTarget<Track>(
    onWillAccept: (track) {
      return trackList.indexOf(track) != index;
    },
    onAccept: (track) {
      setState(() {
        int currentIndex = trackList.indexOf(track);
        trackList.remove(track);
        trackList.insert(currentIndex > index ? index : index - 1, track);
      });
    },
    builder: (BuildContext context, List<Track> candidateData,
        List<dynamic> rejectedData) {
      return Column(
        children: <Widget>[
          AnimatedSize(
            duration: Duration(milliseconds: 100),
            vsync: this,
            child: candidateData.isEmpty
                ? Container()
                : Opacity(
                    opacity: 0.0,
                    child: tile,
                  ),
          ),
          Card(
            child: candidateData.isEmpty ? draggable : tile,
          )
        ],
      );
    },
  );
}

我想,这不是最好的解决方案,也许我会对其进行进一步的更改,但是目前它运行良好

答案 3 :(得分:1)

enter image description here

您可以使用本机flutter小部件ReorderableListView来实现它,下面是执行此操作的示例。

List<String> _list = ["Apple", "Ball", "Cat", "Dog", "Elephant"];

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: ReorderableListView(
      children: _list.map((item) => ListTile(key: Key("${item}"), title: Text("${item}"), trailing: Icon(Icons.menu),)).toList(),
      onReorder: (int start, int current) {
        // dragging from top to bottom
        if (start < current) {
          int end = current - 1;
          String startItem = _list[start];
          int i = 0;
          int local = start;
          do {
            _list[local] = _list[++local];
            i++;
          } while (i < end - start);
          _list[end] = startItem;
        }
        // dragging from bottom to top
        else if (start > current) {
          String startItem = _list[start];
          for (int i = start; i > current; i--) {
            _list[i] = _list[i - 1];
          }
          _list[current] = startItem;
        }
        setState(() {});
      },
    ),
  );
}

答案 4 :(得分:0)

Flutter团队推出了ReorderableListView小部件。

ReorderableListView(
      children: <Widget>[
        for (var item in appState.menuButtons) 
        Text('data')              

      ],
    )