@@ -105,6 +105,7 @@ class Switch extends StatelessWidget {
105
105
this .onInactiveThumbImageError,
106
106
this .thumbColor,
107
107
this .trackColor,
108
+ this .trackOutlineColor,
108
109
this .thumbIcon,
109
110
this .materialTapTargetSize,
110
111
this .dragStartBehavior = DragStartBehavior .start,
@@ -151,6 +152,7 @@ class Switch extends StatelessWidget {
151
152
this .materialTapTargetSize,
152
153
this .thumbColor,
153
154
this .trackColor,
155
+ this .trackOutlineColor,
154
156
this .thumbIcon,
155
157
this .dragStartBehavior = DragStartBehavior .start,
156
158
this .mouseCursor,
@@ -333,6 +335,40 @@ class Switch extends StatelessWidget {
333
335
/// | Disabled | `Colors.black12` | `Colors.white10` |
334
336
final MaterialStateProperty <Color ?>? trackColor;
335
337
338
+ /// {@template flutter.material.switch.trackOutlineColor}
339
+ /// The outline color of this [Switch] 's track.
340
+ ///
341
+ /// Resolved in the following states:
342
+ /// * [MaterialState.selected] .
343
+ /// * [MaterialState.hovered] .
344
+ /// * [MaterialState.focused] .
345
+ /// * [MaterialState.disabled] .
346
+ ///
347
+ /// {@tool snippet}
348
+ /// This example resolves the [trackOutlineColor] based on the current
349
+ /// [MaterialState] of the [Switch] , providing a different [Color] when it is
350
+ /// [MaterialState.disabled] .
351
+ ///
352
+ /// ```dart
353
+ /// Switch(
354
+ /// value: true,
355
+ /// onChanged: (_) => true,
356
+ /// trackOutlineColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
357
+ /// if (states.contains(MaterialState.disabled)) {
358
+ /// return Colors.orange.withOpacity(.48);
359
+ /// }
360
+ /// return null; // Use the default color.
361
+ /// }),
362
+ /// )
363
+ /// ```
364
+ /// {@end-tool}
365
+ /// {@endtemplate}
366
+ ///
367
+ /// In Material 3, the outline color defaults to transparent in the selected
368
+ /// state and [ColorScheme.outline] in the unselected state. In Material 2,
369
+ /// the [Switch] track has no outline by default.
370
+ final MaterialStateProperty <Color ?>? trackOutlineColor;
371
+
336
372
/// {@template flutter.material.switch.thumbIcon}
337
373
/// The icon to use on the thumb of this switch
338
374
///
@@ -519,6 +555,7 @@ class Switch extends StatelessWidget {
519
555
onInactiveThumbImageError: onInactiveThumbImageError,
520
556
thumbColor: thumbColor,
521
557
trackColor: trackColor,
558
+ trackOutlineColor: trackOutlineColor,
522
559
thumbIcon: thumbIcon,
523
560
materialTapTargetSize: materialTapTargetSize,
524
561
dragStartBehavior: dragStartBehavior,
@@ -578,6 +615,7 @@ class _MaterialSwitch extends StatefulWidget {
578
615
this .onInactiveThumbImageError,
579
616
this .thumbColor,
580
617
this .trackColor,
618
+ this .trackOutlineColor,
581
619
this .thumbIcon,
582
620
this .materialTapTargetSize,
583
621
this .dragStartBehavior = DragStartBehavior .start,
@@ -604,6 +642,7 @@ class _MaterialSwitch extends StatefulWidget {
604
642
final ImageErrorListener ? onInactiveThumbImageError;
605
643
final MaterialStateProperty <Color ?>? thumbColor;
606
644
final MaterialStateProperty <Color ?>? trackColor;
645
+ final MaterialStateProperty <Color ?>? trackOutlineColor;
607
646
final MaterialStateProperty <Icon ?>? thumbIcon;
608
647
final MaterialTapTargetSize ? materialTapTargetSize;
609
648
final DragStartBehavior dragStartBehavior;
@@ -765,11 +804,17 @@ class _MaterialSwitchState extends State<_MaterialSwitch> with TickerProviderSta
765
804
?? switchTheme.trackColor? .resolve (activeStates)
766
805
?? _widgetThumbColor.resolve (activeStates)? .withAlpha (0x80 )
767
806
?? defaults.trackColor! .resolve (activeStates)! ;
807
+ final Color effectiveActiveTrackOutlineColor = widget.trackOutlineColor? .resolve (activeStates)
808
+ ?? switchTheme.trackOutlineColor? .resolve (activeStates)
809
+ ?? Colors .transparent;
810
+
768
811
final Color effectiveInactiveTrackColor = widget.trackColor? .resolve (inactiveStates)
769
812
?? _widgetTrackColor.resolve (inactiveStates)
770
813
?? switchTheme.trackColor? .resolve (inactiveStates)
771
814
?? defaults.trackColor! .resolve (inactiveStates)! ;
772
- final Color ? effectiveInactiveTrackOutlineColor = switchConfig.trackOutlineColor? .resolve (inactiveStates);
815
+ final Color ? effectiveInactiveTrackOutlineColor = widget.trackOutlineColor? .resolve (inactiveStates)
816
+ ?? switchTheme.trackOutlineColor? .resolve (inactiveStates)
817
+ ?? defaults.trackOutlineColor? .resolve (inactiveStates);
773
818
774
819
final Icon ? effectiveActiveIcon = widget.thumbIcon? .resolve (activeStates)
775
820
?? switchTheme.thumbIcon? .resolve (activeStates);
@@ -858,6 +903,7 @@ class _MaterialSwitchState extends State<_MaterialSwitch> with TickerProviderSta
858
903
..inactiveThumbImage = widget.inactiveThumbImage
859
904
..onInactiveThumbImageError = widget.onInactiveThumbImageError
860
905
..activeTrackColor = effectiveActiveTrackColor
906
+ ..activeTrackOutlineColor = effectiveActiveTrackOutlineColor
861
907
..inactiveTrackColor = effectiveInactiveTrackColor
862
908
..inactiveTrackOutlineColor = effectiveInactiveTrackOutlineColor
863
909
..configuration = createLocalImageConfiguration (context)
@@ -1089,6 +1135,16 @@ class _SwitchPainter extends ToggleablePainter {
1089
1135
notifyListeners ();
1090
1136
}
1091
1137
1138
+ Color ? get activeTrackOutlineColor => _activeTrackOutlineColor;
1139
+ Color ? _activeTrackOutlineColor;
1140
+ set activeTrackOutlineColor (Color ? value) {
1141
+ if (value == _activeTrackOutlineColor) {
1142
+ return ;
1143
+ }
1144
+ _activeTrackOutlineColor = value;
1145
+ notifyListeners ();
1146
+ }
1147
+
1092
1148
Color ? get inactiveTrackOutlineColor => _inactiveTrackOutlineColor;
1093
1149
Color ? _inactiveTrackOutlineColor;
1094
1150
set inactiveTrackOutlineColor (Color ? value) {
@@ -1297,7 +1353,7 @@ class _SwitchPainter extends ToggleablePainter {
1297
1353
final double colorValue = CurvedAnimation (parent: positionController, curve: Curves .easeOut, reverseCurve: Curves .easeIn).value;
1298
1354
final Color trackColor = Color .lerp (inactiveTrackColor, activeTrackColor, colorValue)! ;
1299
1355
final Color ? trackOutlineColor = inactiveTrackOutlineColor == null ? null
1300
- : Color .lerp (inactiveTrackOutlineColor, Colors .transparent , colorValue);
1356
+ : Color .lerp (inactiveTrackOutlineColor, activeTrackOutlineColor , colorValue);
1301
1357
Color lerpedThumbColor;
1302
1358
if (! reaction.isDismissed) {
1303
1359
lerpedThumbColor = Color .lerp (inactivePressedColor, activePressedColor, colorValue)! ;
@@ -1493,7 +1549,6 @@ mixin _SwitchConfig {
1493
1549
double get pressedThumbRadius;
1494
1550
double get thumbRadiusWithIcon;
1495
1551
List <BoxShadow >? get thumbShadow;
1496
- MaterialStateProperty <Color ?>? get trackOutlineColor;
1497
1552
MaterialStateProperty <Color > get iconColor;
1498
1553
double ? get thumbOffset;
1499
1554
Size get transitionalThumbSize;
@@ -1534,9 +1589,6 @@ class _SwitchConfigM2 with _SwitchConfig {
1534
1589
@override
1535
1590
double get trackHeight => 14.0 ;
1536
1591
1537
- @override
1538
- MaterialStateProperty <Color ?>? get trackOutlineColor => null ;
1539
-
1540
1592
@override
1541
1593
double get trackWidth => 33.0 ;
1542
1594
@@ -1590,6 +1642,9 @@ class _SwitchDefaultsM2 extends SwitchThemeData {
1590
1642
});
1591
1643
}
1592
1644
1645
+ @override
1646
+ MaterialStateProperty <Color ?>? get trackOutlineColor => null ;
1647
+
1593
1648
@override
1594
1649
MaterialTapTargetSize get materialTapTargetSize => _theme.materialTapTargetSize;
1595
1650
@@ -1700,6 +1755,19 @@ class _SwitchDefaultsM3 extends SwitchThemeData {
1700
1755
});
1701
1756
}
1702
1757
1758
+ @override
1759
+ MaterialStateProperty <Color ?> get trackOutlineColor {
1760
+ return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
1761
+ if (states.contains (MaterialState .selected)) {
1762
+ return Colors .transparent;
1763
+ }
1764
+ if (states.contains (MaterialState .disabled)) {
1765
+ return _colors.onSurface.withOpacity (0.12 );
1766
+ }
1767
+ return _colors.outline;
1768
+ });
1769
+ }
1770
+
1703
1771
@override
1704
1772
MaterialStateProperty <Color ?> get overlayColor {
1705
1773
return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
@@ -1802,19 +1870,6 @@ class _SwitchConfigM3 with _SwitchConfig {
1802
1870
@override
1803
1871
double get trackHeight => 32.0 ;
1804
1872
1805
- @override
1806
- MaterialStateProperty <Color ?> get trackOutlineColor {
1807
- return MaterialStateProperty .resolveWith ((Set <MaterialState > states) {
1808
- if (states.contains (MaterialState .selected)) {
1809
- return null ;
1810
- }
1811
- if (states.contains (MaterialState .disabled)) {
1812
- return _colors.onSurface.withOpacity (0.12 );
1813
- }
1814
- return _colors.outline;
1815
- });
1816
- }
1817
-
1818
1873
@override
1819
1874
double get trackWidth => 52.0 ;
1820
1875
0 commit comments