【发布时间】:2022-02-08 17:58:23
【问题描述】:
我有一个字符串列表
List<String> answerOptions=['Apple','Orange','Grapes','Kiwi'];
我创建了一个名为 QuizRadioButton 的自定义单选按钮文件
class QuizRadioButton extends StatefulWidget {
final String label;
final void Function(dynamic) onChanged;
const QuizRadioButton(
{required this.label, required this.onChanged, Key? key})
: super(key: key);
@override
_QuizRadioButtonState createState() => _QuizRadioButtonState();
}
class _QuizRadioButtonState extends State<QuizRadioButton> {
int? _value = 0;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
children: [
Radio<int>(
value: 1,
groupValue: _value,
onChanged: (value) => setState(() => _value = value),
),
Text(widget.label, style: Theme.of(context).textTheme.headline3),
],
),
);
}
}
我使用过这个单选按钮类,并使用上面提到的列表填充了 4 个单选按钮
Widget content(BuildContext context){
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Text('which fruit of these are red in color ?'),
SizedBox(height: 30.0,),
for(int i=0;i<answerOptions.length;i++)Container(
child:QuizRadioButton(label: answerOptions[i], onChanged: (value){}) ,
)
],
),
);
}
我得到的输出为
现在我们可以一次选择所有 4 个单选按钮,我想要的是如果我要选择苹果,以苹果为标签的单选按钮应该为 true,其他为 false。请帮忙
【问题讨论】:
标签: flutter dart radio-button radio-group