英文:
Allow only single selection on RadioListTile options flutter
问题
我的RadioListTile允许多选而不是单选。
class _OptionsCardState extends State<OptionsCard> {
String? groupedValue = "";
@override
Widget build(BuildContext context) {
return Card(
color: AppColors.lightBlue,
child: RadioListTile<String>(
title: Text(widget.option),
value: "${widget.option}",
groupValue: groupedValue,
onChanged: (value) => {
setState(() {
groupedValue = value;
}),
widget.onChangeSelectedInput!(value ?? "")
}
),
);
}
}
我在另一个屏幕中这样调用:
for(var item in _questions[index].options!) OptionsCard(
option: item, color: Colors.black,
onChangeSelectedInput: (String value) {
debugPrint("selected ${value}"); //<-- this is printing the selected
value as --> {selectedValue} with the right value I only need selectedValue without the brackets
},
)
总结如何:
- 使只能选择一个选项。
- 获取所选项作为字符串,而不是{字符串}。
英文:
My radioListTile is allowing multiple selections instead of single selection.
class _OptionsCardState extends State<OptionsCard> {
String? groupedValue = "";
@override
Widget build(BuildContext context) {
return Card(
color: AppColors.lightBlue,
child: RadioListTile<String>(
title: Text(widget.option),
value: "{${widget.option}}",
groupValue: groupedValue,
onChanged: (value) => {
setState(() {
groupedValue = value;
}),
widget.onChangeSelectedInput!(value??"")
}
),
);
}
}
I am calling like this in another screen
for(var item in _questions[index].options!) OptionsCard(
option: item, color: Colors.black,
onChangeSelectedInput: (String value) {
debugPrint("selected ${value}"); <-- this is printing the selected
value as --> {selectedValue} with the right value I only need selectedValue without the brackets
},
)
Not sure what I am doing wrong..
In summary how to:
- Make only one option selectable
- Get the selected item as string instead of {string}
答案1
得分: 1
- 在共同的小部件(页面)中,你应该管理所选组值的状态。
- 在
RadioListTile
上有多余和不需要的{}
。我进行了以下更改:
OptionsCard
class OptionsCard extends StatelessWidget {
const OptionsCard({Key? key, required this.option, this.groupValue, required this.onChangeSelectedInput, this.color})
: super(key: key);
final String option;
final String? groupValue;
final Color? color;
final Function(String) onChangeSelectedInput;
@override
Widget build(BuildContext context) {
return Card(
color: AppColors.lightBlue,
child: RadioListTile<String>(
title: Text(option),
value: option,//这里你有不需要的 {},应为 '{${widget.option}}'
groupValue: groupValue,
onChanged: (value) => onChangeSelectedInput(value ?? ""),
);
}
}
PageState(使用 OptionsCard
)
for (var item in _questions[index].options!)
OptionsCard(
option: item,
color: Colors.black,
groupValue: groupValues[index],
onChangeSelectedInput: (String value) {
debugPrint("selected ${value}");
setState(() {
groupValues[index] = value;
});
},
)
在主页面状态中,你应该有一个字段变量 groupValues
Map<int, String> groupValues = {};//所有单选按钮应该有共同的组值
如果你确实希望所有问题都只能选择一个选项,那么你应该使用 String? groupValue;
而不是 groupValues
。
英文:
- You should manage the state of selected group value in common widget(page)
- You are having extra and unwanted
{}
onRadioListTile
I made following changes to achieve this
OptionsCard
class OptionsCard extends StatelessWidget {
const OptionsCard({Key? key, required this.option, this.groupValue, required this.onChangeSelectedInput, this.color})
: super(key: key);
final String option;
final String? groupValue;
final Color? color;
final Function(String) onChangeSelectedInput;
@override
Widget build(BuildContext context) {
return Card(
color: AppColors.lightBlue,
child: RadioListTile<String>(
title: Text(option),
value: option,//Here you have unwanted {} as '{${widget.option}}'
groupValue: groupValue,
onChanged: (value) => onChangeSelectedInput(value ?? "")),
);
}
}
PageState(Usage of OptionCard
)
for (var item in _questions[index].options!)
OptionsCard(
option: item,
color: Colors.black,
groupValue: groupValues[index],
onChangeSelectedInput: (String value) {
debugPrint("selected ${value}");
setState(() {
groupValues[index] = value;
});
},
)
And in the main page state, you should have a field variable groupValues
Map<int, String> groupValues = {};//Group value should be in common for all RadioButtons
If you really like to have single selection for all questions, then you should have String? groupValue;
instead of groupValues
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论