From 40c223ef4420570391343cdebf2c991ddd36bbf0 Mon Sep 17 00:00:00 2001 From: Bas Date: Thu, 18 Sep 2025 08:53:47 -0300 Subject: [PATCH 1/2] allow for custom text styles --- lib/flutter_switch.dart | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/flutter_switch.dart b/lib/flutter_switch.dart index e30ca12..6041779 100644 --- a/lib/flutter_switch.dart +++ b/lib/flutter_switch.dart @@ -43,6 +43,8 @@ class FlutterSwitch extends StatefulWidget { this.inactiveIcon, this.duration = const Duration(milliseconds: 200), this.disabled = false, + this.activeTextStyle, + this.inactiveTextStyle, }) : assert( (switchBorder == null || activeSwitchBorder == null) && (switchBorder == null || inactiveSwitchBorder == null), @@ -251,6 +253,16 @@ class FlutterSwitch extends StatefulWidget { /// Defaults to the value of false. final bool disabled; + /// The text style to use on the text value when the switch is on. + /// This parameter is only necessary when [showOnOff] property is true. + /// The use of this overrides the [activeTextColor] and [activeTextFontWeight] properties. + final TextStyle? activeTextStyle; + + /// The text style to use on the text value when the switch is off. + /// /// This parameter is only necessary when [showOnOff] property is true. + /// The use of this overrides the [inactiveTextColor] and [inactiveTextFontWeight] properties. + final TextStyle? inactiveTextStyle; + @override _FlutterSwitchState createState() => _FlutterSwitchState(); } @@ -433,11 +445,12 @@ class _FlutterSwitchState extends State if (widget.showOnOff) { return Text( widget.activeText ?? "On", - style: TextStyle( - color: widget.activeTextColor, - fontWeight: _activeTextFontWeight, - fontSize: widget.valueFontSize, - ), + style: widget.activeTextStyle ?? + TextStyle( + color: widget.activeTextColor, + fontWeight: _activeTextFontWeight, + fontSize: widget.valueFontSize, + ), ); } @@ -448,11 +461,12 @@ class _FlutterSwitchState extends State if (widget.showOnOff) { return Text( widget.inactiveText ?? "Off", - style: TextStyle( - color: widget.inactiveTextColor, - fontWeight: _inactiveTextFontWeight, - fontSize: widget.valueFontSize, - ), + style: widget.inactiveTextStyle ?? + TextStyle( + color: widget.inactiveTextColor, + fontWeight: _inactiveTextFontWeight, + fontSize: widget.valueFontSize, + ), textAlign: TextAlign.right, ); } From 7e2b4948aeda135c1f9ee0abaa05fdd2df99c1c2 Mon Sep 17 00:00:00 2001 From: Bas Date: Thu, 18 Sep 2025 09:03:35 -0300 Subject: [PATCH 2/2] Add text alignment option --- lib/flutter_switch.dart | 48 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/flutter_switch.dart b/lib/flutter_switch.dart index 6041779..ccea9f0 100644 --- a/lib/flutter_switch.dart +++ b/lib/flutter_switch.dart @@ -45,6 +45,8 @@ class FlutterSwitch extends StatefulWidget { this.disabled = false, this.activeTextStyle, this.inactiveTextStyle, + this.activeTextAlignment = Alignment.centerLeft, + this.inactiveTextAlignment = Alignment.centerLeft, }) : assert( (switchBorder == null || activeSwitchBorder == null) && (switchBorder == null || inactiveSwitchBorder == null), @@ -263,6 +265,12 @@ class FlutterSwitch extends StatefulWidget { /// The use of this overrides the [inactiveTextColor] and [inactiveTextFontWeight] properties. final TextStyle? inactiveTextStyle; + /// The alignment of the text when the switch is on. + final Alignment activeTextAlignment; + + /// The alignment of the text when the switch is off. + final Alignment inactiveTextAlignment; + @override _FlutterSwitchState createState() => _FlutterSwitchState(); } @@ -443,14 +451,17 @@ class _FlutterSwitchState extends State Widget get _activeText { if (widget.showOnOff) { - return Text( - widget.activeText ?? "On", - style: widget.activeTextStyle ?? - TextStyle( - color: widget.activeTextColor, - fontWeight: _activeTextFontWeight, - fontSize: widget.valueFontSize, - ), + return Align( + alignment: widget.activeTextAlignment, + child: Text( + widget.activeText ?? "On", + style: widget.activeTextStyle ?? + TextStyle( + color: widget.activeTextColor, + fontWeight: _activeTextFontWeight, + fontSize: widget.valueFontSize, + ), + ), ); } @@ -459,15 +470,18 @@ class _FlutterSwitchState extends State Widget get _inactiveText { if (widget.showOnOff) { - return Text( - widget.inactiveText ?? "Off", - style: widget.inactiveTextStyle ?? - TextStyle( - color: widget.inactiveTextColor, - fontWeight: _inactiveTextFontWeight, - fontSize: widget.valueFontSize, - ), - textAlign: TextAlign.right, + return Align( + alignment: widget.inactiveTextAlignment, + child: Text( + widget.inactiveText ?? "Off", + style: widget.inactiveTextStyle ?? + TextStyle( + color: widget.inactiveTextColor, + fontWeight: _inactiveTextFontWeight, + fontSize: widget.valueFontSize, + ), + textAlign: TextAlign.right, + ), ); }