Skip to content

Commit 5700f19

Browse files
committed
GFRadio has been changed
1 parent 051e500 commit 5700f19

File tree

4 files changed

+287
-100
lines changed

4 files changed

+287
-100
lines changed

example/lib/main_temp.dart

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,30 +161,46 @@ class _MyHomePageState extends State<MyHomePage>
161161
value: 0,
162162
groupValue: groupValue,
163163
onChanged: (val) {
164-
print('val $val');
165164
setState(() {
166165
groupValue = val;
167166
});
168-
print('radio $groupValue $val');
169167
},
170168
),
171169

172170
Radio(
173171
value: 1,
174172
groupValue: groupValue,
175173
onChanged: (val) {
176-
print('val $val');
177174
setState(() {
178175
groupValue = val;
179176
});
180-
print('radio $groupValue $val');
181177
},
182178
),
183-
// RadioListTile(value: null, groupValue: null, onChanged: null),
179+
GFRadioListTile(
180+
title: Text("hjj"),
181+
value: 0,
182+
groupValue: groupValue,
183+
onChanged: (val) {
184+
setState(() {
185+
groupValue = val;
186+
});
187+
},
188+
),
184189

185-
GFRadioButton(
190+
GFRadioListTile(
191+
title: Text("hjjnjklklk"),
192+
value: 1,
193+
groupValue: groupValue,
194+
onChanged: (val) {
195+
setState(() {
196+
groupValue = val;
197+
});
198+
},
199+
),
200+
201+
GFRadio(
186202
size: GFSize.SMALL,
187-
type: GFRadioButtonType.basic,
203+
type: GFRadioType.basic,
188204
radioColor: GFColors.SUCCESS,
189205
// activebgColor: GFColors.ALT,
190206
// inactivebgColor: GFColors.PRIMARY,
@@ -193,11 +209,9 @@ class _MyHomePageState extends State<MyHomePage>
193209
value: 0,
194210
groupValue: groupValue,
195211
onChanged: (val) {
196-
print('val $val');
197-
// setState(() {
198-
// groupValue = val;
199-
// });
200-
print('radio $groupValue $val');
212+
setState(() {
213+
groupValue = val;
214+
});
201215
},
202216
activeIcon: const Icon(
203217
Icons.check,
@@ -207,10 +221,10 @@ class _MyHomePageState extends State<MyHomePage>
207221
// inactiveIcon: const Icon(Icons.close, size: 20, color: GFColors.DARK,),
208222
// custombgColor: GFColors.SUCCESS,
209223
),
210-
211-
GFRadioButton(
224+
//
225+
GFRadio(
212226
size: GFSize.SMALL,
213-
type: GFRadioButtonType.basic,
227+
type: GFRadioType.basic,
214228
radioColor: GFColors.SUCCESS,
215229
// activebgColor: GFColors.ALT,
216230
// inactivebgColor: GFColors.PRIMARY,
@@ -219,11 +233,9 @@ class _MyHomePageState extends State<MyHomePage>
219233
value: 1,
220234
groupValue: groupValue,
221235
onChanged: (val) {
222-
print('val $val');
223-
// setState(() {
224-
// groupValue = val;
225-
// });
226-
print('radio $groupValue $val');
236+
setState(() {
237+
groupValue = val;
238+
});
227239
},
228240
activeIcon: const Icon(
229241
Icons.check,

lib/components/radio/gf_radio.dart

Lines changed: 91 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import 'package:flutter/material.dart';
22
import 'package:getwidget/getwidget.dart';
3+
import 'package:flutter/foundation.dart';
4+
import 'package:flutter/rendering.dart';
5+
import 'package:flutter/widgets.dart';
36

4-
class GFRadioButton<T> extends StatefulWidget {
5-
const GFRadioButton(
7+
class GFRadio<T> extends StatefulWidget {
8+
const GFRadio(
69
{Key key,
10+
@required this.value,
11+
@required this.groupValue,
12+
@required this.onChanged,
713
this.size = GFSize.SMALL,
8-
this.type = GFRadioButtonType.basic,
14+
this.type = GFRadioType.basic,
915
this.radioColor = GFColors.SUCCESS,
1016
this.activebgColor = GFColors.WHITE,
1117
this.inactivebgColor = GFColors.WHITE,
1218
this.activeBorderColor = GFColors.DARK,
1319
this.inactiveBorderColor = GFColors.DARK,
14-
this.onChanged,
15-
this.value,
1620
this.activeIcon = const Icon(
1721
Icons.check,
1822
size: 20,
@@ -24,16 +28,15 @@ class GFRadioButton<T> extends StatefulWidget {
2428
color: GFColors.DARK,
2529
),
2630
this.custombgColor = GFColors.SUCCESS,
27-
this.groupValue,
2831
this.autofocus = false,
2932
this.focusNode,
3033
this.toggleable = false})
3134
: assert(autofocus != null),
3235
assert(toggleable != null),
3336
super(key: key);
3437

35-
/// type of [GFRadioButtonType] which is of four type is basic, sqaure, circular and custom
36-
final GFRadioButtonType type;
38+
/// type of [GFRadioType] which is of four type is basic, sqaure, circular and custom
39+
final GFRadioType type;
3740

3841
/// type of [double] which is GFSize ie, small, medium and large and can use any double value
3942
final double size;
@@ -54,7 +57,7 @@ class GFRadioButton<T> extends StatefulWidget {
5457
final Color inactiveBorderColor;
5558

5659
/// Called when the user checks or unchecks the checkbox.
57-
final ValueChanged<bool> onChanged;
60+
final ValueChanged<T> onChanged;
5861

5962
// ///Used to set the current state of the checkbox
6063
// final bool value;
@@ -92,85 +95,94 @@ class GFRadioButton<T> extends StatefulWidget {
9295
final bool toggleable;
9396

9497
@override
95-
_GFRadioButtonState<T> createState() => _GFRadioButtonState<T>();
98+
_GFRadioState<T> createState() => _GFRadioState<T>();
9699
}
97100

98-
class _GFRadioButtonState<T> extends State<GFRadioButton<T>>
101+
class _GFRadioState<T> extends State<GFRadio<T>>
99102
with TickerProviderStateMixin {
100103
bool get enabled => widget.onChanged != null;
101-
bool isSelected = false;
104+
bool selected = false;
105+
var groupValue;
102106

103-
@override
104-
void initState() {
105-
super.initState();
106-
isSelected = widget.value == widget.groupValue ?? false;
107+
void onStatusChange() {
108+
print('wer ${widget.value == widget.groupValue}');
109+
groupValue = widget.value;
110+
_handleChanged(widget.value == groupValue);
107111
}
108112

109-
void onStatusChange() {
110-
setState(() {
111-
isSelected = !isSelected;
112-
});
113-
if (widget.onChanged != null) {
114-
widget.onChanged(isSelected);
113+
void _handleChanged(bool selected) {
114+
if (selected == null) {
115+
widget.onChanged(null);
116+
return;
117+
}
118+
if (selected) {
119+
widget.onChanged(widget.value);
115120
}
116121
}
117122

118123
@override
119-
Widget build(BuildContext context) => InkWell(
120-
enableFeedback: enabled,
121-
onTap: onStatusChange,
122-
child: Container(
123-
height: widget.size,
124-
width: widget.size,
125-
decoration: BoxDecoration(
126-
color: isSelected ? widget.activebgColor : widget.inactivebgColor,
127-
borderRadius: widget.type == GFRadioButtonType.basic
128-
? BorderRadius.circular(50)
129-
: widget.type == GFRadioButtonType.square
130-
? BorderRadius.circular(0)
131-
: BorderRadius.circular(10),
132-
border: Border.all(
133-
color: isSelected
134-
? widget.activeBorderColor
135-
: widget.inactiveBorderColor)),
136-
child: isSelected
137-
? widget.type == GFRadioButtonType.basic ||
138-
widget.type == GFRadioButtonType.square
139-
? Stack(
140-
children: <Widget>[
141-
Container(
142-
alignment: Alignment.center,
143-
),
144-
Container(
145-
margin: const EdgeInsets.all(5),
146-
alignment: Alignment.center,
147-
width: widget.size * 0.7,
148-
height: widget.size * 0.7,
149-
decoration: BoxDecoration(
150-
shape: BoxShape.circle, color: widget.radioColor),
151-
)
152-
],
153-
)
154-
: widget.type == GFRadioButtonType.blunt
155-
? Stack(
156-
children: <Widget>[
157-
Container(
158-
alignment: Alignment.center,
159-
),
160-
Container(
161-
margin: const EdgeInsets.all(5),
162-
alignment: Alignment.center,
163-
width: widget.size * 0.8,
164-
height: widget.size * 0.8,
165-
decoration: BoxDecoration(
166-
borderRadius: const BorderRadius.all(
167-
Radius.circular(50)),
168-
color: widget.custombgColor),
169-
)
170-
],
171-
)
172-
: widget.type == GFRadioButtonType.custom
173-
? widget.activeIcon
174-
: widget.inactiveIcon
175-
: widget.inactiveIcon));
124+
Widget build(BuildContext context) {
125+
// print('gr ${widget.value} ${widget.groupValue}');
126+
selected = widget.value == widget.groupValue;
127+
// print('sel $selected');
128+
129+
return InkWell(
130+
enableFeedback: enabled,
131+
onTap: onStatusChange,
132+
child: Container(
133+
height: widget.size,
134+
width: widget.size,
135+
decoration: BoxDecoration(
136+
color: selected ? widget.activebgColor : widget
137+
.inactivebgColor,
138+
borderRadius: widget.type == GFRadioType.basic
139+
? BorderRadius.circular(50)
140+
: widget.type == GFRadioType.square
141+
? BorderRadius.circular(0)
142+
: BorderRadius.circular(10),
143+
border: Border.all(
144+
color: selected
145+
? widget.activeBorderColor
146+
: widget.inactiveBorderColor)),
147+
child: selected
148+
? widget.type == GFRadioType.basic ||
149+
widget.type == GFRadioType.square
150+
? Stack(
151+
children: <Widget>[
152+
Container(
153+
alignment: Alignment.center,
154+
),
155+
Container(
156+
margin: const EdgeInsets.all(5),
157+
alignment: Alignment.center,
158+
width: widget.size * 0.7,
159+
height: widget.size * 0.7,
160+
decoration: BoxDecoration(
161+
shape: BoxShape.circle, color: widget.radioColor),
162+
)
163+
],
164+
)
165+
: widget.type == GFRadioType.blunt
166+
? Stack(
167+
children: <Widget>[
168+
Container(
169+
alignment: Alignment.center,
170+
),
171+
Container(
172+
margin: const EdgeInsets.all(5),
173+
alignment: Alignment.center,
174+
width: widget.size * 0.8,
175+
height: widget.size * 0.8,
176+
decoration: BoxDecoration(
177+
borderRadius: const BorderRadius.all(
178+
Radius.circular(50)),
179+
color: widget.custombgColor),
180+
)
181+
],
182+
)
183+
: widget.type == GFRadioType.custom
184+
? widget.activeIcon
185+
: widget.inactiveIcon
186+
: widget.inactiveIcon));
187+
}
176188
}

0 commit comments

Comments
 (0)