Toggle | jaspr_daisyui_components
jaspr_daisyui_components
pub.dev ↗

Toggle

Toggle is a checkbox that is styled to look like a switch button.

Class name Type Description
toggle Component For <input type="checkbox">
toggle-primary Color primary color
toggle-secondary Color secondary color
toggle-accent Color accent color
toggle-neutral Color neutral color
toggle-success Color success color
toggle-warning Color warning color
toggle-info Color info color
toggle-error Color error color
toggle-xs Size Extra small size
toggle-sm Size Small size
toggle-md Size Medium size
toggle-lg Size Large size
toggle-xl Size Extra large size

Toggle (switch)

<input type="checkbox" checked="checked" class="toggle" />
Toggle(const [], checked: true);

With fieldset and label

Login options
<fieldset class="fieldset bg-base-100 border-base-300 rounded-box w-64 border p-4">
  <legend class="fieldset-legend">Login options</legend>
  <label class="label">
    <input type="checkbox" checked="checked" class="toggle" />
    Remember me
  </label>
</fieldset>
Fieldset(
  [
    FieldsetLegend([Component.text('Login options')]),
    label([Toggle(const [], checked: true), Component.text('Remember me')], classes: 'label'),
  ],
  classes: 'bg-base-100 border-base-300 rounded-box w-64 border p-4',
);

Sizes

<input type="checkbox" checked="checked" class="toggle toggle-xs" />
<input type="checkbox" checked="checked" class="toggle toggle-sm" />
<input type="checkbox" checked="checked" class="toggle toggle-md" />
<input type="checkbox" checked="checked" class="toggle toggle-lg" />
<input type="checkbox" checked="checked" class="toggle toggle-xl" />
Component.fragment([
  Toggle(const [], size: ToggleSize.xs, checked: true),
  Toggle(const [], size: ToggleSize.sm, checked: true),
  Toggle(const [], size: ToggleSize.md, checked: true),
  Toggle(const [], size: ToggleSize.lg, checked: true),
  Toggle(const [], size: ToggleSize.xl, checked: true),
]);

Colors

<input type="checkbox" checked="checked" class="toggle toggle-primary" />
<input type="checkbox" checked="checked" class="toggle toggle-secondary" />
<input type="checkbox" checked="checked" class="toggle toggle-accent" />
<input type="checkbox" checked="checked" class="toggle toggle-neutral" />
<input type="checkbox" checked="checked" class="toggle toggle-info" />
<input type="checkbox" checked="checked" class="toggle toggle-success" />
<input type="checkbox" checked="checked" class="toggle toggle-warning" />
<input type="checkbox" checked="checked" class="toggle toggle-error" />
Component.fragment([
  Toggle(const [], color: ToggleColor.primary, checked: true),
  Toggle(const [], color: ToggleColor.secondary, checked: true),
  Toggle(const [], color: ToggleColor.accent, checked: true),
  Toggle(const [], color: ToggleColor.neutral, checked: true),
  Toggle(const [], color: ToggleColor.info, checked: true),
  Toggle(const [], color: ToggleColor.success, checked: true),
  Toggle(const [], color: ToggleColor.warning, checked: true),
  Toggle(const [], color: ToggleColor.error, checked: true),
]);

Disabled

<input type="checkbox" class="toggle" disabled="" />
<input type="checkbox" class="toggle" disabled="" checked="checked" />
Component.fragment([
  Toggle(const [], disabled: true),
  Toggle(const [], checked: true, disabled: true),
]);

Indeterminate

<!-- You can make a checkbox indeterminate using JS -->
<script>document.getElementById("my-toggle").indeterminate = true</script>
<input type="checkbox" class="toggle" id="my-toggle" />
Component.fragment([
  RawText(r'''<script>
  document.getElementById("my-toggle").indeterminate = true
</script>'''),
  Toggle(const [], id: 'my-toggle'),
]);

Toggle with icons inside

Use toggle class for a label, put a checkbox and 2 icons inside it.
<label class="toggle text-base-content">
  <input type="checkbox" />
  <svg aria-label="enabled" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
    <g stroke-linejoin="round" stroke-linecap="round" stroke-width="4" fill="none" stroke="currentColor">
      <path d="M20 6 9 17l-5-5"></path>
    </g>
  </svg>
  <svg aria-label="disabled" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round">
    <path d="M18 6 6 18"></path>
    <path d="m6 6 12 12"></path>
  </svg>
</label>
label(
  [
    input(type: InputType.checkbox),
    RawText(r'''<svg aria-label="enabled" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
    <g stroke-linejoin="round" stroke-linecap="round" stroke-width="4" fill="none" stroke="currentColor">
      <path d="M20 6 9 17l-5-5"></path>
    </g>
  </svg>'''),
    RawText(r'''<svg aria-label="disabled" xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round" stroke-linejoin="round">
    <path d="M18 6 6 18"></path>
    <path d="m6 6 12 12"></path>
  </svg>'''),
  ],
  classes: 'toggle text-base-content',
);

Toggle with custom colors

<input type="checkbox" checked="checked" class="toggle border-indigo-600 bg-indigo-500 checked:border-orange-500 checked:bg-orange-400 checked:text-orange-800" />
Toggle(
  const [],
  classes: 'border-indigo-600 bg-indigo-500 checked:border-orange-500 checked:bg-orange-400 checked:text-orange-800',
  checked: true,
);