Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions lib/custom_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
],
),
Expand Down