单选按钮未更改

时间:2019-06-23 00:32:39

标签: flutter

RadioButton仅设置为其中一个且未更改

Radio<int>(activeColor: Colors.orange,value: 0, groupValue: 1,onChanged: 
HandleRadio)
,Radio<int>(activeColor: Colors.amber,value: 1, groupValue: 1, onChanged: 
HandleRadio)
,Radio<int>(activeColor: Colors.blue, value: 2, groupValue: 1, onChanged: 
HandleRadio)

 //function
int RadioValue = 0;
void HandleRadio(int value){
   setState(() {
     RadioValue = value;
   });
 }

应将其设置为选定的电台

1 个答案:

答案 0 :(得分:1)

那是因为您为groupValue赋予了Radio硬编码值

这是工作代码。

Radio<int>(
  activeColor: Colors.orange,
  value: 0,
  groupValue: radioValue, // changed this
  onChanged: handleRadio,
),
Radio<int>(
  activeColor: Colors.amber,
  value: 1,
  groupValue: radioValue, // changed this 
  onChanged: handleRadio,
),
Radio<int>(
  activeColor: Colors.blue,
  value: 2,
  groupValue: radioValue, // changed this
  onChanged: handleRadio,
),


//function
int radioValue = 0;

void handleRadio(int value) {
  setState(() {
    radioValue = value;
  });
}