gpt4 book ai didi

Flutter 可排序拖放 ListView

转载 作者:IT老高 更新时间:2023-10-28 12:33:50 24 4
gpt4 key购买 nike

所以我开始学习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com