ReorderAbleListView.builder()未在用户输入上重新排序
我对 reoderablelistview.builder()
的期望简直就是Flutter Doc所说的,可以创建一个允许用户在列表视图上下移动/拖动列表项目的小部件。但是,我从仿真器中获得的不是拖动动画,也没有重新排序列表(用户输入),甚至没有呼叫 onreorder
callback。
我尝试过的东西:
- 确保我的
taskId
和taskName
列表列表的长度 - 与
ItemBuilder
和OnReorder
>回调,出乎意料的是,仅从itembuilder
回调的回调 - 并粘贴了窗口小部件代码及其相应的列表数据,将数据完全添加到其他小部件类(或文件),并且仍然获得完全相同的结果
valuekey
在text()
中的列表视图中。- 尝试使用与
text()
呈现的相同列表数据,tasknames
,用于值valuekey
我唯一没有尝试的是直接将此小部件的官方示例复制到我的代码库中,但是我所拥有的测试代码应该与结构上的官方示例非常相似。
清单。dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'add_task.dart';
class Checklist extends StatefulWidget {
const Checklist({Key? key}) : super(key: key);
@override
State<Checklist> createState() => _ChecklistState();
}
class _ChecklistState extends State<Checklist> {
final List<int> taskID = <int>[0, 1, 2, 4, 6];
final List<String> taskNames = <String>['A', 'B', 'C', 'D', 'E'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
children: [
const Text("TODO"),
ElevatedButton(
onPressed: () {
},
child: const Text("Google Calendar"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.black12),
),
),
]
),
),
body: ReorderableListView.builder(
itemCount: taskNames.length,
itemBuilder: (BuildContext context, int index) {
print("B");
return ListTile(
key: ValueKey(taskID[index]),
tileColor: Colors.black12,
title: Text('Entry ${taskNames[index]}')
);
},
onReorder: (int oldIndex, int newIndex) {
print("A");
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final int elTid = taskID.removeAt(oldIndex);
final String elTnm = taskNames.removeAt(oldIndex);
taskID.insert(newIndex, elTid);
taskNames.insert(newIndex, elTnm);
});
},
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码确实有效。它仅在长时间拖动上移动。不在正常的阻力上..在正常的阻力上,它会倾向于滚动列表
Your code does work. It moves only on long press and drag. Not on normal drag.. On normal drag it will tend to scroll the list
