How to group list of Radio buttons from string array. Radio Button when clicked is pointed in all items. Radio list tile is selected in all items

2.5k views Asked by At

I am using flutter-SQLite-Dart to develop a mobile application. with radio list tile I have assigned a radio button and passed a value of index. When executing the application I am able to click all the radio buttons. I need to make the list of radio buttons to be clickable only once, when I have selected one radio button the others should not be selected. This is the requirement.

return InkWell(
            onTap: press,
            child: Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                        height: 60.0,
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              unselectedWidgetColor: Colors.grey,
                              disabledColor: Colors.blue),
                          child: Column(children: [
                        
                            RadioListTile(
                              title: Text("${sample[index]}",
                                  style:
                                  TextStyle(color: Colors.black)),
                              groupValue: id,
                              value: index,
                              activeColor: Colors.green,
                              onChanged: (val) {
                                setState(() {
                                  id = val;
                                  debugPrint('val answer$val');
                                });
                              },
                              toggleable: true,
                            ),
                          ]),
                        )),
                  ),
                  ],
              ),
            ),
          );

The output of the application

enter image description here

2

There are 2 answers

0
Sanjay Kumar On

Hi Please check my answer below. I have used statefull widget.

import 'package:flutter/material.dart';

class RadioList extends StatefulWidget {
  final List<String> list;
  final ValueChanged<int>? selectedIndex;

  RadioList(this.list, this.selectedIndex);

  @override
  _RadioListState createState() => _RadioListState();
}

class _RadioListState extends State<RadioList> {

  int currentIndex = -1;

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      itemCount: widget.list.length,
      itemBuilder: (_, index) {
        String value = widget.list[index];
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [

            Radio(value: index, groupValue: currentIndex, onChanged: (flag) {
              setState(() {
                currentIndex = index;
              });
            }),
            Text(value)
          ],
        );
      }, separatorBuilder: (_, index) {
      return SizedBox(height: 10,);
    },);
  }
}
0
mideveloper On

Try below code, you will declare a String with groupValue.


String _groupValue;

return InkWell(
            onTap: press,
            child: Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                        height: 60.0,
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              unselectedWidgetColor: Colors.grey,
                              disabledColor: Colors.blue),
                          child: Column(children: [
                        
                            RadioListTile(
                              title: Text("${sample[index]}",
                                  style:
                                  TextStyle(color: Colors.black)),
                              groupValue: _groupValue,
                              value: index,
                              activeColor: Colors.green,
                              onChanged: (val) {
                                setState(() {
                                   _groupValue = val;
                                  debugPrint('val answer$val');
                                });
                              },
                          
                            ),
                          ]),
                        )),
                  ),
                  ],
              ),
            ),
          );