[英]Flutter ReorderableListView with custom object
I am currently having a few difficulties with ReorderableListView.我目前在使用 ReorderableListView 时遇到了一些困难。
I have a list of ToDoPrefabs, which I want to output, because my list does not consist of strings but something else, I get errors in the index, for the key and the text.我有一个 ToDoPrefabs 列表,我想要 output,因为我的列表不包含字符串而是其他内容,我在索引中遇到错误,对于键和文本。
this is the error:这是错误:
"type 'To Prefab' is not a subtype of type 'int'" “类型'To Prefab'不是'int'类型的子类型”
I suspect that this is the key.我怀疑这是关键。 It is also logical since todoprefab is not an int.
这也是合乎逻辑的,因为 todoprefab 不是 int。 Does anyone have an idea how I can get another index of the type int in my ReorderableListView
有谁知道如何在我的 ReorderableListView 中获得另一个 int 类型的索引
This is my ReorderableListView这是我的 ReorderableListView
return ReorderableListView(
onReorder: _onReorder,
scrollDirection: Axis.vertical,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: [
for(final item in toDoManagerClass.todoBank)
todocardprefab(item)
]
);
the todocardprefab (not the list) todocardprefab(不是列表)
todocardprefab(var key){
return Dismissible(
key: ValueKey(key),
background: Container(color: Colors.red, alignment: Alignment.centerLeft, child: Icon(Icons.delete),),
secondaryBackground: Container(color: Colors.green, alignment: Alignment.centerRight, child: Icon(Icons.archive),),
child: Card(
child: ListTile(
//leading: Icon(Icons.check_box_outline_blank),
title: Text(toDoManagerClass.todoBank[key].toDoText),
onTap: (){},
),
),
);
}
and this is the simple list.这是简单的列表。
class ToDoManagerClass{
List<ToDoPrefab> todoBank = [
ToDoPrefab(text: 'Homework', f: false),
ToDoPrefab(text: 'Homework1', f: false),
ToDoPrefab(text: 'Homework12', f: false),
ToDoPrefab(text: 'Homework123', f: false),
ToDoPrefab(text: 'Homework1234', f: false),
ToDoPrefab(text: 'Homework12345', f: false),
];
}
This method should work:这种方法应该有效:
Your reordable listview您的可重新排序列表视图
return ReorderableListView(
onReorder: _onReorder,
scrollDirection: Axis.vertical,
padding: const EdgeInsets.symmetric(vertical: 8.0),
children: [
for(final item in toDoManagerClass.todoBank)
todocardprefab(item, index())
]
);
and here the function.这里是 function。 It returns a number higher with each call.
每次调用都会返回一个更高的数字。
int index(){
if(i < toDoManagerClass.todoBank.length){
print(i);
i++;
}
return i-1;
}
now you only have to use the index instead of the item in your todocard现在您只需要使用索引而不是 todocard 中的项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.