Forms

Switch

An on/off toggle that takes effect immediately. A checkbox with role="switch", drawn in CSS.

JxSwitch import 'package:jx_cli/ui.dart' Available since 0.0.2

Preview

When to use it

  • Settings that apply at once (Wi-Fi, dark mode, notifications).

States

Disabled

Minimal example

Dart
JxSwitch(id: 'wifi', label: 'Wi-Fi')

Complete example

A full Jaspr component you can paste into a project.

Dart
import 'package:jaspr/jaspr.dart';
import 'package:jx_cli/ui.dart';

class NotificationsToggle extends StatefulComponent {
  const NotificationsToggle({super.key});

  @override
  State<NotificationsToggle> createState() => _NotificationsToggleState();
}

class _NotificationsToggleState extends State<NotificationsToggle> {
  bool enabled = true;

  @override
  Component build(BuildContext context) {
    return JxSwitch(
      id: 'notifications',
      label: enabled ? 'Notifications on' : 'Notifications off',
      checked: enabled,
      onChange: (value) => setState(() => enabled = value),
    );
  }
}

Properties

Property Type Default Description
id String required Links the label to the input.
label String required Visible label.
name String? null Form field name.
checked bool false On or off.
disabled bool false Disables the switch.
classes String? null Extra JX classes, appended after the component classes.

Events

Callback Type Fires when
onChange ValueChanged<bool>? The switch is turned on or off.

Customizing

  • Pass classes to add or override utilities: they come last, so classes: 'w:full' wins over the default width.
  • The track uses peer-checked:bg:blue-700; the thumb slides with peer-checked:tx:5.

Accessibility

  • A real checkbox with role="switch", visually hidden but focusable: Space toggles it and screen readers announce "on" or "off".
  • The focus ring is drawn on the track with peer-focus-visible:.
  • The thumb does not slide when the system asks for reduced motion.

Limitations

  • No description text: add a hint next to it if the effect is not obvious.