NIB / DOCUMENTATION
Switch — NibComponents
Change a setting and show when confirmation is pending.
Open interactive viewUse a switch for an on/off setting
NibSwitch requires a boolean value. The onChanged callback receives its proposed inverse. Keep that value in the host; a switch has no separate controller. disabled: true, loading: true, or null onChanged blocks toggling.
Represent pending confirmation honestly
loading shows a spinner and blocks repeat activation. It does not send a request or change value. For an optimistic setting, update value then restore it on failure. For a confirmed setting, keep the previous value while awaiting the result and only commit on success. Decide which behavior your application needs and explain errors near the setting.
Customize the thumb without replacing the switch
onThumbIcon and offThumbIcon accept any Widget, including Image.asset or Image.memory; they decorate the built-in thumb. The Images variant uses the existing Nib logo PNG embedded in the copied source. It loads without a network or asset declaration. The package does not include an SVG decoder. loading takes precedence over those icons. thumbBuilder replaces shape, color, icon, and spinner entirely. Its NibSwitchThumbState supplies value, enabled, loading, and resolved size; a custom builder must draw its own busy state. The switch still positions and animates it.
Use images inside the thumb or replace it entirely
Images keeps the built-in circular thumb and displays the Nib logo in both states. Image-thumb replaces the entire thumb with a contrasting outline, the same logo and a loading ring. Custom uses a rounded square; Icons adds a state badge. All variants keep the brand visible when off. Both variants use NibSwitch animated movement; changing the picture does not create a separate animation engine.
For application assets, declare the PNG paths in pubspec.yaml and pass Image.asset to onThumbIcon and offThumbIcon. An SVG-rendering widget can occupy the same slots if your application provides one; NibSwitch accepts the widget without parsing SVG itself. Keep these decorative images excluded from semantics and label the setting once.
NibSwitch(
value: enabled, animated: true, label: 'Workspace updates',
transition: const NibTransition(
duration: Duration(milliseconds: 320), curve: Curves.easeInOutCubic,
),
onThumbIcon: Image.asset('assets/moving.png', excludeFromSemantics: true),
offThumbIcon: Image.asset('assets/still.png', excludeFromSemantics: true),
onChanged: (next) => setState(() => enabled = next),
)Keep visible size and interaction size separate
The default track is 44 by 24 logical pixels. trackWidth and trackHeight change that geometry; thumbShape replaces the default circular shape. A label makes the whole row actionable and description provides a second line. Null color overrides retain NibTheme tokens.
Label the setting and preserve focus
semanticLabel defaults to label. Supply a name for a track without visible text. External focusNode ownership stays with the caller; autofocus defaults to false. The package defaults animated to false; this website opts in. The demo uses a 320 ms easeInOutCubic transition so the glide is visible. Import NibTransition from package:nib_motion/nib_motion.dart for that override. System reduced motion still takes priority; the value changes without decorative travel. Retargeting starts from the current position rather than jumping to the previous endpoint.
An icon disappeared or the setting stays busy
Check whether loading is still true or thumbBuilder overrides the built-in icons. A promise completing does not clear loading automatically. Avoid replacing the switch while waiting; preserving its identity also preserves focus.