How to use Flutter Radio buttons

Radio buttons are a widget that allows users to select one option from a group of options. In Flutter, we can easily implement radio buttons using the Radio widget and a RadioListTile widget for a more convenient and user-friendly experience.

Constructor and parameters

The Radio widget provides a constructor with various parameters to customize its behavior and layout.

Radio({
  Key? key, 
  required T value, 
  required T? groupValue,
  required ValueChanged<T?>? onChanged,
  MouseCursor? mouseCursor, 
  bool toggleable = false, 
  Color? activeColor, 
  MaterialStateProperty<Color?>? fillColor,
  Color? focusColor, 
  Color? hoverColor, 
  MaterialStateProperty<Color?>? overlayColor,
  double? splashRadius, 
  MaterialTapTargetSize? materialTapTargetSize, 
  VisualDensity? visualDensity,
  FocusNode? focusNode,
  bool autofocus = false
})

Let's take a closer look at each parameter and its purpose:

  • key: An optional parameter representing the key to identify the Radio widget.

  • value: The value associated with the radio button. When the radio button is selected, this value is passed to the onChanged callback. The type T represents the data type of the value.

  • groupValue: The currently selected value in the group of radio buttons. The groupValue should match the value of the selected radio button. The type T represents the data type of the value.

  • onChanged: A callback function when the radio button is selected. It takes the newly selected value as its parameter, allowing us to handle the changes accordingly. The type T represents the data type of the value.

  • mouseCursor: An optional parameter specifying the mouse cursor to be used when the mouse hovers over the radio button.

  • toggleable: An optional parameter determining whether the radio button can be toggled on and off. If set to true, clicking the selected radio button will deselect it.

  • activeColor: An optional parameter that sets the radio button's color when selected.

  • fillColor: An optional parameter that defines the fill color of the radio button. The color can be customized based on different states, such as when it's pressed, hovered over, or disabled. We can use a MaterialStateColor to define different colors for different states.

  • focusColor: An optional parameter that sets the color of the radio button when it has keyboard focus.

  • hoverColor: An optional parameter that sets the color of the radio button when the mouse hovers over it.

  • overlayColor: An optional parameter that defines the color of the radio button's overlay when pressed. Similarly to fillColor, we can use a MaterialStateColor to customize the overlay color based on different states.

  • splashRadius: An optional parameter that determines the size of the splash radius when the radio button is pressed. It specifies the radius of the circular ripple effect around the button.

  • materialTapTargetSize: An optional parameter specifying the radio button's target size. It affects the hit test area of the button and can be used to adjust its overall size.

  • visualDensity: An optional parameter that allows us to adjust the spacing and density of the radio button within its surrounding container.

  • focusNode: An optional parameter that associates a FocusNode with the radio button. It enables keyboard focus management and navigation.

  • autofocus: An optional parameter determining whether the radio button should request focus when the widget is first displayed.

Implementation

We can follow the instructions given below to add a Radio widget in UI:

Import the necessary packages

To use the Radio widget, we need to import the flutter/material.dart package into your Dart file.

import 'package:flutter/material.dart';

Declaring variables

We need to declare selectedOption variable just above the build, which will hold the currently selected value of Radio widgets.

int selectedOption;

Adding a simple radio button

Below is a sample code of the Flutter Radio button with a listTile.

import 'package:flutter/material.dart';  
  
void main() {  
  runApp(MaterialApp( home: MyHomePage(),));  
}  
  
class MyHomePage extends StatefulWidget {  
  @override  
  _HomePageState createState() => _HomePageState();  
}  
  
class _HomePageState extends State<MyHomePage> {  
  int selectedOption = 1; 
  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: Scaffold(  
        appBar: AppBar(title: Text('Educative Answers'),),  
        body: Container(  
            child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ListTile(
               title: const Text('www.educative.io'),
                leading: Radio<int>(
                  value: 1,
                  groupValue: selectedOption,
                  onChanged: (value) {
                   setState(() {
                    selectedOption = value;
                    print("Button value: $value");
                    });
                  },
                ),
              ),
            ],
          ),
        ),  
      ),  
    );  
  }  
}  

Code explanation

  1. Lines 21–38: Inside the Column, a ListTile widget is added, representing a single row in the list. It contains a Text widget as the title on line 5 and a Radio widget as the optional leading widget on line 6.

  2. Lines 26–35. The Radio widget is the radio button allowing a single option to select.

    1. Line 27: Here, we assigned 1 to its value property to make it distinguish between a group of radio buttons.

    2. Line 28: The groupValue property is set to the selectedOption variable, ensuring proper grouping of radio buttons.

    3. Line 29–34: The onChanged property is a callback function that triggers when the radio button is selected. Within this callback, setState() is called on to update the selectedOption variable with the newly selected value.

Output

Simple Radio button using ListTIle widget
Simple Radio button using ListTIle widget

Adding multiple radio buttons with a default value

Below is a sample code of the Flutter for adding multiple Radio button with default value in a listTile.

import 'package:flutter/material.dart';  
  
void main() {  
  runApp(MaterialApp( home: MyHomePage(),));  
}  
  
class MyHomePage extends StatefulWidget {  
  @override  
  _HomePageState createState() => _HomePageState();  
}  
  
