flutter
  1. flutter-radio-button

Flutter Radio Button

Radio buttons are a popular choice for allowing users to select a single option from a list. In Flutter, radio buttons can be implemented using the Radio widget, which provides a simple and intuitive way to handle user selection.

Syntax

Radio(
  value: value,
  groupValue: groupValue,
  onChanged: onChanged,
)
  • value: The value associated with this radio button
  • groupValue: The currently selected value of the group
  • onChanged: A callback function that gets called when the radio button is selected

Example

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int _selectedValue;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        RadioListTile(
          title: Text('Option 1'),
          value: 1,
          groupValue: _selectedValue,
          onChanged: (value) {
            setState(() {
              _selectedValue = value;
            });
          },
        ),
        RadioListTile(
          title: Text('Option 2'),
          value: 2,
          groupValue: _selectedValue,
          onChanged: (value) {
            setState(() {
              _selectedValue = value;
            });
          },
        ),
        RadioListTile(
          title: Text('Option 3'),
          value: 3,
          groupValue: _selectedValue,
          onChanged: (value) {
            setState(() {
              _selectedValue = value;
            });
          },
        ),
      ],
    );
  }
}

In this example, we are using RadioListTile widgets to display a list of three options and allow the user to select a single option. The selected value is stored in the _selectedValue state variable and updated using the setState function.

Output

The output of this example is a list of three radio buttons, one for each option. When one of the radio buttons is selected, the _selectedValue state variable is updated and the UI is updated to reflect the selected option.

Explanation

Radio buttons are a common user interface widget used in many applications. In Flutter, radio buttons can be easily implemented using the Radio widget. The Radio widget takes in a value, a group value, and a callback function for handling user selection. By creating a list of Radio widgets and linking them to a common group value, we can allow the user to select a single option from the list.

Use

Radio buttons can be used whenever a user needs to select a single value from a list of options. Some examples include:

  • Selecting a payment method from a list of available methods
  • Selecting a subscription tier from a list of available tiers
  • Selecting a delivery method from a list of available options

Important Points

  • Radio buttons are a useful and common user interface component.
  • They can be implemented using the Radio widget in Flutter.
  • Radio buttons should be used when a user needs to select a single option from a list.

Summary

Radio buttons are a simple and intuitive way to allow users to select a single option from a list. In Flutter, we can easily implement radio buttons using the Radio widget. By linking the Radio widgets to a common group value, we can ensure that only one option can be selected at a time. Radio buttons are a popular choice for many user interface scenarios and can help improve the user experience of our Flutter applications.

Published on: