'How to apply onTap event to RadioListTile?

I have a popup dialog with a list of radio buttons that I wish to pop whenever a radio button is selected regardless of whether it is currently selected or not.

Using the Radio or RadioListTile widget included in the flutter framework only allows for the onChanged event, which only fires off if there is a change. This means that if an item is already selected, the dialog will not be popped.

The toggleable property, is just that, a property, and does not allow for additional instructions to pop the dialog. It simply has an internal toggle that deselects the value.

I tried wrapping the RadioListTile with a GestureDetector which did not work, because the child (ListTile) of the RadioListTile has the tap priority.

There are other third packages that does this better but I would prefer to stick to the built in widget as much as I can.

Any help is appreciated. See below snippets of the code:

RadioListTile<Folder>(
  value: controller.folders[index],
  groupValue: controller.selectedFolder.value,
  onChanged: (newValue){
    // This callback is not invoked when an item is already selected
    controller.selectedFolder.value = newValue ?? controller.folders[0];
    // I'd like this to be called even if item has not changed
    Get.back(result: controller.selectedFolder.value);
  },
  title: Text(folder.isEmpty ? 'None' : folder.name),
)


Solution 1:[1]

You may set toggleable to true and update only when the new value is not null. E.g.

RadioListTile<Folder>(
  value: controller.folders[index],
  groupValue: controller.selectedFolder.value,
  toggleable: true,
  onChanged: (newValue) {
    if (newValue != null) {
      controller.selectedFolder.value = newValue;
    }

    Get.back(result: controller.selectedFolder.value);
  },
  title: Text(folder.isEmpty ? 'None' : folder.name),
)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 khw147