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

Add option to use custom decoration instead of colors, border and border #45

Open
wants to merge 1 commit 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
86 changes: 68 additions & 18 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 @@ -129,6 +130,55 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
SizedBox(height: 20.0),
Text("Custom Decoration"),
SizedBox(height: 10.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
FlutterSwitch(
width: 100.0,
height: 55.0,
toggleSize: 45.0,
value: status3,
padding: 2.0,
toggleColor: Color.fromRGBO(225, 225, 225, 1),
toggleBorder: Border.all(
color: Color.fromRGBO(2, 107, 206, 1),
width: 5.0,
),
activeDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(
colors: [
Colors.red,
Colors.orange,
],
),
),
inactiveDecoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
gradient: LinearGradient(
colors: [
Colors.blue,
Colors.orange,
],
),
),
onToggle: (val) {
setState(() {
status3 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status3",
),
),
],
),
SizedBox(height: 20.0),
Text("With 'On' and 'Off' text and custom text colors"),
SizedBox(height: 10.0),
Row(
Expand All @@ -138,17 +188,17 @@ class _MyHomePageState extends State<MyHomePage> {
showOnOff: true,
activeTextColor: Colors.black,
inactiveTextColor: Colors.blue[50],
value: status3,
value: status4,
onToggle: (val) {
setState(() {
status3 = val;
status4 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status3",
"Value: $status4",
),
),
],
Expand All @@ -164,17 +214,17 @@ class _MyHomePageState extends State<MyHomePage> {
height: 25.0,
valueFontSize: 12.0,
toggleSize: 18.0,
value: status4,
value: status5,
onToggle: (val) {
setState(() {
status4 = val;
status5 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status4",
"Value: $status5",
),
),
],
Expand All @@ -190,20 +240,20 @@ class _MyHomePageState extends State<MyHomePage> {
height: 55.0,
valueFontSize: 25.0,
toggleSize: 45.0,
value: status5,
value: status6,
borderRadius: 30.0,
padding: 8.0,
showOnOff: true,
onToggle: (val) {
setState(() {
status5 = val;
status6 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status5",
"Value: $status6",
),
),
],
Expand All @@ -217,21 +267,21 @@ class _MyHomePageState extends State<MyHomePage> {
FlutterSwitch(
activeText: "All Good. Negative.",
inactiveText: "Under Quarantine.",
value: status6,
value: status7,
valueFontSize: 10.0,
width: 110,
borderRadius: 30.0,
showOnOff: true,
onToggle: (val) {
setState(() {
status6 = val;
status7 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text(
"Value: $status6",
"Value: $status7",
),
),
],
Expand All @@ -253,7 +303,7 @@ class _MyHomePageState extends State<MyHomePage> {
width: 100.0,
height: 55.0,
toggleSize: 45.0,
value: status7,
value: status8,
borderRadius: 30.0,
padding: 2.0,
activeToggleColor: Color(0xFF6E40C9),
Expand All @@ -278,7 +328,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
onToggle: (val) {
setState(() {
status7 = val;
status8 = val;

if (val) {
_textColor = Colors.white;
Expand All @@ -294,7 +344,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
Container(
alignment: Alignment.centerRight,
child: Text("Value: $status7"),
child: Text("Value: $status8"),
),
],
),
Expand All @@ -308,7 +358,7 @@ class _MyHomePageState extends State<MyHomePage> {
width: 100.0,
height: 55.0,
toggleSize: 45.0,
value: status8,
value: status9,
borderRadius: 30.0,
padding: 2.0,
activeToggleColor: Color(0xFF0082C8),
Expand All @@ -331,13 +381,13 @@ class _MyHomePageState extends State<MyHomePage> {
),
onToggle: (val) {
setState(() {
status8 = val;
status9 = val;
});
},
),
Container(
alignment: Alignment.centerRight,
child: Text("Value: $status8"),
child: Text("Value: $status9"),
),
],
),
Expand Down
29 changes: 23 additions & 6 deletions lib/flutter_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class FlutterSwitch extends StatefulWidget {
Key? key,
required this.value,
required this.onToggle,
this.activeDecoration,
this.activeColor = Colors.blue,
this.inactiveDecoration,
this.inactiveColor = Colors.grey,
this.activeTextColor = Colors.white70,
this.inactiveTextColor = Colors.white70,
Expand Down Expand Up @@ -111,11 +113,21 @@ class FlutterSwitch extends StatefulWidget {
/// [inactiveTextFontWeight] - The font weight to use on the text value when the switch is off.
final String? inactiveText;

/// The color to use on the switch when the switch is on.
/// The decoration to use on the switch when the switch is on.
/// If null the other values like [activeColor], [borderRadius]
/// and [activeSwitchBorder] will be used.
final Decoration? activeDecoration;

/// The color to use on the switch when the switch is off.
///
/// Defaults to [Colors.blue].
final Color activeColor;

/// The decoration to use on the switch when the switch is off.
/// If null the other values like [inactiveColor], [borderRadius]
/// and [inactiveSwitchBorder] will be used.
final Decoration? inactiveDecoration;

/// The color to use on the switch when the switch is off.
///
/// Defaults to [Colors.grey].
Expand Down Expand Up @@ -304,6 +316,9 @@ class _FlutterSwitchState extends State<FlutterSwitch>
Border? _switchBorder;
Border? _toggleBorder;

Decoration? decoration =
widget.value ? widget.activeDecoration : widget.inactiveDecoration;

if (widget.value) {
_toggleColor = widget.activeToggleColor ?? widget.toggleColor;
_switchColor = widget.activeColor;
Expand Down Expand Up @@ -345,11 +360,13 @@ class _FlutterSwitchState extends State<FlutterSwitch>
width: widget.width,
height: widget.height,
padding: EdgeInsets.all(widget.padding),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(widget.borderRadius),
color: _switchColor,
border: _switchBorder,
),
decoration: decoration ??
BoxDecoration(
borderRadius:
BorderRadius.circular(widget.borderRadius),
color: _switchColor,
border: _switchBorder,
),
child: Stack(
children: <Widget>[
AnimatedOpacity(
Expand Down