英文:
adding and removing values in a flutter lists
问题
下面是您要翻译的代码部分:
class cartMock extends StatefulWidget {
const cartMock({Key? key}) : super(key: key);
@override
State<cartMock> createState() => _cartMockState();
}
class _cartMockState extends State<cartMock> {
String? _selectedFruit;
final List<String> _fruits = [];
void _addItemToList(String item) {
if (_fruits.contains(item)) {
// if the item already exists in the list, remove it
_fruits.remove(item);
}
// add the new item to the list
_fruits.add(item);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _fruits.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_fruits[index]),
);
},
),
),
RadioListTile(
value: "apple",
groupValue: _selectedFruit,
onChanged: (value) {
setState(() {
if (value != _selectedFruit) {
_selectedFruit = value as String?;
_addItemToList("apple");
}
});
},
title: const Text("Apple"),
),
RadioListTile(
value: "orange",
groupValue: _selectedFruit,
onChanged: (value) {
setState(() {
if (value != _selectedFruit) {
_selectedFruit = value as String?;
_addItemToList("orange");
}
});
},
title: const Text("Orange"),
),
],
),
);
}
}
希望这对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
so below i have a list where for every time I tap a specific radio button value of the button gets added into an empty list , so what i'm trying to do i only want one value to only ever be contained in the list , meaning if i tap a radio button and the the value is placed in the list , the next radio button i tap must push out the former radios value and newly tapped radio button's value must be the only one in the list . my code above displays both values when i tap both radios , how can i fix this ?
class cartMock extends StatefulWidget {
const cartMock({Key? key}) : super(key: key);
@override
State<cartMock> createState() => _cartMockState();
}
class _cartMockState extends State<cartMock> {
String? _selectedFruit;
final List<String> _fruits = [];
void _addItemToList(String item) {
if (_fruits.contains(item)) {
// if the item already exists in the list, remove it
_fruits.remove(item);
}
// add the new item to the list
_fruits.add(item);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _fruits.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_fruits[index]),
);
},
),
),
RadioListTile(
value: "apple",
groupValue: _selectedFruit,
onChanged: (value) {
setState(() {
if (value != _selectedFruit) {
_selectedFruit = value as String?;
_addItemToList("apple");
}
});
},
title: const Text("Apple"),
),
RadioListTile(
value: "orange",
groupValue: _selectedFruit,
onChanged: (value) {
setState(() {
if (value != _selectedFruit) {
_selectedFruit = value as String?;
_addItemToList("orange");
}
});
},
title: const Text("Orange"),
),
],
),
);
}
}
答案1
得分: 0
只需像这样终止_addItemToList
方法:
void _addItemToList(String item) {
if (_fruits.contains(item)) {
// 如果项目已经存在于列表中,请将其移除
_fruits.remove(item);
return; // <----------------- 在这里添加 return
}
// 将新项目添加到列表中
_fruits.add(item);
}
英文:
You just need to terminate _addItemToList
methode like this
void _addItemToList(String item) {
if (_fruits.contains(item)) {
// if the item already exists in the list, remove it
_fruits.remove(item);
return; <----------------- add return here
}
// add the new item to the list
_fruits.add(item);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论