@@ -41,8 +41,7 @@ class FlutterSwitch extends StatefulWidget {
4141 this .inactiveToggleBorder,
4242 this .activeIcon,
4343 this .inactiveIcon,
44- this .duration,
45- this .curve,
44+ this .duration = const Duration (milliseconds: 200 ),
4645 }) : assert (
4746 (toggleColor == null || activeToggleColor == null ) &&
4847 (toggleColor == null || inactiveToggleColor == null ),
@@ -246,14 +245,9 @@ class FlutterSwitch extends StatefulWidget {
246245
247246 /// The duration in milliseconds to change the state of the switch
248247 ///
249- /// https://api.flutter.dev/flutter/dart-core/Duration-class.html
248+ /// Defaults to the value of 200 milliseconds.
250249 final Duration duration;
251250
252- /// An parametric animation easing curve, i.e. a mapping of the unit interval to the unit interval.
253- ///
254- /// https://api.flutter.dev/flutter/animation/Curves-class.html
255- final Curve curve;
256-
257251 @override
258252 _FlutterSwitchState createState () => _FlutterSwitchState ();
259253}
@@ -269,15 +263,15 @@ class _FlutterSwitchState extends State<FlutterSwitch>
269263 _animationController = AnimationController (
270264 vsync: this ,
271265 value: widget.value ? 1.0 : 0.0 ,
272- duration: widget.duration ?? Duration (milliseconds : 60 ) ,
266+ duration: widget.duration,
273267 );
274268 _toggleAnimation = AlignmentTween (
275269 begin: Alignment .centerLeft,
276270 end: Alignment .centerRight,
277271 ).animate (
278272 CurvedAnimation (
279273 parent: _animationController,
280- curve: widget.curve ?? Curves .linear,
274+ curve: Curves .linear,
281275 ),
282276 );
283277 }
@@ -306,22 +300,21 @@ class _FlutterSwitchState extends State<FlutterSwitch>
306300 Color _switchColor = Colors .white;
307301 Border _switchBorder;
308302 Border _toggleBorder;
309- Widget _icon;
310303
311304 if (widget.value) {
312305 _toggleColor = widget.activeToggleColor ?? widget.toggleColor;
313306 _switchColor = widget.activeColor;
314307 _switchBorder = widget.activeSwitchBorder ?? widget.switchBorder;
315308 _toggleBorder = widget.activeToggleBorder ?? widget.toggleBorder;
316- _icon = widget.activeIcon;
317309 } else {
318310 _toggleColor = widget.inactiveToggleColor ?? widget.toggleColor;
319311 _switchColor = widget.inactiveColor;
320312 _switchBorder = widget.inactiveSwitchBorder ?? widget.switchBorder;
321313 _toggleBorder = widget.inactiveToggleBorder ?? widget.toggleBorder;
322- _icon = widget.inactiveIcon;
323314 }
324315
316+ double _textSpace = widget.width - widget.toggleSize;
317+
325318 return AnimatedBuilder (
326319 animation: _animationController,
327320 builder: (context, child) {
@@ -346,11 +339,28 @@ class _FlutterSwitchState extends State<FlutterSwitch>
346339 ),
347340 child: Stack (
348341 children: < Widget > [
349- Container (
350- alignment: Alignment .center,
351- child: _toggleAnimation.value == Alignment .centerRight
352- ? _activeText
353- : _inactiveText,
342+ AnimatedOpacity (
343+ opacity: widget.value ? 1.0 : 0.0 ,
344+ duration: widget.duration,
345+ child: Container (
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,
362+ ),
363+ ),
354364 ),
355365 Container (
356366 child: Align (
@@ -363,7 +373,26 @@ class _FlutterSwitchState extends State<FlutterSwitch>
363373 color: _toggleColor ?? Colors .white,
364374 border: _toggleBorder,
365375 ),
366- child: _icon,
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+ ],
394+ ),
395+ ),
367396 ),
368397 ),
369398 ),
0 commit comments