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