This app shows three radio buttons labeled Red, Green, and Blue. Only one can be selected at a time. The selected color name is shown below.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _selectedColor = 'red';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Radio Buttons Example')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListTile(
title: const Text('Red'),
leading: Radio<String>(
value: 'red',
groupValue: _selectedColor,
onChanged: (String? value) {
setState(() {
_selectedColor = value!;
});
},
),
),
ListTile(
title: const Text('Green'),
leading: Radio<String>(
value: 'green',
groupValue: _selectedColor,
onChanged: (String? value) {
setState(() {
_selectedColor = value!;
});
},
),
),
ListTile(
title: const Text('Blue'),
leading: Radio<String>(
value: 'blue',
groupValue: _selectedColor,
onChanged: (String? value) {
setState(() {
_selectedColor = value!;
});
},
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Text('Selected color: $_selectedColor', style: const TextStyle(fontSize: 18)),
),
],
),
),
);
}
}