> ## Documentation Index
> Fetch the complete documentation index at: https://docs.thehuxdesign.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Toggle

> Two-state toggle button for formatting controls and feature toggles

## Overview

`HuxToggle` is a two-state button component commonly used for formatting controls (like bold, italic) or feature toggles. It supports both icon-only and icon-with-text configurations, with multiple variants and sizes.

## Basic Usage

```dart theme={null}
HuxToggle(
  value: isBold,
  onChanged: (value) => setState(() => isBold = value),
  icon: FeatherIcons.bold,
  label: 'Bold', // Optional visual label
  semanticLabel: 'Bold', // Optional when label is provided
)
```

![Basic toggle usage](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=1f2aa6cd62e13572889a3b255985b2c1)

## Icon-Only Toggle

```dart theme={null}
HuxToggle(
  value: isEditing,
  onChanged: (value) => setState(() => isEditing = value),
  icon: FeatherIcons.edit2,
  semanticLabel: 'Edit', // Required when label is null
)
```

## Variants

HuxToggle supports four visual variants through the `HuxButtonVariant` enum:

### Primary Toggle

![Primary toggle variants](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=1f2aa6cd62e13572889a3b255985b2c1)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.primary, // Default
)
```

### Secondary Toggle

![Secondary toggle variants](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-secondary.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=da5c4c910e7aa0a70c2a787081d5e976)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.secondary,
)
```

### Outline Toggle

![Outline toggle variants](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-outline.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=d8bb983586d58dd290895c632635fc8b)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.outline,
)
```

### Ghost Toggle

![Ghost toggle variants](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-ghost.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=5a6e40f4f9fe4f792450e86c249ae709)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  variant: HuxButtonVariant.ghost,
)
```

## Sizes

Control toggle dimensions with three size options:

### Small Toggle

Compact size for tight spaces (32px height).

![Small toggle](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-small.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=f2385c2cdf1894bc49b694b9bd0c0a58)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.small,
)
```

### Medium Toggle (Default)

Default size for most use cases (40px height).

![Medium toggle](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-primary.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=1f2aa6cd62e13572889a3b255985b2c1)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.medium, // Default
)
```

### Large Toggle

Prominent size for important actions (48px height).

![Large toggle](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-large.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=a37f76c11036600d8cfae087a8856b38)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  size: HuxToggleSize.large,
)
```

## States

### Disabled State

![Disabled toggle states](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-disabled.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=e4463cec82e87ed735be84bcd9ae5939)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: null, // Disable the toggle
  icon: FeatherIcons.edit2,
  isDisabled: true,
)
```

### Custom Primary Color

![Custom color toggle](https://mintcdn.com/thehuxdesign/klklbwMu82vQdxDk/images/toggle/toggle-custom.png?fit=max\&auto=format\&n=klklbwMu82vQdxDk\&q=85\&s=d31a3d0bd26676f62f649376c1010a6d)

```dart theme={null}
HuxToggle(
  value: isActive,
  onChanged: (value) => setState(() => isActive = value),
  icon: FeatherIcons.edit2,
  primaryColor: Colors.deepPurple,
)
```

## API Reference

### HuxToggle Properties

| Property | Type | Default | Description |
| - | - | - | - |
| `value` | `bool` | **required** | The current toggle state (on/off) |
| `onChanged` | `ValueChanged<bool>?` | `null` | Callback triggered when toggle state changes |
| `icon` | `IconData` | **required** | The icon to display in the toggle |
| `label` | `String?` | `null` | Optional text label to display next to the icon |
| `semanticLabel` | `String?` | `null` | Accessibility label used when `label` is not provided |
| `size` | `HuxToggleSize` | `medium` | Size variant of the toggle |
| `variant` | `HuxButtonVariant` | `primary` | Visual variant of the toggle |
| `isDisabled` | `bool` | `false` | Whether the toggle is disabled |
| `primaryColor` | `Color?` | `null` | Optional custom primary color |

> If `label` is omitted (icon-only toggle), provide `semanticLabel` for accessibility.

### HuxToggleSize Enum

* `HuxToggleSize.small` - 32px height, compact padding
* `HuxToggleSize.medium` - 40px height, standard padding
* `HuxToggleSize.large` - 48px height, generous padding

## Accessibility

HuxToggle includes several accessibility features:

### Automatic Contrast

Text and icon colors are automatically calculated to ensure WCAG AA compliance (4.5:1 contrast ratio) against any background color.

### Semantic Roles

Toggles include proper semantic roles for screen readers and announce `label` or `semanticLabel`.

### Focus States

Keyboard navigation support with visible focus indicators.

### Disabled States

Proper disabled state handling for assistive technologies.

## Best Practices

> [accordion] Choose the Right Variant
>
> * Use **Primary** for important toggles that need emphasis
> * Use **Secondary** for standard toggles in content areas
> * Use **Outline** for toggles that need visual separation
> * Use **Ghost** for subtle toggles in toolbars

> [accordion] Size Appropriately
>
> * Use **Large** for important feature toggles
> * Use **Medium** for most standard interactions
> * Use **Small** for compact toolbars or formatting controls

> [accordion] Icon Selection
>
> * Choose clear, meaningful icons that represent the action
> * Use standard icons (bold, italic) for formatting controls
> * Add labels for actions that aren't immediately clear
> * For icon-only toggles, always provide `semanticLabel`

> [accordion] Theme Integration
>
> * Let toggles adapt to your app's theme
> * Use `primaryColor` only for specific emphasis
> * Maintain consistent styling within toggle groups

## Examples

> [card] [Form Building](https://docs.thehuxdesign.com/examples/form-building.md)
>
> See toggles in action within forms

> [card] [Text Editor](https://docs.thehuxdesign.com/examples/text-editor)
>
> Learn how to build a formatting toolbar

> [card] [Custom Theming](https://docs.thehuxdesign.com/examples/custom-theming)
>
> Apply custom colors and themes

> [card] [Accessibility](https://docs.thehuxdesign.com/advanced/accessibility.md)
>
> Ensure your toggles are accessible


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.