Sun. Apr 20th, 2025

Here’s a simple example of using radio buttons in Flutter. It lets the user select one option from a group of choices

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: RadioButtonExample(),
);
}
}

class RadioButtonExample extends StatefulWidget {
@override
_RadioButtonExampleState createState() => _RadioButtonExampleState();
}

class _RadioButtonExampleState extends State<RadioButtonExample> {
String selectedOption = 'Option 1';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Radio Button Example')),
body: Column(
children: <Widget>[
ListTile(
title: Text('Option 1'),
leading: Radio<String>(
value: 'Option 1',
groupValue: selectedOption,
onChanged: (value) {
setState(() {
selectedOption = value!;
});
},
),
),
ListTile(
title: Text('Option 2'),
leading: Radio<String>(
value: 'Option 2',
groupValue: selectedOption,
onChanged: (value) {
setState(() {
selectedOption = value!;
});
},
),
),
ListTile(
title: Text('Option 3'),
leading: Radio<String>(
value: 'Option 3',
groupValue: selectedOption,
onChanged: (value) {
setState(() {
selectedOption = value!;
});
},
),
),
SizedBox(height: 20),
Text(
'Selected: $selectedOption',
style: TextStyle(fontSize: 18),
),
],
),
);
}
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *