'Change the value of the radio button when selected

Ive got a list of strings

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

and I've created a custom radio button file named 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),
    ],
  ),
);
}
}

I've used this radio button class and I've populated 4 radio buttons using the list mentioned above

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){}) ,
      )


    ],
  ),
);
}

and I get the output as

enter image description here

Right now we can select all 4 radio buttons at once, what I want is if I want to pick apple, the radio button with apple as the label should be true and others as false. Please help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source