Flutter 可排序拖放 ListView

标签 flutter listview flutter-layout flutter-animation

所以我开始学习 Flutter,并想使用 Material 设计拖放列表,就像在 Material 指南网站上看到的那样。

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

与此相比,到目前为止我尝试过的所有库看起来都像垃圾一样。是否有一个我缺少的好库或原生 Flutter 小部件?

最佳答案

enter image description here

你可以使用原生的flutter widget,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(() {});
      },
    ),
  );
}

关于Flutter 可排序拖放 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53908025/

相关文章:

flutter 表结构

dart - 如何在 flutter 中做 widget 的下拉动画?

flutter - 弹出框编辑后无法保存日期

delphi - TListView 组件中的复选框不滚动

flutter - 通过容器垂直填充 ListView 瓦片 - Flutter

android - 在列表 onItemClick 中使用 Intent

java - ListView 用完了显示字符串的空间

json - 无法在 flutter 中使用对象预填充下拉列表

flutter - 获取 Container 获取 Header 下面屏幕的剩余空间

dart - 在 Flutter 中保存项目列表的状态