Skip to content

Commit 2b05e16

Browse files
committed
refactor code to use RadioGroup instead of groupValue/onChanged in Radio
1 parent fba3b82 commit 2b05e16

File tree

1 file changed

+69
-109
lines changed

1 file changed

+69
-109
lines changed

packages/diagrams/lib/src/radio_list_tile.dart

Lines changed: 69 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,33 @@ 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,
4636
style: const TextStyle(
4737
color: Colors.blueAccent,
4838
decoration: TextDecoration.underline,
4939
),
50-
recognizer: TapGestureRecognizer()
51-
..onTap = () {
52-
print('Label has been tapped.');
53-
},
40+
recognizer:
41+
TapGestureRecognizer()
42+
..onTap = () {
43+
print('Label has been tapped.');
44+
},
5445
),
5546
),
5647
],
@@ -63,40 +54,21 @@ class LabeledRadio extends StatelessWidget {
6354
const LabeledRadio({
6455
required this.label,
6556
required this.padding,
66-
required this.groupValue,
6757
required this.value,
68-
required this.onChanged,
6958
super.key,
7059
});
7160

7261
final String label;
7362
final EdgeInsets padding;
74-
final bool groupValue;
7563
final bool value;
76-
final ValueChanged<bool?> onChanged;
7764

7865
@override
7966
Widget build(BuildContext context) {
8067
return InkWell(
81-
onTap: () {
82-
if (value != groupValue) {
83-
onChanged(value);
84-
}
85-
},
68+
onTap: () {},
8669
child: Padding(
8770
padding: padding,
88-
child: Row(
89-
children: <Widget>[
90-
Radio<bool>(
91-
groupValue: groupValue,
92-
value: value,
93-
onChanged: (bool? newValue) {
94-
onChanged(newValue);
95-
},
96-
),
97-
Text(label),
98-
],
99-
),
71+
child: Row(children: <Widget>[Radio<bool>(value: value), Text(label)]),
10072
),
10173
);
10274
}
@@ -127,29 +99,25 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
12799
alignment: FractionalOffset.center,
128100
padding: const EdgeInsets.all(5.0),
129101
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-
],
102+
child: RadioGroup<SingingCharacter?>(
103+
groupValue: _character,
104+
onChanged: (SingingCharacter? value) {
105+
setState(() {
106+
_character = value;
107+
});
108+
},
109+
child: const Column(
110+
children: <Widget>[
111+
RadioListTile<SingingCharacter>(
112+
title: Text('Lafayette'),
113+
value: SingingCharacter.lafayette,
114+
),
115+
RadioListTile<SingingCharacter>(
116+
title: Text('Thomas Jefferson'),
117+
value: SingingCharacter.jefferson,
118+
),
119+
],
120+
),
153121
),
154122
),
155123
);
@@ -161,31 +129,27 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
161129
alignment: FractionalOffset.center,
162130
padding: const EdgeInsets.all(5.0),
163131
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-
],
132+
child: RadioGroup<bool>(
133+
groupValue: _isRadioSelected,
134+
onChanged: (bool? newValue) {
135+
setState(() {
136+
_isRadioSelected = newValue!;
137+
});
138+
},
139+
child: const Column(
140+
children: <Widget>[
141+
LinkedLabelRadio(
142+
label: 'First tappable label text',
143+
padding: EdgeInsets.symmetric(horizontal: 5.0),
144+
value: true,
145+
),
146+
LinkedLabelRadio(
147+
label: 'Second tappable label text',
148+
padding: EdgeInsets.symmetric(horizontal: 5.0),
149+
value: false,
150+
),
151+
],
152+
),
189153
),
190154
),
191155
);
@@ -197,31 +161,27 @@ class _RadioListTileDiagramState extends State<RadioListTileDiagram> {
197161
alignment: FractionalOffset.center,
198162
padding: const EdgeInsets.all(5.0),
199163
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-
],
164+
child: RadioGroup<bool?>(
165+
groupValue: _isRadioSelected,
166+
onChanged: (bool? newValue) {
167+
setState(() {
168+
_isRadioSelected = newValue!;
169+
});
170+
},
171+
child: const Column(
172+
children: <Widget>[
173+
LabeledRadio(
174+
label: 'This is the first label text',
175+
padding: EdgeInsets.symmetric(horizontal: 5.0),
176+
value: true,
177+
),
178+
LabeledRadio(
179+
label: 'This is the second label text',
180+
padding: EdgeInsets.symmetric(horizontal: 5.0),
181+
value: false,
182+
),
183+
],
184+
),
225185
),
226186
),
227187
);

0 commit comments

Comments
 (0)