main.dart
import 'package:flutter/material.dart';
void main() => runApp(const FlutterExample());
class FlutterExample extends StatelessWidget {
const FlutterExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Example',
theme: ThemeData(primarySwatch: Colors.red),
home: const StateExample()
);
}
}
class StateExample extends StatefulWidget {
const StateExample({Key? key}) : super(key: key);
@override
_StateExampleState createState() => _StateExampleState();
}
enum colors { red, green, blue }
class _StateExampleState extends State<StateExample> {
colors _color = colors.green;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF5F5F5),
appBar: AppBar(
title: const Text("Flutter - Radio Button Example")
),
body: bodyContent()
);
}
bodyContent() {
return Column(
children: [
ListTile(
title: const Text("Red"),
tileColor: Colors.black12,
leading: Radio<colors>(
value: colors.red,
groupValue: _color,
onChanged: (colors? value){
setState(() {
_color = value??_color;
});
},
),
),
ListTile(
title: const Text("Green"),
tileColor: Colors.black12,
leading: Radio<colors>(
value: colors.green,
groupValue: _color,
onChanged: (colors? value){
setState(() {
_color = value??_color;
});
},
),
),
ListTile(
title: const Text("Blue"),
tileColor: Colors.black12,
leading: Radio<colors>(
value: colors.blue,
groupValue: _color,
onChanged: (colors? value){
setState(() {
_color = value??_color;
});
},
),
)
],
);
}
}
![](https://fluttertuts.wordpress.com/wp-content/uploads/2021/06/fbaf5-flutterradiobuttonexample.png)
![](https://fluttertuts.wordpress.com/wp-content/uploads/2021/06/551d1-flutterradiobuttonexample2.png)
More flutter tutorials
- flutter – LinearProgressIndicator width
- flutter – CircularProgressIndicator example
- flutter – CircularProgressIndicator with percentage
- flutter – LinearProgressIndicator percentage
- flutter – How to create rounded corners ToggleButtons
- flutter – How to use ToggleButtons onPressed
- flutter – Checkbox checked color
- flutter – Checkbox onChanged
- flutter – CheckboxListTile left
- flutter – CheckboxListTile subtitle and icon
- flutter – CheckboxListTile list
- flutter – Radio button group
- flutter – Radio button color
- flutter – Radio button with text
- flutter – Radio button list