Skip to content

Commit d5be7b5

Browse files
committed
changes in GFRadio
1 parent 7be645c commit d5be7b5

File tree

3 files changed

+127
-116
lines changed

3 files changed

+127
-116
lines changed

example/lib/main_temp.dart

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,32 @@ class _MyHomePageState extends State<MyHomePage>
161161
Radio(value: null, groupValue: null, onChanged: null),
162162
RadioListTile(value: null, groupValue: null, onChanged: null),
163163

164+
GFRadioButton(
165+
size: GFSize.SMALL,
166+
type: GFRadioButtonType.basic,
167+
radioColor: GFColors.SUCCESS,
168+
// activebgColor: GFColors.ALT,
169+
// inactivebgColor: GFColors.PRIMARY,
170+
// activeBorderColor: GFColors.DANGER,
171+
// inactiveBorderColor: GFColors.DARK,
172+
value: check,
173+
groupValue: check,
174+
onChanged: (val) {
175+
print('val $val');
176+
setState(() {
177+
check = val;
178+
});
179+
print('cch $check $val');
180+
},
181+
activeIcon: const Icon(
182+
Icons.check,
183+
size: 20,
184+
color: GFColors.DARK,
185+
),
186+
// inactiveIcon: const Icon(Icons.close, size: 20, color: GFColors.DARK,),
187+
// custombgColor: GFColors.SUCCESS,
188+
),
189+
164190
GFCheckboxListTile(
165191
color: GFColors.LIGHT,
166192
title: const Text('is checked'),
@@ -220,28 +246,6 @@ class _MyHomePageState extends State<MyHomePage>
220246
autofocus: true,
221247
),
222248

223-
GFRadioButton(
224-
size: GFSize.SMALL,
225-
value: true,
226-
type: GFRadioButtonType.basic,
227-
radioColor: GFColors.SUCCESS,
228-
// activebgColor: GFColors.ALT,
229-
// inactivebgColor: GFColors.PRIMARY,
230-
// activeBorderColor: GFColors.DANGER,
231-
// inactiveBorderColor: GFColors.DARK,
232-
onChanged: (val) {
233-
print('on change val $val');
234-
},
235-
activeIcon: const Icon(
236-
Icons.check,
237-
size: 20,
238-
color: GFColors.DARK,
239-
),
240-
// inactiveIcon: const Icon(Icons.close, size: 20, color: GFColors.DARK,),
241-
// custombgColor: GFColors.SUCCESS,
242-
// groupValue:
243-
),
244-
245249
Checkbox(
246250
value: check,
247251
onChanged: (bool value) {
@@ -428,28 +432,6 @@ class _MyHomePageState extends State<MyHomePage>
428432
),
429433
),
430434

