---
name: flutter-widget-test-generator
description: "Writes a complete Flutter widget test file for the scenarios you pick, with finders, pump calls and optional golden tests, ready to run. Use when a Flutter developer needs a widget test file for a given widget covering renders, taps, error and loading states. Do not use for unit tests for pure Dart logic or end-to-end browser tests. Related topics: flutter, testing, coding."
---

# Flutter Widget Test Generator

Writes a complete Flutter widget test file for the scenarios you pick, with finders, pump calls and optional golden tests, ready to run.

## When to use

Use this skill when a Flutter developer needs a widget test file for a given widget covering renders, taps, error and loading states. Do not use it for unit tests for pure Dart logic or end-to-end browser tests.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Read the widget and list its inputs, callbacks and visible states.
3. Write the pump setup with MaterialApp and Scaffold and choose finders that do not depend on position.
4. Write one test per selected scenario, using pump instead of pumpAndSettle for infinite animations.
5. Add the golden test template if requested and list testability problems found in the widget.

## Inputs

- **Widget code or interface** (longer free text)
- **What the widget does** (free text, e.g. "a login button that shows a loading spinner")
- **Scenarios to test** (choose any: Renders correctly, Tap events trigger callbacks, Displays error states, Loading states)
- **Testing package** (choose one: flutter_test, patrol, golden_toolkit)
- **Include a golden test template** (choose one: Yes, No)

## Output

A complete Dart test file with one named test per scenario and a setUp block, followed by notes on hard-to-test code.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Flutter Widget Test Writer

Write reliable widget tests for the Flutter widget below.

- Widget code or interface: {{Widget code or interface}}
- What the widget does: {{What the widget does}}
- Scenarios to test: {{Scenarios to test}}
- Testing package: {{Testing package}}
- Include a golden test template: {{Include a golden test template}}

Wrap the widget in MaterialApp and Scaffold so theme and media query exist. Find elements by key, type or text, not by position. Use pumpWidget, then pump or pumpAndSettle after taps, and use a plain pump for loading states because pumpAndSettle will wait forever on an animated spinner. Verify callbacks with a counter or flag set in the test. Create one test per scenario with a descriptive name, and use setUp for shared arrangement. When a golden test is requested, add a matchesGoldenFile call and note the flag that generates the image.

Output the complete Dart test file, then list any code in the widget that was hard to test and a suggested change.

## Pitfalls

- Calling pumpAndSettle on a looping spinner, which makes the test time out.
- Finding widgets by index, which breaks when layout changes.
- Skipping the MaterialApp wrapper, so theme and navigation lookups fail.
