---
title: Icons
description: >-
  Draw icon glyphs from Material, Cupertino or any other icon font in a
  generated widget.
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/abausg/home_widget/llms.txt
> Use this file to discover all available pages before exploring further.

# Icons

A generated widget can draw icon glyphs from Material, Cupertino or any other icon font package —
without you touching Xcode or an Android `res/` folder. `home_widget_generator` and
`home_widget_cli` do the rest at generation time.

```dart
HWIcon.fixed(Icons.favorite, size: 24)
```

<Info>
This page covers icons on a **generated** widget. Rendering an icon glyph from a hand-written
`RemoteViews`/Glance or SwiftUI widget uses the same underlying mechanism, documented on its own at
[Icons](/features/icons).
</Info>

## Fixed icons

`HWIcon.fixed` takes any `IconData` constant known at generation time — Material, Cupertino, or one
from any other icon font package:

```dart
HWIcon.fixed(Icons.favorite, size: 24, color: HWDefaultColor(HWColorRole.contentPrimary))
HWIcon.fixed(CupertinoIcons.heart_fill, size: 20)
```

## Runtime icons

Wrap an `HWIconData` to let the icon change based on saved data — a mood, a weather condition, a
status marker:

```dart
HWIcon(
  HWIconData(
    'mood',
    icons: [Icons.wb_sunny, Icons.cloud, Icons.umbrella],
    defaultValue: Icons.wb_sunny,
    previewValue: Icons.cloud,
  ),
)
```

`icons` is required and non-empty, and every icon in the list must come from the same icon font
family — mixing `Icons.*` and `CupertinoIcons.*` in one list is a generation error. `defaultValue`
and `previewValue`, when given, must be `IconData` constants and members of `icons`.

No two icons in one list may be the same glyph. Flutter ships aliases that share a codepoint —
`Icons.airplanemode_active` and `Icons.airplanemode_on` are both `0xe06e` — and only the codepoint
is stored, so a widget could never tell the two apart. Listing both is a generation error naming
them.

For each `HWIconData` field the generated Dart helper exposes an enum named
`<WidgetClassName><FieldKeyPascal>Icon` — a field `mood` on a widget class `FontAndIcons` becomes
`FontAndIconsMoodIcon` — with one value per listed icon and an `icon` getter returning that
`IconData` back:

```dart
enum FontAndIconsMoodIcon {
  wbSunny(IconData(0xe6d9, fontFamily: 'MaterialIcons', fontPackage: null)),
  cloud(IconData(0xe16f, fontFamily: 'MaterialIcons', fontPackage: null)),
  umbrella(IconData(0xe68a, fontFamily: 'MaterialIcons', fontPackage: null));

  const FontAndIconsMoodIcon(this.icon);

  final IconData icon;

  int get codePoint => icon.codePoint;

  /// The value storing [codePoint], or null when this widget shows no icon
  /// for it.
  static FontAndIconsMoodIcon? fromCodePoint(int? codePoint) {
    if (codePoint == null) return null;
    for (final value in values) {
      if (value.codePoint == codePoint) return value;
    }
    return null;
  }
}
```

`saveData` accepts this enum rather than a raw `IconData`, so only an icon actually declared in
`icons` can ever be saved:

```dart
await FontAndIconsHomeWidget.saveData(mood: FontAndIconsMoodIcon.cloud);
await FontAndIconsHomeWidget.updateWidget();
```

`fromCodePoint` is the way back: storage holds the bare code point, and `getData()` runs it through
`fromCodePoint` for you, yielding `null` when the stored value matches no icon this widget lists.

A value's name comes from the constant you listed, lower-camel-cased — `Icons.wb_sunny` becomes
`wbSunny`. Two cases fall back: an inline `IconData(0xe6d9, ...)` written in the schema rather than
a named constant becomes `icon0xE6D9`, and a name that would collide with a Dart keyword or with a
member the enum already carries (`index`, `name`, `values`, `icon`, `codePoint`, …) gets a trailing
underscore.

