Skip to content

Commit e7a4e7c

Browse files
author
Hans Muller
authored
Update Switch doc: disabled state (flutter#24941)
1 parent ab36f05 commit e7a4e7c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

packages/flutter/lib/src/material/switch.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ enum _SwitchType { material, adaptive }
3535
/// that use a switch will listen for the [onChanged] callback and rebuild the
3636
/// switch with a new [value] to update the visual appearance of the switch.
3737
///
38+
/// If the [onChanged] callback is null, then the switch will be disabled (it
39+
/// will not respond to input). A disabled switch's thumb and track are rendered
40+
/// in shades of grey by default. The default appearance of a disabled switch
41+
/// can be overridden with [inactiveThumbColor] and [inactiveTrackColor].
42+
///
3843
/// Requires one of its ancestors to be a [Material] widget.
3944
///
4045
/// See also:
@@ -505,8 +510,7 @@ class _RenderSwitch extends RenderToggleable {
505510
@override
506511
void paint(PaintingContext context, Offset offset) {
507512
final Canvas canvas = context.canvas;
508-
509-
final bool isActive = onChanged != null;
513+
final bool isEnabled = onChanged != null;
510514
final double currentValue = position.value;
511515

512516
double visualPosition;
@@ -519,7 +523,17 @@ class _RenderSwitch extends RenderToggleable {
519523
break;
520524
}
521525

522-
final Color trackColor = isActive ? Color.lerp(inactiveTrackColor, activeTrackColor, currentValue) : inactiveTrackColor;
526+
final Color trackColor = isEnabled
527+
? Color.lerp(inactiveTrackColor, activeTrackColor, currentValue)
528+
: inactiveTrackColor;
529+
530+
final Color thumbColor = isEnabled
531+
? Color.lerp(inactiveColor, activeColor, currentValue)
532+
: inactiveColor;
533+
534+
final ImageProvider thumbImage = isEnabled
535+
? (currentValue < 0.5 ? inactiveThumbImage : activeThumbImage)
536+
: inactiveThumbImage;
523537

524538
// Paint the track
525539
final Paint paint = Paint()
@@ -544,8 +558,6 @@ class _RenderSwitch extends RenderToggleable {
544558
try {
545559
_isPainting = true;
546560
BoxPainter thumbPainter;
547-
final Color thumbColor = isActive ? Color.lerp(inactiveColor, activeColor, currentValue) : inactiveColor;
548-
final ImageProvider thumbImage = isActive ? (currentValue < 0.5 ? inactiveThumbImage : activeThumbImage) : inactiveThumbImage;
549561
if (_cachedThumbPainter == null || thumbColor != _cachedThumbColor || thumbImage != _cachedThumbImage) {
550562
_cachedThumbColor = thumbColor;
551563
_cachedThumbImage = thumbImage;

0 commit comments

Comments
 (0)