11import 'package:flutter/material.dart' ;
22import '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
7294class _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