所以我开始学习 Flutter,并想使用 Material 设计拖放列表,就像在 Material 指南网站上看到的那样。
与此相比,到目前为止我尝试过的所有库看起来都像垃圾一样。是否有一个我缺少的好库或原生 Flutter 小部件?
最佳答案
你可以使用原生的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/