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),
),
],
),
);
}
}