diff --git a/README.md b/README.md index f91b50f..5594a3d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ An easy to implement custom switch created for Flutter. Give it a custom height ## Demo - ![flutter_switch](https://media0.giphy.com/media/zQ7AwA3SiAzqiFCW1o/giphy.gif) + ![flutter_switch](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNjdqeDV2YWo4YjZ0bXNvcHRzYjFoanUyaGdoNDQ0ZGh1Y2g3ZHQzdCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/0bEpvlWRt6aXPdyyxP/giphy.gif) Video version: https://youtu.be/JqJnxjE6Efc @@ -62,6 +62,21 @@ class _MyHomePageState extends State { borderRadius: 30.0, padding: 8.0, showOnOff: true, + gradient: + LinearGradient( + begin: Alignment.topLeft, + end: Alignment(0.8, 1), + colors: [ + Color(0xff1f005c), + Color(0xff5b0060), + Color(0xff870160), + Color(0xffac255e), + Color(0xffca485c), + Color(0xffe16b5c), + Color(0xfff39060), + Color(0xffffb56b), + ], + ), onToggle: (val) { setState(() { status = val; diff --git a/example/lib/main.dart b/example/lib/main.dart index 61cb3d7..d6d0a9e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,6 +29,7 @@ class _MyHomePageState extends State { bool status6 = false; bool status7 = false; bool status8 = false; + bool status9 = false; bool isSwitchOn = false; Color _textColor = Colors.black; @@ -341,6 +342,45 @@ class _MyHomePageState extends State { ), ], ), + SizedBox(height: 20.0), + Text("Gradient Example"), + SizedBox(height: 10.0), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + FlutterSwitch( + value: status9, + borderRadius: 30.0, + padding: 2.0, + gradient: LinearGradient( + begin: Alignment.topLeft, + end: Alignment(0.8, 1), + colors: [ + Color(0xff1f005c), + Color(0xff5b0060), + Color(0xff870160), + Color(0xffac255e), + Color(0xffca485c), + Color(0xffe16b5c), + Color(0xfff39060), + Color(0xffffb56b), + ], + ), + onToggle: (val) { + setState(() { + status9 = val; + }); + }, + ), + Container( + alignment: Alignment.centerRight, + child: Text( + "Value: $status9", + ), + ), + ], + ), + SizedBox(height: 20.0), ], ), ), diff --git a/lib/flutter_switch.dart b/lib/flutter_switch.dart index e30ca12..4b12f30 100644 --- a/lib/flutter_switch.dart +++ b/lib/flutter_switch.dart @@ -43,6 +43,7 @@ class FlutterSwitch extends StatefulWidget { this.inactiveIcon, this.duration = const Duration(milliseconds: 200), this.disabled = false, + this.gradient, }) : assert( (switchBorder == null || activeSwitchBorder == null) && (switchBorder == null || inactiveSwitchBorder == null), @@ -251,6 +252,8 @@ class FlutterSwitch extends StatefulWidget { /// Defaults to the value of false. final bool disabled; + final Gradient? gradient; + @override _FlutterSwitchState createState() => _FlutterSwitchState(); } @@ -301,12 +304,14 @@ class _FlutterSwitchState extends State Widget build(BuildContext context) { Color _toggleColor = Colors.white; Color _switchColor = Colors.white; + Gradient? _gradient; Border? _switchBorder; Border? _toggleBorder; if (widget.value) { _toggleColor = widget.activeToggleColor ?? widget.toggleColor; _switchColor = widget.activeColor; + _gradient = widget.gradient; _switchBorder = widget.activeSwitchBorder as Border? ?? widget.switchBorder as Border?; _toggleBorder = widget.activeToggleBorder as Border? ?? @@ -314,6 +319,7 @@ class _FlutterSwitchState extends State } else { _toggleColor = widget.inactiveToggleColor ?? widget.toggleColor; _switchColor = widget.inactiveColor; + _gradient = null; _switchBorder = widget.inactiveSwitchBorder as Border? ?? widget.switchBorder as Border?; _toggleBorder = widget.inactiveToggleBorder as Border? ?? @@ -346,6 +352,7 @@ class _FlutterSwitchState extends State height: widget.height, padding: EdgeInsets.all(widget.padding), decoration: BoxDecoration( + gradient: _gradient, borderRadius: BorderRadius.circular(widget.borderRadius), color: _switchColor, border: _switchBorder,