Home > database >  Flutter - Radio button value is not getting selected
Flutter - Radio button value is not getting selected

Time:06-14

I have a radio buttons created but while on click of the buttons the value is not getting changed.

here is my code.

Widget _buildOmsIDTextField() {

// Group Value for Radio Button.
int id = 1;

List<OptionList> nList = [
  OptionList(
    padding : const EdgeInsets.fromLTRB(14.0, 180, 25, 50),
    index: 1,
    option: "Accept",
  ),
  OptionList(
    padding : const EdgeInsets.all(14.0),
    index: 2,
    option: "Reject",
  ),


];
return Column(
  children: <Widget>[
    const Padding(
padding : EdgeInsets.all(14.0),
        child: Text("Please Select an Option", style: TextStyle(fontSize: 23))
    ),

         SizedBox(
           height: 150.0,
          child: Column(
            children:
            nList.map((data) => RadioListTile(
              title: Text(data.option),
              groupValue: data.index,
              value: id,
              onChanged: (val) {
                setState(() {
                 radioItemHolder = data.option ;
                  id = data.index;
                });
              },
            )).toList(),
          ),
        ),
  ],
);
   }

Any help would be highly appreciated!

CodePudding user response:

While you are calling _buildOmsIDTextField() inside build method, the int id = 1; will always get 1. Put your group value outside the build method. And use same group value on radio buttons.

// Group Value for Radio Button. I also prefer putting any decleared varable for radio button here 
int id = 1;

Widget _buildOmsIDTextField() {... 
  ... 
     nList.map((data) => RadioListTile(
          title: Text(data.option),
          groupValue: id,
          value: data.option,
}
 @override
  Widget build(BuildContext context) {

A better way of doing this, providing data type on RadioListTile.

  OptionList? selectOption;

  List<OptionList> nList = [
    OptionList(
      padding: const EdgeInsets.fromLTRB(14.0, 180, 25, 50),
      index: 1,
      option: "Accept",
    ),
    OptionList(
      padding: const EdgeInsets.all(14.0),
      index: 2,
      option: "Reject",
    ),
  ];

  Widget _buildOmsIDTextField() {
    return Column(
      children: <Widget>[
        const Padding(
            padding: EdgeInsets.all(14.0),
            child: Text("Please Select an Option",
                style: TextStyle(fontSize: 23))),
        SizedBox(
          height: 150.0,
          child: Column(
            children: nList
                .map((data) => RadioListTile<OptionList>(
                      title: Text(data.option),
                      groupValue: selectOption,
                      value: data,
                      onChanged: (val) {
                        setState(() {
                          selectOption = val;
                        });
                      },
                    ))
                .toList(),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildOmsIDTextField(),
    );
  }
  • Related