Skip to content

Commit bc4bb28

Browse files
authored
refactor code to use RadioGroup instead of groupValue/onChanged in Radio (#259)
1 parent fba3b82 commit bc4bb28

File tree

1 file changed

+64
-100
lines changed

1 file changed

+64
-100
lines changed

packages/diagrams/lib/src/radio_list_tile.dart

Lines changed: 64 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,21 @@ class LinkedLabelRadio extends StatelessWidget {
1515
const LinkedLabelRadio({
1616
required this.label,
1717
required this.padding,
18-
required this.groupValue,
1918
required this.value,
20-
required this.onChanged,
2119
super.key,
2220
});
2321

2422
final String label;
2523
final EdgeInsets padding;
26-
final bool groupValue;
2724
final bool value;
28-
final ValueChanged<bool?> onChanged;
2925

3026
@override
3127
Widget build(BuildContext context) {
3228
return Padding(
3329
padding: padding,
3430
child: Row(
3531
children: <Widget>[
36-
Radio<bool>(
37-
groupValue: groupValue,
38-
value: value,
39-
onChanged: (bool? newValue) {
40-
onChanged(newValue);
41-
},
42-
),
32+
Radio<bool>(value: value),
4333
RichText(
4434
text: TextSpan(
4535
text: label,
@@ -63,37 +53,23 @@ class LabeledRadio extends StatelessWidget {
6353
const LabeledRadio({
6454
required this.label,
6555
required this.padding,
66-
required this.groupValue,
6756
required this.value,
68-
required this.onChanged,
6957
super.key,
7058
});
7159

7260
final String label;
7361
final EdgeInsets padding;
74-
final bool groupValue;
7562
final bool value;
76-
final ValueChanged<bool?> onChanged;
7763

7864
@override
7965
Widget build(BuildContext context) {
8066
return InkWell(
81-
onTap: () {
82-
if (value != groupValue) {
83-
onChanged(value);
84-
}
85-
},
67+
onTap: () {},
8668
child: Padding(
8769
padding: padding,
8870
child: Row(
8971
children: <Widget>[
90-
Radio<bool>(
91-
groupValue: groupValue,
92-
value: value,
93-
onChanged: (bool? newValue) {
94-
onChanged(newValue);
95-
},
96-
),
72+
Radio<bool>(value: value),
9773
Text(label),
9874
],
9975
),
@@ -127,29 +103,25 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
127103
alignment: FractionalOffset.center,
128104
padding: const EdgeInsets.all(5.0),
129105
color: Colors.white,
130-
child: Column(
131-
children: <Widget>[
132-
RadioListTile<SingingCharacter>(
133-
title: const Text('Lafayette'),
134-
value: SingingCharacter.lafayette,
135-
groupValue: _character,
136-
onChanged: (SingingCharacter? value) {
137-
setState(() {
138-
_character = value;
139-
});
140-
},
141-
),
142-
RadioListTile<SingingCharacter>(
143-
title: const Text('Thomas Jefferson'),
144-
value: SingingCharacter.jefferson,
145-
groupValue: _character,
146-
onChanged: (SingingCharacter? value) {
147-
setState(() {
148-
_character = value;
149-
});
150-
},
151-
),
152-
],
106+
child: RadioGroup<SingingCharacter?>(
107+
groupValue: _character,
108+
onChanged: (SingingCharacter? value) {
109+
setState(() {
110+
_character = value;
111+
});
112+
},
113+
child: const Column(
114+
children: <Widget>[
115+
RadioListTile<SingingCharacter>(
116+
title: Text('Lafayette'),
117+
value: SingingCharacter.lafayette,
118+
),
119+
RadioListTile<SingingCharacter>(
120+
title: Text('Thomas Jefferson'),
121+
value: SingingCharacter.jefferson,
122+
),
123+
],
124+
),
153125
),
154126
),
155127
);
@@ -161,31 +133,27 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
161133
alignment: FractionalOffset.center,
162134
padding: const EdgeInsets.all(5.0),
163135
color: Colors.white,
164-
child: Column(
165-
children: <Widget>[
166-
LinkedLabelRadio(
167-
label: 'First tappable label text',
168-
padding: const EdgeInsets.symmetric(horizontal: 5.0),
169-
value: true,
170-
groupValue: _isRadioSelected,
171-
onChanged: (bool? newValue) {
172-
setState(() {
173-
_isRadioSelected = newValue!;
174-
});
175-
},
176-
),
177-
LinkedLabelRadio(
178-
label: 'Second tappable label text',
179-
padding: const EdgeInsets.symmetric(horizontal: 5.0),
180-
value: false,
181-
groupValue: _isRadioSelected,
182-
onChanged: (bool? newValue) {
183-
setState(() {
184-
_isRadioSelected = newValue!;
185-
});
186-
},
187-
),
188-
],
136+
child: RadioGroup<bool>(
137+
groupValue: _isRadioSelected,
138+
onChanged: (bool? newValue) {
139+
setState(() {
140+
_isRadioSelected = newValue!;
141+
});
142+
},
143+
child: const Column(
144+
children: <Widget>[
145+
LinkedLabelRadio(
146+
label: 'First tappable label text',
147+
padding: EdgeInsets.symmetric(horizontal: 5.0),
148+
value: true,
149+
),
150+
LinkedLabelRadio(
151+
label: 'Second tappable label text',
152+
padding: EdgeInsets.symmetric(horizontal: 5.0),
153+
value: false,
154+
),
155+
],
156+
),
189157
),
190158
),
191159
);
@@ -197,31 +165,27 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
197165
alignment: FractionalOffset.center,
198166
padding: const EdgeInsets.all(5.0),
199167
color: Colors.white,
200-
child: Column(
201-
children: <Widget>[
202-
LabeledRadio(
203-
label: 'This is the first label text',
204-
padding: const EdgeInsets.symmetric(horizontal: 5.0),
205-
value: true,
206-
groupValue: _isRadioSelected,
207-
onChanged: (bool? newValue) {
208-
setState(() {
209-
_isRadioSelected = newValue!;
210-
});
211-
},
212-
),
213-
LabeledRadio(
214-
label: 'This is the second label text',
215-
padding: const EdgeInsets.symmetric(horizontal: 5.0),
216-
value: false,
217-
groupValue: _isRadioSelected,
218-
onChanged: (bool? newValue) {
219-
setState(() {
220-
_isRadioSelected = newValue!;
221-
});
222-
},
223-
),
224-
],
168+
child: RadioGroup<bool?>(
169+
groupValue: _isRadioSelected,
170+
onChanged: (bool? newValue) {
171+
setState(() {
172+
_isRadioSelected = newValue!;
173+
});
174+
},
175+
child: const Column(
176+
children: <Widget>[
177+
LabeledRadio(
178+
label: 'This is the first label text',
179+
padding: EdgeInsets.symmetric(horizontal: 5.0),
180+
value: true,
181+
),
182+
LabeledRadio(
183+
label: 'This is the second label text',
184+
padding: EdgeInsets.symmetric(horizontal: 5.0),
185+
value: false,
186+
),
187+
],
188+
),
225189
),
226190
),
227191
);

0 commit comments

Comments
 (0)