Skip to content

Commit bb32d99

Browse files
committed
onChanged changed in GFRadio
1 parent d5be7b5 commit bb32d99

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

example/lib/main_temp.dart

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class _MyHomePageState extends State<MyHomePage>
4444
bool check = false;
4545
String searchData;
4646
final TextEditingController _searchController = TextEditingController();
47+
int groupValue = 0;
4748

4849
@override
4950
void initState() {
@@ -158,8 +159,30 @@ class _MyHomePageState extends State<MyHomePage>
158159
height: 10,
159160
),
160161

161-
Radio(value: null, groupValue: null, onChanged: null),
162-
RadioListTile(value: null, groupValue: null, onChanged: null),
162+
Radio(
163+
value: 0,
164+
groupValue: groupValue,
165+
onChanged: (val) {
166+
print('val $val');
167+
setState(() {
168+
groupValue = val;
169+
});
170+
print('radio $groupValue $val');
171+
},
172+
),
173+
174+
Radio(
175+
value: 1,
176+
groupValue: groupValue,
177+
onChanged: (val) {
178+
print('val $val');
179+
setState(() {
180+
groupValue = val;
181+
});
182+
print('radio $groupValue $val');
183+
},
184+
),
185+
// RadioListTile(value: null, groupValue: null, onChanged: null),
163186

164187
GFRadioButton(
165188
size: GFSize.SMALL,
@@ -169,14 +192,14 @@ class _MyHomePageState extends State<MyHomePage>
169192
// inactivebgColor: GFColors.PRIMARY,
170193
// activeBorderColor: GFColors.DANGER,
171194
// inactiveBorderColor: GFColors.DARK,
172-
value: check,
173-
groupValue: check,
195+
value: 0,
196+
groupValue: groupValue,
174197
onChanged: (val) {
175198
print('val $val');
176199
setState(() {
177-
check = val;
200+
groupValue = val;
178201
});
179-
print('cch $check $val');
202+
print('radio $groupValue $val');
180203
},
181204
activeIcon: const Icon(
182205
Icons.check,

lib/components/radio/gf_radio.dart

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,17 @@ class _GFRadioButtonState extends State<GFRadioButton> {
100100
}
101101

102102

103-
void _handleChanged() {
104-
if (widget.value == null) {
103+
void _handleChanged(bool selected) {
104+
bool selected = false;
105+
if(widget.value == widget.groupValue){
106+
selected = true;
107+
}
108+
if (selected == null) {
105109
widget.onChanged(null);
106110
return;
107111
}
108-
if (widget.value) {
109-
widget.onChanged(!widget.value);
112+
if (selected) {
113+
widget.onChanged(widget.value);
110114
}
111115
}
112116

@@ -117,23 +121,36 @@ class _GFRadioButtonState extends State<GFRadioButton> {
117121
enabled: enabled,
118122
child: InkWell(
119123
canRequestFocus: enabled,
120-
onTap: widget.onChanged != null ? () {widget.onChanged(widget.value);} : null,
124+
onTap: widget.onChanged != null ?
125+
() {
126+
bool selected = false;
127+
if(widget.value == widget.groupValue){
128+
selected = widget.value;
129+
}
130+
if (selected == null) {
131+
widget.onChanged(null);
132+
}
133+
if (selected) {
134+
widget.onChanged(widget.value);
135+
}
136+
return selected;
137+
} : null,
121138
// onTap: enabled ? _handleChanged : null,
122139
child: Container(
123140
height: widget.size,
124141
width: widget.size,
125142
decoration: BoxDecoration(
126-
color: widget.value ? widget.activebgColor : widget.inactivebgColor,
143+
color: enabled ? widget.activebgColor : widget.inactivebgColor,
127144
borderRadius: widget.type == GFRadioButtonType.basic
128145
? BorderRadius.circular(50)
129146
: widget.type == GFRadioButtonType.square
130147
? BorderRadius.circular(0)
131148
: BorderRadius.circular(10),
132149
border: Border.all(
133-
color: widget.value
150+
color: enabled
134151
? widget.activeBorderColor
135152
: widget.inactiveBorderColor)),
136-
child: widget.value
153+
child: enabled
137154
? widget.type == GFRadioButtonType.basic ||
138155
widget.type == GFRadioButtonType.square
139156
? Stack(

0 commit comments

Comments
 (0)