Skip to content

Commit 3d10ab1

Browse files
authored
Merge branch 'master' into master
2 parents 5ed3fa6 + 9a75263 commit 3d10ab1

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

lib/toggle_switch.dart

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ToggleSwitch extends StatefulWidget {
6666
final double iconSize;
6767

6868
/// Divider margin
69-
final double dividerMargin;
69+
final double? dividerMargin;
7070

7171
/// Border width
7272
final double? borderWidth;
@@ -107,6 +107,12 @@ class ToggleSwitch extends StatefulWidget {
107107
/// Set a border only to the active toggle component
108108
List<Border?>? activeBorders;
109109

110+
/// Set the label to the center
111+
final bool centerText;
112+
113+
/// Set the long on Two Lines
114+
final bool twoLineText;
115+
110116
ToggleSwitch(
111117
{Key? key,
112118
this.totalSwitches,
@@ -141,7 +147,9 @@ class ToggleSwitch extends StatefulWidget {
141147
this.dividerMargin = 8.0,
142148
this.doubleTapDisable = false,
143149
this.isVertical = false,
144-
this.activeBorders})
150+
this.activeBorders,
151+
this.centerText = false,
152+
this.twoLineText = false})
145153
: super(key: key);
146154

147155
@override
@@ -247,7 +255,9 @@ class _ToggleSwitchState extends State<ToggleSwitch>
247255
/// Sets same active background color for all items if active background colors list is empty.
248256
/// Sets different active background color for current item by matching index if active background colors list is not empty
249257
if (active) {
250-
bgColor = widget.activeBgColors?[index ~/ 2] ?? activeBgColor;
258+
bgColor = widget.activeBgColors == null
259+
? activeBgColor
260+
: (widget.activeBgColors![index ~/ 2] ?? activeBgColor);
251261
}
252262

253263
if (index % 2 == 1) {
@@ -264,9 +274,10 @@ class _ToggleSwitchState extends State<ToggleSwitch>
264274
margin: widget.isVertical
265275
? EdgeInsets.symmetric(
266276
horizontal:
267-
activeDivider ? 0.0 : widget.dividerMargin)
277+
activeDivider ? 0.0 : widget.dividerMargin!)
268278
: EdgeInsets.symmetric(
269-
vertical: activeDivider ? 0.0 : widget.dividerMargin),
279+
vertical:
280+
activeDivider ? 0.0 : widget.dividerMargin!),
270281
);
271282
} else {
272283
/// Matches corner radius of active switch to that of border
@@ -340,19 +351,41 @@ class _ToggleSwitchState extends State<ToggleSwitch>
340351
/// Assigns custom text styles if available.
341352
/// Assigns default text style if custom text style is not available.
342353
/// Overrides fontSize, activeFgColor, inactiveFgColor.
354+
/// Allow Custom Font Style but still respect activeFgColor and inactiveFgColor
343355
/// If only one TextStyle is passed then we assume that we wanna
344356
/// apply that TextStyle to all the switches.
357+
///
358+
TextStyle oneIndexStyle() {
359+
if (widget.customTextStyles![0]!.color == null) {
360+
return widget.customTextStyles![0]!.copyWith(
361+
color: fgColor,
362+
);
363+
}
364+
return widget.customTextStyles![0]!;
365+
}
366+
345367
TextStyle defaultTextStyle = TextStyle(
346368
color: fgColor,
347369
fontSize: widget.fontSize,
348370
);
371+
372+
TextStyle moreThanOneIndexStyle() {
373+
if (widget.customTextStyles![index ~/ 2]!.color == null) {
374+
return widget.customTextStyles![index ~/ 2]!.copyWith(
375+
color: fgColor,
376+
);
377+
}
378+
379+
return widget.customTextStyles![index ~/ 2]!;
380+
}
381+
349382
var textStyle = defaultTextStyle;
350383
if (widget.customTextStyles != null) {
351384
textStyle = widget.customTextStyles!.length == 1
352-
? widget.customTextStyles![0]!
385+
? oneIndexStyle()
353386
: (widget.customTextStyles!.length > index ~/ 2 &&
354387
widget.customTextStyles![index ~/ 2] != null
355-
? widget.customTextStyles![index ~/ 2]!
388+
? moreThanOneIndexStyle()
356389
: defaultTextStyle);
357390
}
358391

@@ -411,8 +444,12 @@ class _ToggleSwitchState extends State<ToggleSwitch>
411444
left: (icon is Container) ? 0.0 : 5.0),
412445
child: Text(
413446
widget.labels?[index ~/ 2] ?? '',
447+
textAlign:
448+
(widget.centerText) ? TextAlign.center : null,
414449
style: textStyle,
415-
overflow: TextOverflow.ellipsis,
450+
overflow: (!widget.twoLineText)
451+
? TextOverflow.ellipsis
452+
: null,
416453
),
417454
),
418455
),

0 commit comments

Comments
 (0)