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 Added To Flutter Switch Library #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -62,6 +62,21 @@ class _MyHomePageState extends State<MyHomePage> {
borderRadius: 30.0,
padding: 8.0,
showOnOff: true,
gradient:
LinearGradient(
begin: Alignment.topLeft,
end: Alignment(0.8, 1),
colors: <Color>[
Color(0xff1f005c),
Color(0xff5b0060),
Color(0xff870160),
Color(0xffac255e),
Color(0xffca485c),
Color(0xffe16b5c),
Color(0xfff39060),
Color(0xffffb56b),
],
),
onToggle: (val) {
setState(() {
status = val;
Expand Down
40 changes: 40 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class _MyHomePageState extends State<MyHomePage> {
bool status6 = false;
bool status7 = false;
bool status8 = false;
bool status9 = false;
bool isSwitchOn = false;

Color _textColor = Colors.black;
Expand Down Expand Up @@ -341,6 +342,45 @@ class _MyHomePageState extends State<MyHomePage> {
),
],
),
SizedBox(height: 20.0),
Text("Gradient Example"),
SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlutterSwitch(
value: status9,
borderRadius: 30.0,
padding: 2.0,
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment(0.8, 1),
colors: <Color>[
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),
],
),
),
Expand Down
7 changes: 7 additions & 0 deletions lib/flutter_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -251,6 +252,8 @@ class FlutterSwitch extends StatefulWidget {
/// Defaults to the value of false.
final bool disabled;

final Gradient? gradient;

@override
_FlutterSwitchState createState() => _FlutterSwitchState();
}
Expand Down Expand Up @@ -301,19 +304,22 @@ class _FlutterSwitchState extends State<FlutterSwitch>
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? ??
widget.toggleBorder as Border?;
} 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? ??
Expand Down Expand Up @@ -346,6 +352,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
height: widget.height,
padding: EdgeInsets.all(widget.padding),
decoration: BoxDecoration(
gradient: _gradient,
borderRadius: BorderRadius.circular(widget.borderRadius),
color: _switchColor,
border: _switchBorder,
Expand Down