431-
GFRadioButton(
432-
size: GFSize.SMALL,
433-
value: true,
434-
type: GFRadioButtonType.basic,
435-
radioColor: GFColors.SUCCESS,
436-
// activebgColor: GFColors.ALT,
437-
// inactivebgColor: GFColors.PRIMARY,
438-
// activeBorderColor: GFColors.DANGER,
439-
// inactiveBorderColor: GFColors.DARK,
440-
onChanged: (val) {
441-
print('on change val $val');
442-
},
443-
activeIcon: const Icon(
444-
Icons.check,
445-
size: 20,
446-
color: GFColors.DARK,
447-
),
448-
// inactiveIcon: const Icon(Icons.close, size: 20, color: GFColors.DARK,),
449-
// custombgColor: GFColors.SUCCESS,
450-
// groupValue:
451-
),
452-
453435
GFImageOverlay(
454436
height: 200.0,
455437
width: 200.0,

lib/components/checkbox/gf_checkbox.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:getwidget/getwidget.dart';
33

4+
45
class GFCheckbox extends StatefulWidget {
56
const GFCheckbox({
67
Key key,

lib/components/radio/gf_radio.dart

Lines changed: 100 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:getwidget/getwidget.dart';
33

4-
class GFRadioButton extends StatefulWidget {
4+
class GFRadioButton<T> extends StatefulWidget {
55
const GFRadioButton(
66
{Key key,
77
this.size = GFSize.SMALL,
@@ -24,8 +24,11 @@ class GFRadioButton extends StatefulWidget {
2424
color: GFColors.DARK,
2525
),
2626
this.custombgColor = GFColors.SUCCESS,
27-
this.groupValue})
28-
: super(key: key);
27+
this.groupValue,
28+
this.autofocus = false,
29+
this.focusNode
30+
}) : assert(autofocus != null),
31+
super(key: key);
2932

3033
/// type of [GFRadioButtonType] which is of four type is basic, sqaure, circular and custom
3134
final GFRadioButtonType type;
@@ -51,8 +54,14 @@ class GFRadioButton extends StatefulWidget {
5154
/// Called when the user checks or unchecks the checkbox.
5255
final ValueChanged<bool> onChanged;
5356

54-
///Used to set the current state of the checkbox
55-
final bool value;
57+
// ///Used to set the current state of the checkbox
58+
// final bool value;
59+
//
60+
// /// The currently selected value for a group of radio buttons.
61+
// ///
62+
// /// This radio button is considered selected if its [value] matches the
63+
// /// [groupValue].
64+
// final bool groupValue;
5665

5766
///type of Widget used to change the checkbox's active icon
5867
final Widget activeIcon;
@@ -63,87 +72,106 @@ class GFRadioButton extends StatefulWidget {
6372
/// type of [Color] used to change the background color of the custom active checkbox only
6473
final Color custombgColor;
6574

66-
final bool groupValue;
75+
/// {@macro flutter.widgets.Focus.focusNode}
76+
final FocusNode focusNode;
77+
78+
/// {@macro flutter.widgets.Focus.autofocus}
79+
final bool autofocus;
80+
81+
/// The value represented by this radio button.
82+
final T value;
83+
84+
/// The currently selected value for a group of radio buttons.
85+
///
86+
/// This radio button is considered selected if its [value] matches the
87+
/// [groupValue].
88+
final T groupValue;
6789

6890
@override
6991
_GFRadioButtonState createState() => _GFRadioButtonState();
7092
}
7193

7294
class _GFRadioButtonState extends State<GFRadioButton> {
73-
//
74-
75-
bool isSelected = false;
95+
bool get enabled => widget.onChanged != null;
7696

7797
@override
7898
void initState() {
7999
super.initState();
80-
isSelected = widget.value == widget.groupValue ?? false;
81100
}
82101

83-
void onStatusChange() {
84-
setState(() {
85-
isSelected = !isSelected;
86-
});
87-
if (widget.onChanged != null) {
88-
widget.onChanged(isSelected);
102+
103+
void _handleChanged() {
104+
if (widget.value == null) {
105+
widget.onChanged(null);
106+
return;
107+
}
108+
if (widget.value) {
109+
widget.onChanged(!widget.value);
89110
}
90111
}
91112

92113
@override
93-
Widget build(BuildContext context) => InkWell(
94-
onTap: onStatusChange,
95-
child: Container(
96-
height: widget.size,
97-
width: widget.size,
98-
decoration: BoxDecoration(
99-
color: isSelected ? widget.activebgColor : widget.inactivebgColor,
100-
borderRadius: widget.type == GFRadioButtonType.basic
101-
? BorderRadius.circular(50)
102-
: widget.type == GFRadioButtonType.square
103-
? BorderRadius.circular(0)
104-
: BorderRadius.circular(10),
105-
border: Border.all(
106-
color: isSelected
107-
? widget.activeBorderColor
108-
: widget.inactiveBorderColor)),
109-
child: isSelected
110-
? widget.type == GFRadioButtonType.basic ||
111-
widget.type == GFRadioButtonType.square
112-
? Stack(
113-
children: <Widget>[
114-
Container(
115-
alignment: Alignment.center,
116-
),
117-
Container(
118-
margin: const EdgeInsets.all(5),
119-
alignment: Alignment.center,
120-
width: widget.size * 0.7,
121-
height: widget.size * 0.7,
122-
decoration: BoxDecoration(
123-
shape: BoxShape.circle, color: widget.radioColor),
124-
)
125-
],
126-
)
127-
: widget.type == GFRadioButtonType.blunt
128-
? Stack(
129-
children: <Widget>[
130-
Container(
131-
alignment: Alignment.center,
132-
),
133-
Container(
134-
margin: const EdgeInsets.all(5),
135-
alignment: Alignment.center,
136-
width: widget.size * 0.8,
137-
height: widget.size * 0.8,
138-
decoration: BoxDecoration(
139-
borderRadius: const BorderRadius.all(
140-
Radius.circular(50)),
141-
color: widget.custombgColor),
142-
)
143-
],
144-
)
145-
: widget.type == GFRadioButtonType.custom
146-
? widget.activeIcon
147-
: widget.inactiveIcon
148-
: widget.inactiveIcon));
114+
Widget build(BuildContext context) => FocusableActionDetector(
115+
focusNode: widget.focusNode,
116+
autofocus: widget.autofocus,
117+
enabled: enabled,
118+
child: InkWell(
119+
canRequestFocus: enabled,
120+
onTap: widget.onChanged != null ? () {widget.onChanged(widget.value);} : null,
121+
// onTap: enabled ? _handleChanged : null,
122+
child: Container(
123+
height: widget.size,
124+
width: widget.size,
125+
decoration: BoxDecoration(
126+
color: widget.value ? 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: widget.value
134+
? widget.activeBorderColor
135+
: widget.inactiveBorderColor)),
136+
child: widget.value
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)),
176+
);
149177
}

0 commit comments

Comments
 (0)