Radio
sample.packages.flutter.lib.src.material.radio.32.
Here is an example of Radio widgets wrapped in ListTiles, which is similar to what you could get with the RadioListTile widget.
The currently selected character is passed into groupValue
, which is
maintained by the example's State
. In this case, the first Radio
will start off selected because _character
is initialized to
SingingCharacter.lafayette
.
If the second radio button is pressed, the example's state is updated
with setState
, updating _character
to SingingCharacter.jefferson
.
This causes the buttons to rebuild with the updated groupValue
, and
therefore the selection of the second button.
Requires one of its ancestors to be a [Material] widget.
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const Center(
child: MyStatefulWidget(),
),
),
);
}
}
enum SingingCharacter { lafayette, jefferson }
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
SingingCharacter? _character = SingingCharacter.lafayette;
@override
Widget build(BuildContext context) {
return Column(
children: [
ListTile(
title: const Text('Lafayette'),
leading: Radio(
value: SingingCharacter.lafayette,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
ListTile(
title: const Text('Thomas Jefferson'),
leading: Radio(
value: SingingCharacter.jefferson,
groupValue: _character,
onChanged: (SingingCharacter? value) {
setState(() {
_character = value;
});
},
),
),
],
);
}
}
SHARE: