Flutter RadioButton Example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
// This Widget is the main application widget
class MyApp extends StatelessWidget {
static const String _title = 'Radio Button Example';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: Center(
child: MyStatefulWidget(),
),
),
);
}
}
enum BestSite { codingninjas, gfg, leetcode }
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key? key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
BestSite _site = BestSite.codingninjas;
Widget build(BuildContext context) {
return Column(
children: <Widget>[
ListTile(
title: const Text('www.codingninjas.com'),
leading: Radio(
value: BestSite.codingninjas,
groupValue: _site,
onChanged: (BestSite? value) {
setState(() {
_site = value!;
});
},
),
),
ListTile(
title: const Text('www.gfg.com'),
leading: Radio(
value: BestSite.gfg,
groupValue: _site,
onChanged: (BestSite? value) {
setState(() {
_site = value!;
});
},
),
),
ListTile(
title: const Text('www.leetcode.com'),
leading: Radio(
value: BestSite.leetcode,
groupValue: _site,
onChanged: (BestSite? value) {
setState(() {
_site = value!;
});
},
),
),
],
);
}
}
Output:

Radio <T> class
A material design radio button.
They are used to select between several mutually exclusive values. When any one radio button in a group is selected, the other radio buttons cease to be chosen. The values are of type T, the Radio class's type parameter. We commonly use Enums for this purpose.
The radio button itself does not hold any state. Instead, selecting the radio invokes the onChanged callback, passing the value as a parameter. If groupValue and value match, we will choose this radio. Most widgets will react to onChanged by calling State.setState to update the radio button's groupValue.
The syntax is the same as above, with a template class.
For example:
Normal code:
leading: Radio(
value: BestSite.codingninjas,
groupValue: _site,
onChanged: (BestSite? value) {
setState(() {
_site = value!;
});
},
),
Template code:
leading: Radio<SingingCharacter>(
value: BestSite.codingninjas,
groupValue: _site,
onChanged: (BestSite? value) {
setState(() {
_site = value!;
});
},
),
Frequently Asked Questions
What is Flutter used for?
Flutter is Google's portable UI toolkit for crafting attractive, natively compiled mobile, web, & desktop applications from a single codebase. The Flutter works with existing code, is used by developers and organizations worldwide, and is free and open source.
Is Flutter better than Java?
Flutter is Google's Cross-platform framework that is faster, while Java language is a safer option for its strong support, documentation, and continuously updated. Different tools are also available for mobile, web, and desktop application development, but these two have the upper hand on the rest of the frameworks.
Is Flutter a programming language?
No, Flutter is not a programming language. Flutter is an SDK(software development kit) with prewritten code consisting of ready-to-use and customizable widgets, libraries, tools, and documentation that help build cross-platform apps.
What is a Flutter RadioButton?
Flutter lets us to use radio buttons with the help of 'Radio', 'RadioListTile', or 'ListTitle' Widgets. The flutter radio button does not support any state by itself. When we select any radio button, it invokes the onChanged callback and passes the value as a parameter.
What is the difference between the checkbox and Radiobutton?
Checkboxes and RadioButtons are elements for making selections. The Checkboxes allow the user to choose items from a fixed number of alternatives. In contrast, radio buttons allow the user to select exactly one thing from a list of several predefined options.
Conclusion
This article gives information about Flutter RadioButton and its uses. Being a Flutter developer, you must be well versed with the concepts like Flutter RadioButton, which you have learned just now.
Also read Flutter Checkbox, Flutter Table, Flutter Calendar & Flutter Tabbar.
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!
