Skip to content

Commit aa04df5

Browse files
committed
Implementing custom text on switch
1 parent 4f0bdf0 commit aa04df5

2 files changed

Lines changed: 78 additions & 16 deletions

File tree

example/lib/main.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class _MyHomePageState extends State<MyHomePage> {
2626
bool status3 = false;
2727
bool status4 = false;
2828
bool status5 = false;
29+
bool status6 = false;
2930

3031
@override
3132
Widget build(BuildContext context) {
@@ -164,6 +165,43 @@ class _MyHomePageState extends State<MyHomePage> {
164165
),
165166
],
166167
),
168+
SizedBox(height: 20.0),
169+
Text("Custom text"),
170+
SizedBox(height: 10.0),
171+
Row(
172+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
173+
children: <Widget>[
174+
FlutterSwitch(
175+
activeText: Text(
176+
"custom",
177+
style: TextStyle(
178+
color: Colors.white
179+
),
180+
),
181+
offText: Text(
182+
"Custom2",
183+
style: TextStyle(
184+
color: Colors.black87
185+
),
186+
),
187+
value: status6,
188+
width: 110,
189+
borderRadius: 30.0,
190+
showOnOff: true,
191+
onToggle: (val) {
192+
setState(() {
193+
status6 = val;
194+
});
195+
},
196+
),
197+
Container(
198+
alignment: Alignment.centerRight,
199+
child: Text(
200+
"Value: $status6",
201+
),
202+
),
203+
],
204+
)
167205
],
168206
),
169207
),

lib/flutter_switch.dart

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class FlutterSwitch extends StatefulWidget {
1111
inactiveTextColor,
1212
toggleColor;
1313
final double width, height, toggleSize, valueFontSize, borderRadius, padding;
14+
final Widget activeText;
15+
final Widget offText;
1416

1517
const FlutterSwitch({
1618
Key key,
@@ -28,6 +30,8 @@ class FlutterSwitch extends StatefulWidget {
2830
this.borderRadius = 20.0,
2931
this.padding = 4.0,
3032
this.showOnOff = false,
33+
this.activeText,
34+
this.offText
3135
}) : super(key: key);
3236

3337
@override
@@ -87,14 +91,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
8791
? Expanded(
8892
child: Container(
8993
padding: EdgeInsets.symmetric(horizontal: 4.0),
90-
child: Text(
91-
widget.showOnOff ? "On" : "",
92-
style: TextStyle(
93-
color: widget.activeTextColor,
94-
fontWeight: FontWeight.w900,
95-
fontSize: widget.valueFontSize,
96-
),
97-
),
94+
child: _activeText,
9895
),
9996
)
10097
: Container(),
@@ -114,14 +111,7 @@ class _FlutterSwitchState extends State<FlutterSwitch>
114111
child: Container(
115112
padding: EdgeInsets.symmetric(horizontal: 4.0),
116113
alignment: Alignment.centerRight,
117-
child: Text(
118-
widget.showOnOff ? "Off" : "",
119-
style: TextStyle(
120-
color: widget.inactiveTextColor,
121-
fontWeight: FontWeight.w900,
122-
fontSize: widget.valueFontSize,
123-
),
124-
),
114+
child: _offText,
125115
),
126116
)
127117
: Container(),
@@ -132,4 +122,38 @@ class _FlutterSwitchState extends State<FlutterSwitch>
132122
},
133123
);
134124
}
125+
126+
Widget get _activeText {
127+
if (widget.showOnOff) {
128+
if (widget?.activeText != null) return widget.activeText;
129+
130+
return Text(
131+
"On",
132+
style: TextStyle(
133+
color: widget.activeTextColor,
134+
fontWeight: FontWeight.w900,
135+
fontSize: widget.valueFontSize,
136+
),
137+
);
138+
}
139+
140+
return Text("");
141+
}
142+
143+
Widget get _offText {
144+
if (widget.showOnOff) {
145+
if (widget?.offText != null) return widget.offText;
146+
147+
return Text(
148+
"Off",
149+
style: TextStyle(
150+
color: widget.inactiveTextColor,
151+
fontWeight: FontWeight.w900,
152+
fontSize: widget.valueFontSize,
153+
),
154+
);
155+
}
156+
157+
return Text("");
158+
}
135159
}

0 commit comments

Comments
 (0)