Switch constructor

const Switch({
  1. Key? key,
  2. required bool value,
  3. required ValueChanged<bool>? onChanged,
  4. Color? activeColor,
  5. Color? activeTrackColor,
  6. Color? inactiveThumbColor,
  7. Color? inactiveTrackColor,
  8. ImageProvider<Object>? activeThumbImage,
  9. ImageErrorListener? onActiveThumbImageError,
  10. ImageProvider<Object>? inactiveThumbImage,
  11. ImageErrorListener? onInactiveThumbImageError,
  12. MaterialStateProperty<Color?>? thumbColor,
  13. MaterialStateProperty<Color?>? trackColor,
  14. MaterialStateProperty<Color?>? trackOutlineColor,
  15. MaterialStateProperty<double?>? trackOutlineWidth,
  16. MaterialStateProperty<Icon?>? thumbIcon,
  17. MaterialTapTargetSize? materialTapTargetSize,
  18. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  19. MouseCursor? mouseCursor,
  20. Color? focusColor,
  21. Color? hoverColor,
  22. MaterialStateProperty<Color?>? overlayColor,
  23. double? splashRadius,
  24. FocusNode? focusNode,
  25. ValueChanged<bool>? onFocusChange,
  26. bool autofocus = false,
  27. 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,
  this.activeColor,
  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);