Switch class

An interactive visual switch toggle widget representing true/false state.

Interfaces

  • Keyboard: When focused is true, pressing Space (' '), Enter ('\n'), or Carriage Return ('\r') toggles value and triggers onChanged.
  • Mouse: Left-clicking inside bounds toggles value and triggers onChanged.

Example Usage

Switch(
  value: isNotificationsEnabled,
  label: 'Enable Notifications',
  onChanged: (val) {
    setState(() => isNotificationsEnabled = val);
  },
);

Properties and Styling

Property Type Description
value bool The switch toggle state (on/off).
label String The text label accompanying the switch.
onChanged Function(bool) Callback triggered when the state changes.
focused bool Whether this switch has focus.
style Style Normal rendering style.
focusedStyle Style Style applied when focused is true.
Inheritance

Constructors

Switch({required bool value, required String label, required void onChanged(bool newValue), bool focused = false, Style style = Style.empty, Style focusedStyle = const Style(modifiers: Modifier.reverse)})
Creates a new Switch.
const

Properties

focused bool
Whether this switch has keyboard focus.
final
focusedStyle Style
The rendering style applied when the switch is focused.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
The optional key for this widget.
finalinherited
label String
The text label accompanying the switch.
final
onChanged → void Function(bool newValue)
The callback triggered when the state changes.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
style Style
The normal rendering style.
final
value bool
The switch toggle state (on/off).
final

Methods

createElement() Element
Creates an Element to manage this widget's location in the tree.
inherited
getIntrinsicHeight(int width) int
Computes the intrinsic height of this widget under the given width constraint.
inherited
handleKeyEvent(KeyEvent event) → void
Handles key activations (Space or Enter).
handleMouseEvent(MouseEvent event, int localX, int localY) → void
Handles mouse clicks.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
render(Buffer buffer, Rect area) → void
Renders the widget onto the provided buffer within the specified area.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited