diff --git a/lib/custom_switch.dart b/lib/custom_switch.dart index c9cf4bf..ed0b463 100644 --- a/lib/custom_switch.dart +++ b/lib/custom_switch.dart @@ -6,23 +6,28 @@ class CustomSwitch extends StatefulWidget { final bool value; final ValueChanged<bool> onChanged; final Color activeColor; - final Color inactiveColor = Colors.grey; - final String activeText = 'On'; - final String inactiveText = 'Off'; - final Color activeTextColor = Colors.white70; - final Color inactiveTextColor = Colors.white70; + final Color inactiveColor; + final String activeText; + final String inactiveText; + final Color activeTextColor; + final Color inactiveTextColor; const CustomSwitch({ - Key key, - this.value, - this.onChanged, - this.activeColor, - this.inactiveColor, - this.activeText, - this.inactiveText, - this.activeTextColor, - this.inactiveTextColor}) - : super(key: key); + Key key, + this.value, + this.onChanged, + this.activeColor, + inactiveColor, + activeText, + inactiveText, + activeTextColor, + inactiveTextColor}) + : this.inactiveColor = inactiveColor ?? Colors.grey, + this.activeText = activeText ?? 'On', + this.inactiveText = inactiveText ?? 'Off', + this.activeTextColor = activeTextColor ?? Colors.white70, + this.inactiveTextColor = inactiveTextColor ?? Colors.white70, + super(key: key); @override _CustomSwitchState createState() => _CustomSwitchState(); @@ -39,10 +44,17 @@ class _CustomSwitchState extends State<CustomSwitch> _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 60)); _circleAnimation = AlignmentTween( - begin: widget.value ? Alignment.centerRight : Alignment.centerLeft, - end: widget.value ? Alignment.centerLeft : Alignment.centerRight) + begin: widget.value ? Alignment.centerRight : Alignment.centerLeft, + end: widget.value ? Alignment.centerLeft : Alignment.centerRight) .animate(CurvedAnimation( - parent: _animationController, curve: Curves.linear)); + parent: _animationController, curve: Curves.linear)); + } + + + @override + void dispose() { + _animationController ?? _animationController.dispose(); + super.dispose(); } @override @@ -77,15 +89,15 @@ class _CustomSwitchState extends State<CustomSwitch> children: <Widget>[ _circleAnimation.value == Alignment.centerRight ? Padding( - padding: const EdgeInsets.only(left: 4.0, right: 4.0), - child: Text( - widget.activeText, - style: TextStyle( - color: widget.activeTextColor, - fontWeight: FontWeight.w900, - fontSize: 16.0), - ), - ) + padding: const EdgeInsets.only(left: 4.0, right: 4.0), + child: Text( + widget.activeText, + style: TextStyle( + color: widget.activeTextColor, + fontWeight: FontWeight.w900, + fontSize: 16.0), + ), + ) : Container(), Align( alignment: _circleAnimation.value, @@ -98,15 +110,15 @@ class _CustomSwitchState extends State<CustomSwitch> ), _circleAnimation.value == Alignment.centerLeft ? Padding( - padding: const EdgeInsets.only(left: 4.0, right: 5.0), - child: Text( - widget.inactiveText, - style: TextStyle( - color: widget.inactiveTextColor, - fontWeight: FontWeight.w900, - fontSize: 16.0), - ), - ) + padding: const EdgeInsets.only(left: 4.0, right: 5.0), + child: Text( + widget.inactiveText, + style: TextStyle( + color: widget.inactiveTextColor, + fontWeight: FontWeight.w900, + fontSize: 16.0), + ), + ) : Container(), ], ),