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
Minimal example
JxSwitch(id: 'wifi', label: 'Wi-Fi')
Complete example
A full Jaspr component you can paste into a project.
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
classesto add or override utilities: they come last, soclasses: 'w:full'wins over the default width. -
The track uses
peer-checked:bg:blue-700; the thumb slides withpeer-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.