【发布时间】:2021-03-04 01:01:44
【问题描述】:
我有一个带有 ListView.builder() & ListTile() 方法的动态列表,其中可能包含重复项,我想编辑一个项目,但我的问题是它检查列表中该值的第一次出现。
要编辑我在我的列表图块中有 longpress 属性,它会打开一个警告对话框进行编辑
例如(也可以参考图片):[a, b, c, d, a] 我想在索引 4 处编辑 'a' 让我们说 'e',但我的程序编辑了第一次出现的 a it。
final _items = List();
final TextEditingController addeditem = TextEditingController(text: ''); // for adding items
final TextEditingController editeditem = TextEditingController(text: ''); // for editing items
// I have an alert dialog where there is an input field and button to edit the
TextField(
decoration: InputDecoration(
hintText: 'Edit Item',
),
controller: editeditem,
),
TextButton(
child: Text('Done'),
onPressed: (){
_editItem(); // this calls the edit item logic
Navigator.of(context).pop();
}
)// alert dialog ends here
// edit item logic
void _editItem(){
setState(() {
int target = 0;
for(int i=0; i<index; i++){ // I am new to dart and don't know the function right now
if(_items[i] == value){ // so hard coded a for loop for checking selected item
target = i; // and replace it
break;
}
}
_items[target] = editeditem.text; // text editing controller to replace item with new value
});
问候,
【问题讨论】:
标签: android flutter listview dart