Switch
A toggle switch component for binary on/off states with smooth animations
A toggle switch component for binary on/off states with smooth animations.
When to use this
- Feature toggles: Enable or disable features or settings
- Binary choices: Represent yes/no or on/off decisions
- Settings panels: Control boolean preferences in settings
- Quick actions: Provide instant state changes without confirmation
Basic implementation
Basic implementation
import 'package:flutter/material.dart';
import 'package:remix/remix.dart';
class SwitchExample extends StatefulWidget {
const SwitchExample({super.key});
@override
State<SwitchExample> createState() => _SwitchExampleState();
}
class _SwitchExampleState extends State<SwitchExample> {
final ValueNotifier<bool> _selected = ValueNotifier(false);
@override
void dispose() {
_selected.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return RemixSwitch(
semanticLabel: 'Toggle',
style: style,
selected: _selected.value,
onChanged: (value) {
setState(() {
_selected.value = value;
});
},
);
}
SwitchStyler get style {
return SwitchStyler()
.thumbColor(Colors.grey.shade600)
.trackColor(Colors.deepPurpleAccent.shade200)
.size(65, 30)
.borderRadius(.all(const Radius.circular(40)))
.alignment(
_selected.value ? Alignment.centerRight : Alignment.centerLeft,
)
.animate(AnimationConfig.easeOut(300.ms))
.thumb(
BoxStyler()
.color(Colors.white)
.size(40, 30)
.borderRadius(.circular(40))
.scale(0.85)
.decoration(
.boxShadow([
BoxShadowMix(
color: Colors.black.withValues(alpha: 0.1),
offset: const Offset(2, 4),
blurRadius: 4,
spreadRadius: 3,
),
]),
),
);
}
}Fortal widgets
The application-owned Fortal preset provides a themed widget for this component:
Fortal variants
import 'package:flutter/material.dart';
import 'ui/ui.dart';
class FortalSwitchExample extends StatefulWidget {
const FortalSwitchExample({super.key});
@override
State<FortalSwitchExample> createState() => _FortalSwitchExampleState();
}
class _FortalSwitchExampleState extends State<FortalSwitchExample> {
bool _value = false;
@override
Widget build(BuildContext context) {
return Column(
spacing: 16,
children: [
FortalSwitch.surface(
selected: _value,
semanticLabel: 'Notifications',
onChanged: (v) => setState(() => _value = v),
),
FortalSwitch.soft(
selected: _value,
semanticLabel: 'Notifications',
onChanged: (v) => setState(() => _value = v),
),
],
);
}
}See the fortalSwitchStyle source code for all available options.
Constructor
Constructor
import 'package:flutter/material.dart';
import 'package:remix/remix.dart';
RemixSwitch remixSwitchConstructor({
Key? key,
required bool selected,
required String semanticLabel,
ValueChanged<bool>? onChanged,
bool enabled = true,
bool enableFeedback = true,
FocusNode? focusNode,
bool autofocus = false,
bool excludeSemantics = false,
MouseCursor mouseCursor = SystemMouseCursors.click,
SwitchStyler style = const SwitchStyler.create(),
SwitchSpec? styleSpec,
}) => throw UnimplementedError();Properties
Widget Properties
key → Key?
Optional. Controls how one widget replaces another widget in the tree.
enabled → bool
Optional. Whether this switch is enabled.
selected → bool
Required. Whether the switch is currently selected.
onChanged → ValueChanged<bool>?
Optional. Called when the user toggles the switch. When omitted, the switch is disabled.
style → SwitchStyler
Optional. The style configuration for the switch.
styleSpec → SwitchSpec?
Optional. The style spec for the switch.
enableFeedback → bool
Optional. Whether to enable haptic feedback when toggled.
focusNode → FocusNode?
Optional. The focus node for the switch.
autofocus → bool
Optional. Whether the switch should automatically request focus when it is created.
semanticLabel → String
Required. The accessible name announced for this switch. Must be nonblank — an adjacent Text label is not the accessible name.
excludeSemantics → bool
Optional. Whether to hide the switch and its visual subtree from the semantics tree. Defaults to false.
mouseCursor → MouseCursor
Optional. Cursor when hovering over the switch.
Style Methods
thumbColor(Color value)
Sets thumb color
thumb(BoxStyler value)
Sets thumb styling
container(BoxStyler value)
Configures the switch track container style.
alignment(Alignment value)
Sets container alignment
trackColor(Color value)
Sets the track background color.
color(Color value)
Sets the track background color.
onSelected(SwitchStyler value)
Applies a style while the switch is selected.
wrap(WidgetModifierConfig value)
Applies widget modifiers such as clipping, opacity, or scaling.
animate(AnimationConfig value)
Configures implicit animation for style transitions.
variants(List<VariantStyle<SwitchSpec>> value)
Sets custom style variants.
constraints(BoxConstraintsMix value)
Sets size constraints on the component.
decoration(DecorationMix value)
Sets the container decoration.
margin(EdgeInsetsGeometryMix value)
Sets the container margin.
padding(EdgeInsetsGeometryMix value)
Sets the container padding.
foregroundDecoration(DecorationMix value)
Sets a foreground decoration painted on top of the component.
transform(Matrix4 value, AlignmentGeometry alignment = Alignment.center)
Applies a matrix transformation to the component.
call({..., required String semanticLabel, bool excludeSemantics = false, ...})
Creates a RemixSwitch widget with this style applied.