@@ -41,6 +41,7 @@ class FlutterSwitch extends StatefulWidget {
4141 this .inactiveToggleBorder,
4242 this .activeIcon,
4343 this .inactiveIcon,
44+ this .duration = const Duration (milliseconds: 200 ),
4445 }) : assert (
4546 (toggleColor == null || activeToggleColor == null ) &&
4647 (toggleColor == null || inactiveToggleColor == null ),
@@ -242,6 +243,11 @@ class FlutterSwitch extends StatefulWidget {
242243 /// This property is optional.
243244 final Icon inactiveIcon;
244245
246+ /// The duration in milliseconds to change the state of the switch
247+ ///
248+ /// Defaults to the value of 200 milliseconds.
249+ final Duration duration;
250+
245251 @override
246252 _FlutterSwitchState createState () => _FlutterSwitchState ();
247253}
@@ -257,13 +263,16 @@ class _FlutterSwitchState extends State<FlutterSwitch>
257263 _animationController = AnimationController (
258264 vsync: this ,
259265 value: widget.value ? 1.0 : 0.0 ,
260- duration: Duration (milliseconds : 60 ) ,
266+ duration: widget.duration ,
261267 );
262268 _toggleAnimation = AlignmentTween (
263269 begin: Alignment .centerLeft,
264270 end: Alignment .centerRight,
265271 ).animate (
266- CurvedAnimation (parent: _animationController, curve: Curves .linear),
272+ CurvedAnimation (
273+ parent: _animationController,
274+ curve: Curves .linear,
275+ ),
267276 );
268277 }
269278
@@ -291,22 +300,21 @@ class _FlutterSwitchState extends State<FlutterSwitch>
291300 Color _switchColor = Colors .white;
292301 Border _switchBorder;
293302 Border _toggleBorder;
294- Widget _icon;
295303
296304 if (widget.value) {
297305 _toggleColor = widget.activeToggleColor ?? widget.toggleColor;
298306 _switchColor = widget.activeColor;
299307 _switchBorder = widget.activeSwitchBorder ?? widget.switchBorder;
300308 _toggleBorder = widget.activeToggleBorder ?? widget.toggleBorder;
301- _icon = widget.activeIcon;
302309 } else {
303310 _toggleColor = widget.inactiveToggleColor ?? widget.toggleColor;
304311 _switchColor = widget.inactiveColor;
305312 _switchBorder = widget.inactiveSwitchBorder ?? widget.switchBorder;
306313 _toggleBorder = widget.inactiveToggleBorder ?? widget.toggleBorder;
307- _icon = widget.inactiveIcon;
308314 }
309315
316+ double _textSpace = widget.width - widget.toggleSize;
317+
310318 return AnimatedBuilder (
311319 animation: _animationController,
312320 builder: (context, child) {
@@ -329,39 +337,65 @@ class _FlutterSwitchState extends State<FlutterSwitch>
329337 color: _switchColor,
330338 border: _switchBorder,
331339 ),
332- child: Row (
333- mainAxisAlignment: MainAxisAlignment .spaceBetween,
340+ child: Stack (
334341 children: < Widget > [
335- _toggleAnimation.value == Alignment .centerRight
336- ? Expanded (
337- child: Container (
338- padding: EdgeInsets .symmetric (horizontal: 4.0 ),
339- child: _activeText,
340- ),
341- )
342- : Container (),
343- Align (
344- alignment: _toggleAnimation.value,
342+ AnimatedOpacity (
343+ opacity: widget.value ? 1.0 : 0.0 ,
344+ duration: widget.duration,
345345 child: Container (
346- width: widget.toggleSize,
347- height: widget.toggleSize,
348- decoration: BoxDecoration (
349- shape: BoxShape .circle,
350- color: _toggleColor ?? Colors .white,
351- border: _toggleBorder,
346+ width: _textSpace,
347+ padding: EdgeInsets .symmetric (horizontal: 4.0 ),
348+ alignment: Alignment .centerLeft,
349+ child: _activeText,
350+ ),
351+ ),
352+ Align (
353+ alignment: Alignment .centerRight,
354+ child: AnimatedOpacity (
355+ opacity: ! widget.value ? 1.0 : 0.0 ,
356+ duration: widget.duration,
357+ child: Container (
358+ width: _textSpace,
359+ padding: EdgeInsets .symmetric (horizontal: 4.0 ),
360+ alignment: Alignment .centerRight,
361+ child: _inactiveText,
352362 ),
353- child: _icon,
354363 ),
355364 ),
356- _toggleAnimation.value == Alignment .centerLeft
357- ? Expanded (
358- child: Container (
359- padding: EdgeInsets .symmetric (horizontal: 4.0 ),
360- alignment: Alignment .centerRight,
361- child: _inactiveText,
365+ Container (
366+ child: Align (
367+ alignment: _toggleAnimation.value,
368+ child: Container (
369+ width: widget.toggleSize,
370+ height: widget.toggleSize,
371+ decoration: BoxDecoration (
372+ shape: BoxShape .circle,
373+ color: _toggleColor ?? Colors .white,
374+ border: _toggleBorder,
375+ ),
376+ child: Container (
377+ child: Stack (
378+ children: [
379+ Center (
380+ child: AnimatedOpacity (
381+ opacity: widget.value ? 1.0 : 0.0 ,
382+ duration: widget.duration,
383+ child: widget.activeIcon,
384+ ),
385+ ),
386+ Center (
387+ child: AnimatedOpacity (
388+ opacity: ! widget.value ? 1.0 : 0.0 ,
389+ duration: widget.duration,
390+ child: widget.inactiveIcon,
391+ ),
392+ ),
393+ ],
362394 ),
363- )
364- : Container (),
395+ ),
396+ ),
397+ ),
398+ ),
365399 ],
366400 ),
367401 ),
0 commit comments