Skip to main content

ToggleSwitchAppi

A customizable toggle switch widget with smooth animations and flexible styling for Flutter applications.

Features

  • Smooth Animations: Fluid toggle transitions with customizable duration
  • Custom Styling: Configurable colors, sizes, and visual properties
  • Interactive States: Hover and focus effects
  • Flexible Layout: Optional label text with customizable positioning
  • Accessibility: Built-in accessibility support
  • Material Design: Follows Material Design guidelines

Usage

bool isEnabled = false;

ToggleSwitchAppi(
value: isEnabled,
onChanged: (bool newValue) {
setState(() {
isEnabled = newValue;
});
},
label: 'Enable notifications',
)

Parameters

ParameterTypeDescription
valueboolRequired. Current state of the toggle switch
onChangedValueChanged<bool>?Required. Callback when switch state changes
labelString?Optional text label displayed next to switch
activeColorColor?Color when switch is enabled (default: primary color)
activeTrackColorColor?Track color when switch is enabled
inactiveThumbColorColor?Thumb color when switch is disabled
inactiveTrackColorColor?Track color when switch is disabled
activeThumbImageImageProvider?Image for the thumb when enabled
inactiveThumbImageImageProvider?Image for the thumb when disabled
thumbColorMaterialStateProperty<Color?>?Thumb color for different states
trackColorMaterialStateProperty<Color?>?Track color for different states
materialTapTargetSizeMaterialTapTargetSize?Size of the tap target
dragStartBehaviorDragStartBehaviorDrag start behavior (default: start)
mouseCursorMouseCursor?Mouse cursor when hovering
focusColorColor?Color when switch is focused
hoverColorColor?Color when switch is hovered
overlayColorMaterialStateProperty<Color?>?Color for different interaction states
splashRadiusdouble?Radius of the splash effect
focusNodeFocusNode?Focus node for keyboard navigation
autofocusboolWhether to autofocus (default: false)

Examples

Basic Toggle Switch

bool isDarkMode = false;

ToggleSwitchAppi(
value: isDarkMode,
onChanged: (bool value) {
setState(() {
isDarkMode = value;
});
},
label: 'Dark Mode',
)

Custom Styled Toggle Switch

bool isNotificationsEnabled = true;

ToggleSwitchAppi(
value: isNotificationsEnabled,
onChanged: (bool value) {
setState(() {
isNotificationsEnabled = value;
});
},
label: 'Push Notifications',
activeColor: Colors.green,
activeTrackColor: Colors.green.withOpacity(0.3),
inactiveThumbColor: Colors.grey,
inactiveTrackColor: Colors.grey.withOpacity(0.3),
)

Toggle Switch with Material State Properties

ToggleSwitchAppi(
value: isFeatureEnabled,
onChanged: (bool value) {
setState(() {
isFeatureEnabled = value;
});
},
label: 'Advanced Features',
thumbColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey.withOpacity(0.5);
}
if (states.contains(MaterialState.selected)) {
return Colors.blue;
}
return Colors.white;
},
),
trackColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey.withOpacity(0.3);
}
if (states.contains(MaterialState.selected)) {
return Colors.blue.withOpacity(0.5);
}
return Colors.grey.withOpacity(0.3);
},
),
)

Disabled Toggle Switch

ToggleSwitchAppi(
value: true,
onChanged: null, // Disabled
label: 'This feature is locked',
)

Toggle Switch in Settings List

Column(
children: [
ToggleSwitchAppi(
value: settings.autoSave,
onChanged: (bool value) {
setState(() {
settings.autoSave = value;
});
},
label: 'Auto-save documents',
),
ToggleSwitchAppi(
value: settings.showTips,
onChanged: (bool value) {
setState(() {
settings.showTips = value;
});
},
label: 'Show helpful tips',
),
ToggleSwitchAppi(
value: settings.analytics,
onChanged: (bool value) {
setState(() {
settings.analytics = value;
});
},
label: 'Share usage analytics',
),
],
)

Best Practices

  • Always provide an onChanged callback unless the switch should be disabled
  • Use clear, descriptive labels that explain what the switch controls
  • Consider the visual hierarchy when placing switches in lists or forms
  • Use consistent styling across your app for a cohesive experience
  • Provide immediate feedback when the switch state changes
  • Consider using SwitchListTile for better integration in lists
  • Ensure sufficient tap target size for accessibility (minimum 44x44 pixels)