What is a Radio Button?
A radio button is a UI element that allows the user to select exactly one option from a set. In Flutter, the Radio widget provides a simple circular button, while RadioListTile combines a radio button with a label, making it easier to use in forms and lists. Radio buttons are commonly used for gender selection, payment methods, or any exclusive choice.
Basic Radio Widget
The Radio widget is the simplest form. It requires three parameters: value, groupValue, and onChanged. The value is the unique identifier for this radio button. The groupValue is the currently selected value. When the user taps the radio, onChanged is called with the radio's value.
Using RadioListTile for Better UI
RadioListTile wraps a radio button and a label (title, subtitle, etc.) in a single widget. It handles the layout and provides a larger tap target. It also integrates well with ListTile and forms.
Creating a Radio Group
A radio group is simply a set of Radio or RadioListTile widgets that share the same groupValue. The type parameter (e.g., <int>, <String>) ensures type safety. You can use any type as the group value.
Customizing Radio Buttons
You can customize the radio button's color, size, and other properties:
activeColor: Color when selected.
fillColor: Overrides the fill color (MaterialStateProperty).
visualDensity: Adjusts the compactness.
materialTapTargetSize: Controls the tap target size.
Integrating with Forms
To use radio buttons in a Form, you can wrap them in a FormField or use RadioListTile inside a Form with a GlobalKey. For validation, you can create a custom form field that manages the radio group.
Common Mistakes
- Not updating
groupValueinonChanged: The radio won't visually update.
- Not updating
- Using different
valuetypes in the same group: MixingintandStringwill break the group.
- Using different
- Forgetting to call
setState(if using StatefulWidget) to rebuild when selection changes.
- Forgetting to call
- Not providing a
groupValue: The radio will be indeterminate (no selection).
- Not providing a
- Using
RadioListTilewithout atitle: The tile may be empty; always provide at least a title.
- Using
Best Practices
- Use
RadioListTilefor a better user experience – larger tap area, integrated label.
- Use
- Use
enumfor group values to avoid magic numbers/strings.
- Use
- For forms, either use a custom
FormFieldor manage the selection separately and validate in the form submission.
- For forms, either use a custom
- Keep the number of radio options reasonable; for many options, consider a dropdown.
Key Takeaways
Radioprovides a simple circular button;RadioListTileadds a label and larger tap area.
- All radio buttons in a group share the same
groupValueandonChangedupdates it.
- All radio buttons in a group share the same
- Use
enumfor type‑safe group values.
- Use
- Customize colors and density with properties like
activeColor.
- Customize colors and density with properties like
- For forms, create a custom
FormFieldor useRadioListTileinside aFormwith manual validation.
- For forms, create a custom