【问题标题】:Change the value of the radio button when selected选中时更改单选按钮的值
【发布时间】:2022-02-08 17:58:23
【问题描述】:

我有一个字符串列表

List<String> answerOptions=['Apple','Orange','Grapes','Kiwi'];

我创建了一个名为 QuizRa​​dioButton 的自定义单选按钮文件

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


    【解决方案1】:
    int groupVal=0;
    

    实施:

    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){
                  setState(() {
                    groupVal=value;
                  });
                }, index: i, groupVal: groupVal) ,
              )
    
          ],
        ),
      ),
    }
    

    您的 QuizRa​​dioButton:

    class QuizRadioButton extends StatefulWidget {
      final String label;
      final void Function(dynamic) onChanged;
      int index,groupVal;
    
      QuizRadioButton(
          {required this.label, required this.groupVal, required this.onChanged, required this.index, Key? key})
          : super(key: key);
    
      @override
      _QuizRadioButtonState createState() => _QuizRadioButtonState();
    }
    
    class _QuizRadioButtonState extends State<QuizRadioButton> {
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.all(16.0),
          child: Row(
            children: [
              Radio<int>(
                value: widget.index,
                groupValue: widget.groupVal,
                onChanged: widget.onChanged,
              ),
              Text(widget.label, style: Theme.of(context).textTheme.headline3),
            ],
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2015-05-24
      • 2021-08-05
      相关资源
      最近更新 更多