Search code examples
flutterwidget

RadioListTile -> radio button not updating state


In a stateful widget, all radios are in a Dialog widget :

class YourState extends State<YourWidget> {
Item _item;
@override
Widget build(context){
  if (_item == null) {
    _item = itemList[0]; //data set up just before
  } *(1)*
  List<Widget> childs = [];

  childs.add(
        Container(
            child: Material(
                child: RadioListTile<Area>(
          activeColor: appPrimaryColor,
          title: Text(item.name.toString()),
          value: item,
          selected: item == _item,
          groupValue: _item,
          onChanged: (Item value) {
            if (value != null) {
              setState(() => _item = value);
              print(_item == value);
              print(_item);
            }
          },
        ))),
      );
  }
}

So here is my problem, the radiobutton state is not updating when tapped. When I open the dialog it does show that my selection to first item is working (1), but it doesn't change to selected when tapped. The prints does return true and _item value has the right value.


Solution

  • You should keep Item _item; in class properties.

    class YourState extends State<YourWidget> {
      Item? _item;//Declare here
      
      @override
      Widget build(context){
        if (_item == null) {
          _item = itemList[0]; //data set up just on initial
        } 
        List<Widget> childs = [];
    
        childs.add(
            Container(
                child: Material(
                    child: RadioListTile<Area>(
              activeColor: appPrimaryColor,
              title: Text(item.name.toString()),
              value: item,
              selected: item == _item,
              groupValue: _item,
              onChanged: (Item value) {
                if (value != null) {
                  setState(() => _item = value);
                  print(_item == value);
                  print(_item);
                }
              },
            ))),
          );
         ...
      }
    }
    

    Edited

    Following code is working for me..

    class YourWidget extends StatefulWidget {
      const YourWidget({super.key});
    
      @override
      State<YourWidget> createState() => _YourWidgetState();
    }
    
    class _YourWidgetState extends State<YourWidget> {
      List<Area> items = List.generate(6, (index) => Area('Name: $index'));
      Area? _item;
    
      @override
      Widget build(BuildContext context) {
        _item ??= items[0];
        return ListView.builder(
          itemCount: items.length,
          physics: const BouncingScrollPhysics(),
          itemBuilder: (context, int i) {
            final item = items[i];
            return RadioListTile<Area>(
              activeColor: Colors.teal,
              title: Text(item.name.toString()),
              value: item,
              selected: item == _item,
              groupValue: _item,
              onChanged: (Area? value) {
                if (value != null) {
                  setState(() => _item = value);
                  print(_item == value);
                  print(_item);
                }
              },
            );
          },
        );
      }
    }
    
    class Area {
      final String name;
    
      Area(this.name);
    }