flutter - Flutter ReorderableListView 中的 onReorder() 参数
问题描述
我尝试使用 ReorderableListView 小部件在 Flutter 中创建一个可重新排序的列表:
return ReorderableListView(
onReorder: (int index, int targetPosition) {
print(index.toString() + " -> " + targetPosition.toString());
}
...
我找不到关于 onReorder 中的两个参数的确切解释。我找到了一些“oldIndex”、“newIndex”员工——但这看起来并不正确。
我已经建立了一个包含三个项目的列表的示例。当我拖动项目时,我得到以下(让我感到困惑)结果:
Position 1 -> Position 2 results in 0 -> 2
Position 1 -> Position 3 results in 0 -> 3
Position 3 -> Position 1 results in 2 -> 1
对我来说,它看起来像是索引和位置的混合......
也许有人知道我的错误是什么?
解决方案
根据有关ReorderCallback的文档,oldIndex
andnewIndex
被传递给回调。甚至还有一个关于如何使用回调来实际订购 a 中的项目的示例List
:
final List<MyDataObject> backingList = <MyDataObject>[/* ... */];
void handleReorder(int oldIndex, int newIndex) {
if (oldIndex < newIndex) {
// removing the item at oldIndex will shorten the list by 1.
newIndex -= 1;
}
final MyDataObject element = backingList.removeAt(oldIndex);
backingList.insert(newIndex, element);
}
推荐阅读
- python - TypeError: 'int' 对象不可调用 total_interest = num_payments(years * 12) * loanpayment(principal, percent, years)
- entity-relationship - 双方一对一关系的实体关系
- javascript - 如何使用 Formik Yup 验证多个电话号码
- c++ - 如何通过保存在容器中的成员指针函数调用?
- sparql - 使用 SPARQL 查询时,如何保护运行 Sesame Triplestore 的系统免受注入?
- python - 如何将变量分配给 matplotlib 图形对象并在布局中重用它
- reactjs - React Hook useEffect 缺少依赖项:'execute'
- javascript - Google Apps 脚本:将 Gmail 附件保存到 Google Drive
- python - HTTP GET API 设计,它使用相同的端点,但在 Flask 中提供带有请求参数的过滤结果
- javascript - 用于时刻日期的自定义 js-data 格式化程序