@@ -66,7 +66,7 @@ class ToggleSwitch extends StatefulWidget {
66
66
final double iconSize;
67
67
68
68
/// Divider margin
69
- final double dividerMargin;
69
+ final double ? dividerMargin;
70
70
71
71
/// Border width
72
72
final double ? borderWidth;
@@ -107,6 +107,12 @@ class ToggleSwitch extends StatefulWidget {
107
107
/// Set a border only to the active toggle component
108
108
List <Border ?>? activeBorders;
109
109
110
+ /// Set the label to the center
111
+ final bool centerText;
112
+
113
+ /// Set the long on Two Lines
114
+ final bool twoLineText;
115
+
110
116
ToggleSwitch (
111
117
{Key ? key,
112
118
this .totalSwitches,
@@ -141,7 +147,9 @@ class ToggleSwitch extends StatefulWidget {
141
147
this .dividerMargin = 8.0 ,
142
148
this .doubleTapDisable = false ,
143
149
this .isVertical = false ,
144
- this .activeBorders})
150
+ this .activeBorders,
151
+ this .centerText = false ,
152
+ this .twoLineText = false })
145
153
: super (key: key);
146
154
147
155
@override
@@ -247,7 +255,9 @@ class _ToggleSwitchState extends State<ToggleSwitch>
247
255
/// Sets same active background color for all items if active background colors list is empty.
248
256
/// Sets different active background color for current item by matching index if active background colors list is not empty
249
257
if (active) {
250
- bgColor = widget.activeBgColors? [index ~ / 2 ] ?? activeBgColor;
258
+ bgColor = widget.activeBgColors == null
259
+ ? activeBgColor
260
+ : (widget.activeBgColors! [index ~ / 2 ] ?? activeBgColor);
251
261
}
252
262
253
263
if (index % 2 == 1 ) {
@@ -264,9 +274,10 @@ class _ToggleSwitchState extends State<ToggleSwitch>
264
274
margin: widget.isVertical
265
275
? EdgeInsets .symmetric (
266
276
horizontal:
267
- activeDivider ? 0.0 : widget.dividerMargin)
277
+ activeDivider ? 0.0 : widget.dividerMargin! )
268
278
: EdgeInsets .symmetric (
269
- vertical: activeDivider ? 0.0 : widget.dividerMargin),
279
+ vertical:
280
+ activeDivider ? 0.0 : widget.dividerMargin! ),
270
281
);
271
282
} else {
272
283
/// Matches corner radius of active switch to that of border
@@ -340,19 +351,41 @@ class _ToggleSwitchState extends State<ToggleSwitch>
340
351
/// Assigns custom text styles if available.
341
352
/// Assigns default text style if custom text style is not available.
342
353
/// Overrides fontSize, activeFgColor, inactiveFgColor.
354
+ /// Allow Custom Font Style but still respect activeFgColor and inactiveFgColor
343
355
/// If only one TextStyle is passed then we assume that we wanna
344
356
/// 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
+
345
367
TextStyle defaultTextStyle = TextStyle (
346
368
color: fgColor,
347
369
fontSize: widget.fontSize,
348
370
);
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
+
349
382
var textStyle = defaultTextStyle;
350
383
if (widget.customTextStyles != null ) {
351
384
textStyle = widget.customTextStyles! .length == 1
352
- ? widget.customTextStyles ! [ 0 ] !
385
+ ? oneIndexStyle ()
353
386
: (widget.customTextStyles! .length > index ~ / 2 &&
354
387
widget.customTextStyles! [index ~ / 2 ] != null
355
- ? widget.customTextStyles ! [index ~ / 2 ] !
388
+ ? moreThanOneIndexStyle ()
356
389
: defaultTextStyle);
357
390
}
358
391
@@ -411,8 +444,12 @@ class _ToggleSwitchState extends State<ToggleSwitch>
411
444
left: (icon is Container ) ? 0.0 : 5.0 ),
412
445
child: Text (
413
446
widget.labels? [index ~ / 2 ] ?? '' ,
447
+ textAlign:
448
+ (widget.centerText) ? TextAlign .center : null ,
414
449
style: textStyle,
415
- overflow: TextOverflow .ellipsis,
450
+ overflow: (! widget.twoLineText)
451
+ ? TextOverflow .ellipsis
452
+ : null ,
416
453
),
417
454
),
418
455
),
0 commit comments