1- import 'package:flutter/cupertino.dart' ;
21import 'package:flutter/material.dart' ;
32import 'package:getflutter/types/gf_toggle_type.dart' ;
43
@@ -17,7 +16,7 @@ class GFToggle extends StatefulWidget {
1716 GFToggle (
1817 {Key key,
1918 @required this .onChanged,
20- @required this .value,
19+ this .value ,
2120 this .enabledText,
2221 this .disabledText,
2322 this .enabledTextStyle,
@@ -74,6 +73,7 @@ class GFToggle extends StatefulWidget {
7473 /// Called when the user toggles the switch on or off.
7574 final ValueChanged <bool > onChanged;
7675
76+
7777 @override
7878 _GFToggleState createState () => _GFToggleState ();
7979}
@@ -84,14 +84,19 @@ class _GFToggleState extends State<GFToggle> with TickerProviderStateMixin {
8484 AnimationController controller;
8585 Animation <Offset > offset;
8686
87- bool isOn = false ;
87+ bool isOn;
88+
89+
8890
8991 @override
9092 void initState () {
91- super .initState ();
93+ setState (() {
94+ isOn = widget.value?? false ;
95+ });
9296 controller = AnimationController (vsync: this , duration: widget.duration);
93- offset = Tween <Offset >(begin: Offset . zero, end: Offset (1.0 , 0.0 ))
97+ offset = (isOn ? Tween <Offset >(begin: Offset ( 1.0 , 0.0 ), end : Offset . zero) : Tween < Offset >(begin : Offset .zero , end: Offset (1.0 , 0.0 ) ))
9498 .animate (controller);
99+ super .initState ();
95100 }
96101
97102 @override
@@ -101,116 +106,105 @@ class _GFToggleState extends State<GFToggle> with TickerProviderStateMixin {
101106 super .dispose ();
102107 }
103108
109+ void onStatusChange (){
110+ if (widget.onChanged!= null ){
111+ setState (() {
112+ isOn = ! isOn;
113+ });
114+
115+ switch (controller.status) {
116+ case AnimationStatus .dismissed:
117+ controller.forward ();
118+ break ;
119+ case AnimationStatus .completed:
120+ controller.reverse ();
121+ break ;
122+ default :
123+ }
124+ widget.onChanged (isOn);
125+ }
126+ }
127+
104128 @override
105129 Widget build (BuildContext context) {
106- return GestureDetector (
107- onTap: () {
108- setState (() {
109- isOn = ! isOn;
110- });
111- switch (controller.status) {
112- case AnimationStatus .dismissed:
113- controller.forward ();
114- break ;
115- case AnimationStatus .completed:
116- controller.reverse ();
117- break ;
118- default :
119- }
120- if (widget.onChanged != null ) {
121- widget.onChanged (isOn);
122- }
123- },
124- child: Stack (
125- children: < Widget > [
126- Container (
127- height: widget.type == GFToggleType .android ? 25 : 30 ,
128- width: widget.type == GFToggleType .android ? 40 : 50 ,
129- ),
130- Positioned (
131- top: 5 ,
130+ return Stack (
131+ children: < Widget > [
132+ Container (
133+ height: widget.type == GFToggleType .android ? 25 : 30 ,
134+ width: widget.type == GFToggleType .android ? 40 : 50 ,
135+ ),
136+ Positioned (
137+ top: 5 ,
138+ child: InkWell (
139+ onTap: onStatusChange,
132140 child: Container (
133- width: widget.type == GFToggleType .ios ? 45 : 36 ,
134- height: widget.type == GFToggleType .ios ? 25 : 15 ,
135- decoration: BoxDecoration (
136- color: isOn
137- ? widget.enabledTrackColor ?? Colors .lightGreen
138- : widget.disabledTrackColor ?? Colors .grey,
139- borderRadius: widget.type == GFToggleType .square
140- ? BorderRadius .all (Radius .circular (0 ))
141- : widget.borderRadius ??
142- BorderRadius .all (Radius .circular (20 ))),
143- child: Padding (
144- padding: widget.type == GFToggleType .ios
145- ? EdgeInsets .only (left: 3.5 , right: 3.5 , top: 5.4 )
146- : EdgeInsets .only (left: 3 , right: 3 , top: 3.4 ),
147- child: isOn
148- ? Text (
149- widget.enabledText ??
150- (widget.type == GFToggleType .custom
151- ? 'ON'
152- : '' ),
153- style: widget.enabledTextStyle ??
154- (widget.type == GFToggleType .ios
155- ? TextStyle (
156- color: Colors .white, fontSize: 12 )
157- : TextStyle (
158- color: Colors .white, fontSize: 8 )))
159- : Text (
160- widget.disabledText ??
161- (widget.type == GFToggleType .custom
162- ? 'OFF'
163- : '' ),
164- textAlign: TextAlign .end,
165- style: widget.disabledTextStyle ??
166- (widget.type == GFToggleType .ios
167- ? TextStyle (color: Colors .white, fontSize: 12 )
168- : TextStyle (color: Colors .white, fontSize: 8 ))))),
169- ),
170- Positioned (
171- top: widget.type == GFToggleType .ios ? 7.5 : 3 ,
172- left: widget.type == GFToggleType .ios ? 2 : 0 ,
173- child: GestureDetector (
174- onTap: () {
175- setState (() {
176- isOn = ! isOn;
177- });
178- switch (controller.status) {
179- case AnimationStatus .dismissed:
180- controller.forward ();
181- break ;
182- case AnimationStatus .completed:
183- controller.reverse ();
184- break ;
185- default :
186- }
187- if (widget.onChanged != null ) {
188- widget.onChanged (isOn);
189- }
190- },
191- child: SlideTransition (
192- position: offset,
193- child: Container (
194- padding: EdgeInsets .only (left: 10 ),
195- height: 20 ,
196- width: 20 ,
197- decoration: BoxDecoration (
198- shape: widget.type == GFToggleType .square
199- ? BoxShape .rectangle
200- : widget.boxShape ?? BoxShape .circle,
201- color: isOn
202- ? widget.enabledThumbColor ?? Colors .white
203- : widget.disabledThumbColor ?? Colors .white,
204- boxShadow: [
205- new BoxShadow (
206- color: Colors .black.withOpacity (0.16 ),
207- blurRadius: 6.0 ,
208- spreadRadius: 0.0 ),
209- ]),
210- ),
211- ))),
212- ],
213- ),
214- );
141+ width: widget.type == GFToggleType .ios ? 45 : 36 ,
142+ height: widget.type == GFToggleType .ios ? 25 : 15 ,
143+ decoration: BoxDecoration (
144+ color: isOn
145+ ? widget.enabledTrackColor ?? Colors .lightGreen
146+ : widget.disabledTrackColor ?? Colors .grey,
147+ borderRadius: widget.type == GFToggleType .square
148+ ? BorderRadius .all (Radius .circular (0 ))
149+ : widget.borderRadius ??
150+ BorderRadius .all (Radius .circular (20 ))),
151+ child: Padding (
152+ padding: widget.type == GFToggleType .ios
153+ ? EdgeInsets .only (left: 3.5 , right: 3.5 , top: 5.4 )
154+ : EdgeInsets .only (left: 3 , right: 3 , top: 3.4 ),
155+ child: isOn
156+ ? Text (
157+ widget.enabledText ??
158+ (widget.type == GFToggleType .custom
159+ ? 'ON'
160+ : '' ),
161+ style: widget.enabledTextStyle ??
162+ (widget.type == GFToggleType .ios
163+ ? TextStyle (
164+ color: Colors .white, fontSize: 12 )
165+ : TextStyle (
166+ color: Colors .white, fontSize: 8 )))
167+ : Text (
168+ widget.disabledText ??
169+ (widget.type == GFToggleType .custom
170+ ? 'OFF'
171+ : '' ),
172+ textAlign: TextAlign .end,
173+ style: widget.disabledTextStyle ??
174+ (widget.type == GFToggleType .ios
175+ ? TextStyle (color: Colors .white, fontSize: 12 )
176+ : TextStyle (color: Colors .white, fontSize: 8 )),),),),
177+ ),),
178+ Positioned (
179+ top: widget.type == GFToggleType .ios ? 7.5 : 3 ,
180+ left: widget.type == GFToggleType .ios ? 2 : 0 ,
181+ child: InkWell (
182+ onTap: onStatusChange,
183+ child: SlideTransition (
184+ position: offset,
185+ child: Container (
186+ padding: EdgeInsets .only (left: 10 ),
187+ height: 20 ,
188+ width: 20 ,
189+ decoration: BoxDecoration (
190+ shape: widget.type == GFToggleType .square
191+ ? BoxShape .rectangle
192+ : widget.boxShape ?? BoxShape .circle,
193+ color: isOn
194+ ? widget.enabledThumbColor ?? Colors .white
195+ : widget.disabledThumbColor ?? Colors .white,
196+ boxShadow: [
197+ new BoxShadow (
198+ color: Colors .black.withOpacity (0.16 ),
199+ blurRadius: 6.0 ,
200+ spreadRadius: 0.0 ),
201+ ]),
202+ ),
203+ )
204+ )),
205+ ],
206+
207+ );
215208 }
216209}
210+
0 commit comments