class _HomePageState extends State<MyHomePage> {  
  int selectedOption = 1; 
  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: Scaffold(  
        appBar: AppBar(title: Text('Educative Answers'),),  
        body: Container(  
            child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ListTile(
                title: const Text('Option 1'),
                leading: Radio<int>(
                  value: 1,
                  groupValue: selectedOption,
                  onChanged: (value) {
                    setState(() {
                      selectedOption = value;
                    });
                  },
                ),
              ),
              ListTile(
                title: const Text('Option 2'),
                leading: Radio<int>(
                  value: 2,
                  groupValue: selectedOption,
                  onChanged: (value) {
                    setState(() {
                      selectedOption = value;
                    });
                  },
                ),
              ),
            ],
          ),
        ),  
      ),  
    );  
  }  
}  

To have the default selected radio button, we need to assign a value to selectedOption variable like this:

int selectedOption = 1;

Code explanation

  1. Lines 21–49: Inside, the Column are two ListTile widgets, representing two rows in the column.

  2. Line 25: The title property of the first ListTile is set to a Text widget with the text "Option 1".

  3. Line 26: The leading property of the first ListTile is set to a Radio widget. The Radio widget is a circular button representing a single option to select.

  4. Line 27: The value property of the first Radio widget is set to 1, indicating that it represents the first option.

  5. Line 28: The groupValue property of the first Radio widget is set to the selectedOption variable. This property is used to group radio buttons together.

  6. Lines 29–34: The onChanged property of the first Radio widget is set to a callback function. When the radio button is selected, this function will be called. It uses the setState method to update the selectedOption variable with the newly selected value.

  7. Lines 36–47: The second ListTile is similar to the first one, representing the second option.

Output

Radio buttons with default values
Radio buttons with default values

Change the radio button color

Below is a sample code of the Flutter for adding color to the Radio button:

Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ListTile(
title: const Text('Option 1'),
leading: Radio<int>(
value: 1,
groupValue: selectedOption,
activeColor: Colors.red, // Change the active radio button color here
fillColor: MaterialStateProperty.all(Colors.red), // Change the fill color when selected
splashRadius: 20, // Change the splash radius when clicked
onChanged: (int? value) {
setState(() {
selectedOption = value!;
});
},
),
),
ListTile(
title: const Text('Option 2'),
leading: Radio<int>(
value: 2,
groupValue: selectedOption,
activeColor: Colors.blue, // Change the active radio button color here
fillColor: MaterialStateProperty.all(Colors.blue), // Change the fill color when selected
splashRadius: 25, // Change the splash radius when clicked
onChanged: (int? value) {
setState(() {
selectedOption = value!;
});
},
),
),
],
),

Code explanation

This code is similar to the previous code explanation, with some additional properties set for the Radio widget.

  1. activeColor: This property sets the color of the selected radio button. In the first Radio widget, it is set to Colors.red, and in the second Radio widget, it is set to Colors.blue.

  2. fillColor: This property sets the color of the fill when the radio button is selected. It is set using the MaterialStateProperty.all constructor. Both Radio widgets are set to Colors.red and Colors.blue respectively.

  3. splashRadius: This property sets the radius of the splash effect when the radio button is clicked. In the first Radio widget, it is set to 20, and in the second Radio widget, it is set to 25.

  4. onChanged: This property defines the callback function when selecting the radio button. It updates the selectedOption variable.

Output

Customized radio buttons
Customized radio buttons

Complete code

We get the following output by putting together the code explained above.

import 'package:flutter/material.dart';  
  
void main() {  
  runApp(MaterialApp( home: MyHomePage(),));  
}  
  
class MyHomePage extends StatefulWidget {  
  @override  
  _HomePageState createState() => _HomePageState();  
}  
  
class _HomePageState extends State<MyHomePage> {  
  int selectedOption = 1; 
  
  @override  
  Widget build(BuildContext context) {  
    return MaterialApp(  
      home: Scaffold(  
        appBar: AppBar(title: Text('Educative Answers'),),  
        body: Container(  
            child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              ListTile(
                title: const Text('Option 1'),
                leading: Radio<int>(
                  value: 1,
                  groupValue: selectedOption,
                  activeColor: Colors.red, // Change the active radio button color here
                  fillColor: MaterialStateProperty.all(Colors.red), // Change the fill color when selected
                  splashRadius: 20, // Change the splash radius when clicked
                  onChanged: (value) {
                    setState(() {
                      selectedOption = value;
                    });
                  },
                ),
              ),
              ListTile(
                title: const Text('Option 2'),
                leading: Radio<int>(
                  value: 2,
                  groupValue: selectedOption,
                  activeColor: Colors.blue, // Change the active radio button color here
                  fillColor: MaterialStateProperty.all(Colors.blue), // Change the fill color when selected
                  splashRadius: 25, // Change the splash radius when clicked
                  onChanged: (value) {
                    setState(() {
                      selectedOption = value;
                    });
                  },
                ),
              ),
            ],
          ),
        ),  
      ),  
    );  
  }  
}  

Conclusion

Flutter provides a simple and effective way to implement radio buttons. We can create visually appealing and interactive user options with the Radio widget and its customizable properties. We can easily handle the selection changes and update the UI by utilizing the callback. With Flutter's radio buttons, we can enhance our app's user experience and create intuitive interfaces.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved