Better Flutter UI From Claude Code: Tokens, Not Defaults

· 5 min read

Ask an AI agent for “a settings screen” and you get a settings screen. It works. It also looks like every other Flutter demo: default blue, default spacing, a ListTile stack with no rhythm. The fix is not a better prompt. It is giving Claude Code a design system to build with, and rules that stop it from going around that system.

This post shows how to set up tokens in Flutter’s theme, how to write rules Claude follows, and how to let it check its own work.

Why agent UI drifts to defaults

An agent writes the most likely code for the request. With no theme in the project, the most likely code is Colors.blue, EdgeInsets.all(16) and TextStyle(fontSize: 18) scattered through each widget. Each screen ends up a little different, and nothing feels designed.

Tokens fix both problems. When every color, size and text style has a name in one place, the agent has something specific to reach for, and you can change the look in one file.

Step 1: put the basics in ThemeData

Flutter’s themes cookbook shows the starting point: a ColorScheme built from a seed color, plus a TextTheme.

final lightTheme = ThemeData(
  colorScheme: ColorScheme.fromSeed(
    seedColor: const Color(0xFF2E6B5E),
    brightness: Brightness.light,
  ),
  textTheme: const TextTheme(
    displayLarge: TextStyle(fontSize: 48, fontWeight: FontWeight.w700),
    titleLarge: TextStyle(fontSize: 22, fontWeight: FontWeight.w600),
    bodyMedium: TextStyle(fontSize: 15, height: 1.4),
  ),
);

Widgets then read from the theme:

final colors = Theme.of(context).colorScheme;
final text = Theme.of(context).textTheme;

Text('Welcome back', style: text.titleLarge?.copyWith(color: colors.primary));

This matters more than it looks. The ThemeData API docs say “Material components typically depend exclusively on the colorScheme and textTheme.” Get those two right and buttons, fields, cards and dialogs follow without extra work.

Step 2: add your own tokens with ThemeExtension

Spacing, radii, brand colors outside the Material roles, and semantic colors like “success” do not fit in ColorScheme. That is what ThemeExtension is for. The API docs describe it as “An interface that defines custom additions to a ThemeData object” and say: “To use, subclass ThemeExtension, define a number of fields (e.g. Colors), and implement the copyWith and lerp methods.”

@immutable
class AppTokens extends ThemeExtension<AppTokens> {
  const AppTokens({
    required this.success,
    required this.spaceS,
    required this.spaceM,
    required this.radius,
  });

  final Color success;
  final double spaceS;
  final double spaceM;
  final double radius;

  @override
  AppTokens copyWith({Color? success, double? spaceS, double? spaceM, double? radius}) {
    return AppTokens(
      success: success ?? this.success,
      spaceS: spaceS ?? this.spaceS,
      spaceM: spaceM ?? this.spaceM,
      radius: radius ?? this.radius,
    );
  }

  @override
  AppTokens lerp(AppTokens? other, double t) {
    if (other is! AppTokens) return this;
    return AppTokens(
      success: Color.lerp(success, other.success, t)!,
      spaceS: lerpDouble(spaceS, other.spaceS, t)!,
      spaceM: lerpDouble(spaceM, other.spaceM, t)!,
      radius: lerpDouble(radius, other.radius, t)!,
    );
  }
}

Register it with ThemeData(extensions: [AppTokens(...)]) and read it with Theme.of(context).extension<AppTokens>()!. The API docs describe extension<T>() as “Used to obtain a particular ThemeExtension from extensions.” To see a complete example, Flutter ships one you can generate:

flutter create --sample=material.ThemeExtension.1 mysample

Adding a small BuildContext extension, like context.tokens, makes the call short enough that the agent will actually use it.

Step 3: write the rules down

Tokens only help if Claude uses them. Put the rules in CLAUDE.md, where Claude Code reads them at the start of every session:

## UI rules
- Colors come from Theme.of(context).colorScheme or context.tokens. Never Color(0x...) or Colors.* in widgets.
- Text styles come from Theme.of(context).textTheme. Never a raw TextStyle(fontSize: ...).
- Spacing uses tokens.spaceS / spaceM / spaceL. No magic numbers in EdgeInsets.
- Every screen works in light and dark mode.
- Layout decisions use window width, not device type.
- Tappable targets are at least 48x48.

Rules work best when they are short and checkable. Our CLAUDE.md for Flutter post has a fuller template. For a rule like “no raw colors,” add a hook that greps for Color(0x in lib/features/ after each edit, so violations surface immediately.

Step 4: build for real screens

Flutter’s adaptive design guide gives rules worth putting straight into your prompt or rules file:

  • Use MediaQuery.sizeOf(context) rather than MediaQuery.of(context) when you only need size. The guide says it is more efficient.
  • Use LayoutBuilder when a widget should respond to its own constraints.
  • Choose breakpoints by window size, not by device type. The guide uses 600 logical pixels as an example breakpoint.
  • Do not lock the app to portrait.

Flutter’s accessibility guide adds hard numbers:

  • Text contrast of “at least 4.5:1.”
  • “All tappable targets should be at least 48x48 pixels.”
  • “The UI should remain legible and usable at very large scale factors” of text.

Ask Claude to check each new screen against these, and to test with large text. Then try it yourself with TalkBack and VoiceOver, which the guide recommends.

Step 5: let Claude see the result

An agent that cannot see the screen is guessing. Two ways to close the loop:

Screenshots. Claude Code accepts images by path, drag and drop, or paste. Run the app, save a screenshot, and ask Claude to compare it to a mockup or list what looks off. Our iOS simulator post shows the commands.

Golden tests. Flutter’s golden file testing compares a widget’s rendering to a saved image:

await expectLater(find.byType(SaveButton), matchesGoldenFile('save.png'));

Update the baseline with flutter test --update-goldens when a change is intentional. Golden tests stop the agent from quietly changing a screen while working on something else. See testing a Flutter app with Claude Code for the wider test setup.

A prompt that works

Once tokens and rules are in place, prompts can be short:

Build the order history screen from docs/spec.md. Use only theme and tokens. Support light and dark, large text, and widths above 600 with a two-column layout. Add a golden test. Then screenshot it on the simulator and tell me anything that breaks the UI rules.

Compare that to “make it look nice.” The first gives the agent constraints it can verify. The second leaves it to guess.

Design first, then build

Tokens make UI consistent. They do not decide what the UI should be. Pick your palette, type scale and spacing before the first screen, ideally from a reference you like. The Claude Code mobile app design skill post covers that step, and the Flutter with Claude Code guide shows where it sits in the full build. MobileKit ships a token-first Flutter standard and a design quality checklist if you want these rules ready-made.

Sources read on the day this post was written: Flutter docs: Use themes to share colors and font styles, Flutter API: ThemeExtension class, Flutter API: ThemeData class, Flutter docs: Adaptive and responsive design, Flutter docs: Accessibility, Flutter API: matchesGoldenFile and Claude Code docs: Common workflows.

Questions

Why does Flutter UI from Claude Code look generic?

Without a theme and rules, the agent falls back to default Material widgets and hard-coded values. Give it a ColorScheme, a TextTheme, a ThemeExtension for your own tokens, and a rule that widgets read only from the theme.

What is a ThemeExtension in Flutter?

The Flutter API docs call it an interface that defines custom additions to a ThemeData object. You subclass it, define fields such as colors, and implement copyWith and lerp, then read it with Theme.of(context).extension<T>().

Can Claude Code check its own Flutter UI?

Yes, if you give it a way to see the result. Claude Code accepts screenshots by file path, and Flutter golden tests with matchesGoldenFile catch visual changes on every run.

Try it on your next idea

One payment, every update to this major version, and a refund window if it is not for you. Refund policy.