Switch constructor

const Switch({
  1. Key? key,
  2. required bool value,
  3. required ValueChanged<bool>? onChanged,
  4. @Deprecated('Use activeThumbColor instead. ' 'This feature was deprecated after v3.31.0-2.0.pre.') Color? activeColor,
  5. Color? activeThumbColor,
  6. Color? activeTrackColor,
  7. Color? inactiveThumbColor,
  8. Color? inactiveTrackColor,
  9. ImageProvider<Object>? activeThumbImage,
  10. ImageErrorListener? onActiveThumbImageError,
  11. ImageProvider<Object>? inactiveThumbImage,
  12. ImageErrorListener? onInactiveThumbImageError,
  13. MaterialStateProperty<Color?>? thumbColor,
  14. MaterialStateProperty<Color?>? trackColor,
  15. MaterialStateProperty<Color?>? trackOutlineColor,
  16. MaterialStateProperty<double?>? trackOutlineWidth,
  17. MaterialStateProperty<Icon?>? thumbIcon,
  18. MaterialTapTargetSize? materialTapTargetSize,
  19. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  20. MouseCursor? mouseCursor,
  21. Color? focusColor,
  22. Color? hoverColor,
  23. MaterialStateProperty<Color?>? overlayColor,
  24. double? splashRadius,
  25. FocusNode? focusNode,
  26. ValueChanged<bool>? onFocusChange,
  27. bool autofocus = false,
  28. EdgeInsetsGeometry? padding,
})

Creates a Material Design switch.

The switch itself does not maintain any state. Instead, when the state of the switch changes, the widget calls the onChanged callback. Most widgets that use a switch will listen for the onChanged callback and rebuild the switch with a new value to update the visual appearance of the switch.

The following arguments are required:

  • value determines whether this switch is on or off.
  • onChanged is called when the user toggles the switch on or off.

Implementation

const Switch({
  super.key,
  required this.value,
  required this.onChanged,
  @Deprecated(
    'Use activeThumbColor instead. '
    'This feature was deprecated after v3.31.0-2.0.pre.',
  )
  this.activeColor,
  this.activeThumbColor,
  this.activeTrackColor,
  this.inactiveThumbColor,
  this.inactiveTrackColor,
  this.activeThumbImage,
  this.onActiveThumbImageError,
  this.inactiveThumbImage,
  this.onInactiveThumbImageError,
  this.thumbColor,
  this.trackColor,
  this.trackOutlineColor,
  this.trackOutlineWidth,
  this.thumbIcon,
  this.materialTapTargetSize,
  this.dragStartBehavior = DragStartBehavior.start,
  this.mouseCursor,
  this.focusColor,
  this.hoverColor,
  this.overlayColor,
  this.splashRadius,
  this.focusNode,
  this.onFocusChange,
  this.autofocus = false,
  this.padding,
}) : _switchType = _SwitchType.material,
     applyCupertinoTheme = false,
     assert(activeThumbImage != null || onActiveThumbImageError == null),
     assert(inactiveThumbImage != null || onInactiveThumbImageError == null);