Home > Blockchain >  radio button onchanged not working flutter
radio button onchanged not working flutter

Time:02-03

I have radio list tile button dynamic. When I will change value, radio button not move selected (onchanged not working). This is my code.

RadioListTile(
    contentPadding: EdgeInsets.zero,
    value: '${listOption?.id}',
    groupValue: listQuestin?.code,
    activeColor: Colors.blue,
    selected: true,
    title: Align(
        alignment: Alignment(-1.1, 0),
        child: Text('${listOption?.text}', style: s2style)),
    onChanged: (val) {
      setState(() {
        print(val);
        listQuestin?.code = val.toString();

        question["questions[${listQuestin?.id}]"] = {
          "question": listQuestin?.text,
          "value": listQuestin?.code,
          "type": "radio",
          "text": listOption?.text
        };
      });
      // statusselect = val![position]['id'];
    }),

CodePudding user response:

You have to tell the error that appears so that we can find out what the problem is.

You can see how to use it here https://api.flutter.dev/flutter/material/RadioListTile-class.html

CodePudding user response:

try testing by part to find out more detailed errors

https://api.flutter.dev/flutter/material/RadioListTile-class.html

try to add a key like SingingCharacter when using RadioListTile or use LabeledRadio if you don't use a key in this example

RadioListTile<SingingCharacter>(
      contentPadding: EdgeInsets.zero,
      value: SingingCharacter.newValue,
      groupValue: _character,
      activeColor: Colors.blue,
      selected: true,
      title: const Text('New Value'),
      onChanged: (val) {
        setState(() {
          print(val);
          _character = val;
          /*listQuestin?.code = val.toString();

            question["questions[${listQuestin?.id}]"] = {
              "question": listQuestin?.text,
              "value": listQuestin?.code,
              "type": "radio",
              "text": listOption?.text
            };*/
        });
        // statusselect = val![position]['id'];
      },
    ),

CodePudding user response:

There are three things you need to take care, selected, value and groupValue.

value: data[index], // for each item value
selected: data[index] == selectedValue, // condition to select tile style/color
groupValue: selectedValue,// radio button active color

Example code:

 final data = List.generate(4, (index) => index.toString());
  String? selectedValue;
  void radioTileOnChanged(String? val) {
    setState(() {
      selectedValue = val;
    });
    print(val);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          ListView.builder(
            shrinkWrap: true,
            itemCount: data.length,
            itemBuilder: (context, index) {
              return RadioListTile<String?>(
                value: data[index], // for each item value
                selected: data[index] ==
                    selectedValue, // condition to select tile style/color
                groupValue: selectedValue, // radio button active color
                title: Text(data[index]),
                onChanged: radioTileOnChanged,
              );
            },
          ),
        ],
      ),
    );
  }
  •  Tags:  
  • Related