---
name: flutter-widget-refactorer
description: "Refactors a long, deeply nested Flutter build method into small, const-friendly widgets, with the same behaviour and a short list of what changed. Use when a Flutter developer has a long nested build method and wants it split into small reusable widgets without changing behaviour. Do not use for creating a new screen or writing tests for a widget. Related topics: flutter, dart, coding."
---

# Flutter Widget Refactorer

Refactors a long, deeply nested Flutter build method into small, const-friendly widgets, with the same behaviour and a short list of what changed.

## When to use

Use this skill when a Flutter developer has a long nested build method and wants it split into small reusable widgets without changing behaviour. Do not use it for creating a new screen or writing tests for a widget.

## 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 code and mark the sections that can stand alone as widgets.
3. Extract each section into its own widget class and pass only the data it needs.
4. Move logic out of build using the existing state management and add const constructors where possible.
5. Rename unclear identifiers, add doc comments if requested, then list every change and risk.

## Inputs

- **Flutter code** (longer free text)
- **State management used** (choose one: None or StatefulWidget, Provider, Riverpod, Bloc, GetX)
- **Main issue** (choose one: Too long and nested, Performance issues, Hard to read, Reusability)
- **Specific targets** (choose any: Extract sub-widgets, Use const constructors, Improve naming, Separate logic from UI)
- **Add documentation comments** (choose one: Yes, No)

## Output

The refactored Dart code with each extracted widget as its own class, followed by a bullet list of changes and any behaviour risks.

## Prompt template

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

### Flutter Widget Refactor

Refactor the complex Flutter UI code below into clean, reusable widgets without changing what it does.

- Flutter code: {{Flutter code}}
- State management used: {{State management used}}
- Main issue: {{Main issue}}
- Specific targets: {{Specific targets}}
- Add documentation comments: {{Add documentation comments}}

Extract sub-widgets as separate StatelessWidget classes rather than helper methods that return widgets, since classes rebuild independently and can be const. Pass only the data each widget needs. Keep state and business logic where the chosen state management puts it and move it out of the build method; do not introduce a new package. Add const constructors wherever every field is final and constant. Rename vague variables and widgets to describe their role.

Output the refactored code with each new widget in its own class, then a bullet list of what moved, what was renamed and any behaviour risk you noticed.

## Pitfalls

- Replacing nested widgets with helper methods, which keeps rebuilding the whole tree.
- Changing state management or adding a package during a refactor.
- Passing the whole model to every child, which defeats the benefit of const and narrow rebuilds.
