Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradient for the switch background #57

Open
wants to merge 2 commits into
base: add-code-of-conduct-1
Choose a base branch
from
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
160 changes: 88 additions & 72 deletions lib/flutter_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FlutterSwitch extends StatefulWidget {
this.inactiveTextColor = Colors.white70,
this.toggleColor = Colors.white,
this.activeToggleColor,
this.activeGradient,
this.inactiveToggleColor,
this.width = 70.0,
this.height = 35.0,
Expand Down Expand Up @@ -116,6 +117,8 @@ class FlutterSwitch extends StatefulWidget {
/// Defaults to [Colors.blue].
final Color activeColor;

/// The Gradient to use on the switch when the switch is on.
final Gradient? activeGradient;
/// The color to use on the switch when the switch is off.
///
/// Defaults to [Colors.grey].
Expand Down Expand Up @@ -230,14 +233,16 @@ class FlutterSwitch extends StatefulWidget {
final BoxBorder? inactiveToggleBorder;

/// The icon inside the toggle when the given value is true.
/// activeIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Icon? activeIcon;
final Widget? activeIcon;

/// The icon inside the toggle when the given value is false.
/// inactiveIcon can be an Icon Widget, an Image or Fontawesome Icons.
///
/// This property is optional.
final Icon? inactiveIcon;
final Widget? inactiveIcon;

/// The duration in milliseconds to change the state of the switch
///
Expand Down Expand Up @@ -301,15 +306,18 @@ class _FlutterSwitchState extends State<FlutterSwitch>
Color _switchColor = Colors.white;
Border? _switchBorder;
Border? _toggleBorder;
Gradient? _gradient = widget.activeGradient;

if (widget.value) {
_gradient = widget.gradient;
_toggleColor = widget.activeToggleColor ?? widget.toggleColor;
_switchColor = widget.activeColor;
_switchBorder = widget.activeSwitchBorder as Border? ??
widget.switchBorder as Border?;
_toggleBorder = widget.activeToggleBorder as Border? ??
widget.toggleBorder as Border?;
} else {
_gradient = null;
_toggleColor = widget.inactiveToggleColor ?? widget.toggleColor;
_switchColor = widget.inactiveColor;
_switchBorder = widget.inactiveSwitchBorder as Border? ??
Expand All @@ -323,89 +331,97 @@ class _FlutterSwitchState extends State<FlutterSwitch>
return AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
return Align(
child: GestureDetector(
onTap: () {
if (!widget.disabled) {
if (widget.value)
_animationController.forward();
else
_animationController.reverse();

widget.onToggle(!widget.value);
}
},
child: Opacity(
opacity: widget.disabled ? 0.6 : 1,
child: Container(
width: widget.width,
height: widget.height,
padding: EdgeInsets.all(widget.padding),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.borderRadius),
color: _switchColor,
border: _switchBorder,
),
child: Stack(
children: <Widget>[
AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerLeft,
child: _activeText,
),
),
Align(
alignment: Alignment.centerRight,
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
return Container(
width: widget.width,
child: Align(
child: GestureDetector(
onTap: () {
if (!widget.disabled) {
if (widget.value)
_animationController.forward();
else
_animationController.reverse();

widget.onToggle(!widget.value);
}
},
child: Opacity(
opacity: widget.disabled ? 0.6 : 1,
child: Container(
width: widget.width,
height: widget.height,
padding: EdgeInsets.all(widget.padding),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.borderRadius),
color: _switchColor,
gradient: _gradient,
border: _switchBorder,
),
child: Stack(
children: <Widget>[
AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerRight,
child: _inactiveText,
alignment: Alignment.centerLeft,
child: _activeText,
),
),
),
Container(
child: Align(
alignment: _toggleAnimation.value,
child: Container(
width: widget.toggleSize,
height: widget.toggleSize,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _toggleColor,
border: _toggleBorder,
Align(
alignment: Alignment.centerRight,
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: Container(
width: _textSpace,
padding: EdgeInsets.symmetric(horizontal: 4.0),
alignment: Alignment.centerRight,
child: _inactiveText,
),
),
),
Container(
child: Align(
alignment: _toggleAnimation.value,
child: Container(
child: Stack(
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
),
),
Center(
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
),
width: widget.toggleSize,
height: widget.toggleSize,
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _toggleColor,
border: _toggleBorder,
),
child: FittedBox(
fit: BoxFit.contain,
child: Container(
child: Stack(
children: [
Center(
child: AnimatedOpacity(
opacity: widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.activeIcon,
),
),
Center(
child: AnimatedOpacity(
opacity: !widget.value ? 1.0 : 0.0,
duration: widget.duration,
child: widget.inactiveIcon,
),
),
],
),
],
),
),
),
),
),
),
],
],
),
),
),
),
Expand Down