`HWIconData` works nested inside [`HWJson`](/generator/data#nested-json) and
[`HWTimedData`](/generator/timed-data), the same as any other data type.

The enum is named after the field key alone, so two fields that share a key share the enum — an
`icon` under `HWJson('today', ...)` and one under `HWJson('tomorrow', ...)` both read and write
`<Widget>IconIcon`. The generated enum is then the **union** of both lists, and either field accepts
any of its values. Where the two lists disagree — the same name for different glyphs, the same glyph
under different names, or two different icon fonts — generation fails and names both fields; give one
of them a key of its own.

## Directional icons

Directional icons mirror in a right-to-left layout exactly as they do in Flutter. An icon whose
`IconData.matchTextDirection` is set — `Icons.arrow_back`, `Icons.arrow_forward`,
`Icons.format_list_bulleted` — is drawn flipped horizontally when the widget sits in a right-to-left
layout, and left alone otherwise. The flag is read off the `IconData` itself, so there is nothing to
declare: it applies to an `HWIcon.fixed` and to every icon of an `HWIconData` alike, and only the
directional ones among a list are mirrored.

What counts as a right-to-left layout follows each platform's own rules. Android reads the app's
locale, so a per-app or device language such as Arabic flips the widget. On iOS a widget extension
takes its layout direction from the languages **its own bundle** is localized for, exactly like an
app: an extension localized only for English stays left-to-right under an Arabic system. Ship
right-to-left translations through [`HWText.localized`](/generator/localization) and the mirror
follows.

<Info>
Need to switch between icons from **different** families at runtime — say a filled vs. an outline
icon set — rather than between icons in one family? Pick between two `HWIcon.fixed` branches with
[`HWConditional`](/generator/widgets/hw-conditional) instead; `HWIconData` itself only ever picks
among icons that already share a family.
</Info>

See [`HWIcon`](/generator/widgets/hw-icon) for the full constructor reference, and
[Data Types](/generator/data#icon-data) for `HWIconData`'s storage and defaults.

## What the CLI does

Flutter tree-shakes icon fonts (`MaterialIcons`, `CupertinoIcons`, or a package's own icon font)
down to the glyphs your app itself uses, so the copy shipped with the app cannot be relied on — the
specific glyph a widget needs may have been shaken out of it. On every `home_widget generate` run,
`home_widget_cli` builds a **subset font containing exactly the glyphs the schema uses** and copies
it into the native projects:

- `MaterialIcons` glyphs come from the Flutter SDK's own copy of `MaterialIcons-Regular.otf`.
- `CupertinoIcons` glyphs come from the `cupertino_icons` package.
- Any other icon font's glyphs come from that package's own `flutter: fonts:` declaration.

Subsetting needs Flutter's own font subsetting tool out of the SDK cache. When it is not there — a
cache that was never populated, or a trimmed CI checkout — the CLI copies the font whole instead and
warns once per run that icon fonts are not being shrunk, pointing at `flutter precache`. The widget
still renders; the font file is just megabytes rather than kilobytes.

The subset lands at:

- Android — `android/app/src/main/res/font/hw_font_<widget>__icons_<family>[__<package>].<ext>`
- iOS — `ios/<Widget>HomeWidget/hw_font_icons_<family>[__<package>].<ext>`, wired into the widget
  extension target's Resources build phase automatically.

The name is built from the icon family and, for a family that comes from a dependency, the package
owning it; `<ext>` follows the source font, so a `MaterialIcons` subset is `.otf` and a
`CupertinoIcons` one is `.ttf`.

This is fully automatic — no prompt, no CLI flag to turn it on. Every file the CLI writes for this
is prefixed `hw_font_`, and a file that is no longer referenced by any widget is deleted the next
time you generate, the same pruning policy the generator already applies to localized string
resources.

<Info>
This copy step is for **icon** fonts only. A custom **text** font (the one you set with
`fontFamily`) is never copied anywhere — both platforms read the font your app already bundles, as
described in [How it renders](/generator/custom-fonts#how-it-renders) on the Custom Fonts page.
</Info>

## Version requirement

Icons require `home_widget` **0.10.0 or newer**. `home_widget_cli` checks the resolved version in
`pubspec.lock` and fails with a clear error if a schema uses `HWIcon`/`HWIconData` against an older
one.

## Limitations

- An icon font must be resolvable at generation time. `MaterialIcons` comes out of the Flutter SDK's
  own cache, so a checkout that has never been precached fails with a `flutter precache` hint;
  `CupertinoIcons` comes out of the `cupertino_icons` package, so that dependency has to be in your
  `pubspec.yaml` and resolved. Any other family must be declared under `flutter: fonts:` in the
  owning package's `pubspec.yaml`.
