From f253f2a798a277769e4f7cff25a214e653b80e49 Mon Sep 17 00:00:00 2001 From: Satyajit Ghana Date: Wed, 14 Oct 2020 22:25:25 +0530 Subject: [PATCH] Update: Add default parameters, animation dispose Default parameters cannot be initialized again using `this.param`, fixed that added animation dispose --- lib/custom_switch.dart | 84 ++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 36 deletions(-) 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 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 _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 children: [ _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 ), _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(), ], ),