الانتقال إلى المحتوى
تطبيقات الموبايلFlutter· 7 دقائق للقراءة

A Rebuild Is Not a Repaint: What build() Really Costs in Flutter

build() describes the UI and paint() draws it: they're separate phases, and fixing one doesn't fix the other.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Flutter 3.x
محتويات المقال

A dashboard shows a live clock above an expensive sales chart, and it stutters every second. The team does what every performance thread recommends: they move the clock's setState into its own small widget so the dashboard stops rebuilding. A print in the dashboard's build() confirms it now runs once. The stutter is still there. With repaint highlighting turned on, the chart flashes on every tick. Nothing rebuilt it. It was repainted anyway.

The common misconception

The belief: every call to build() redraws that widget on screen. So rebuilds are the enemy, and the fewer build() calls you see in the logs, the less work Flutter does to draw the screen.

It's easy to see where it comes from. "Rebuild" sounds like "redraw", and on the web "re-render" means updating what's on screen. Flutter's own docs tell you to avoid costly work in build() and to keep setState local, which sounds like "rebuilds are expensive". And a rebuild can lead to a repaint, when it changes something visible. So "avoid rebuilds" is decent advice for the wrong reason, and it hides the other half: painting has its own triggers.

What actually happens

A frame runs in phases, and each one does only dirty work

Each frame goes through three phases that matter here: build, layout and paint. In the build phase, the framework keeps a list of dirty elements and jumps straight to them, skipping the clean ones. Layout visits only the that need it and a limited area around them. Paint draws only the parts of the render tree that were marked as needing paint. setState marks one element dirty for the build phase. Nothing in it says "paint this".

A rebuild that changes nothing paints nothing

When a widget rebuilds, each render object widget below it copies its new configuration onto its existing render object. The render object marks itself for layout or paint only when something it draws actually changed. RenderParagraph, the render object behind Text, compares the old and new text and returns early when they're identical. For a CustomPaint, that decision is your shouldRepaint. And when the walk meets the very same widget instance as last frame, as with a const widget, it stops there.

In a widget test that rebuilt a screen every second for ten seconds with identical output, build() ran 11 times and the chart's paint() ran once.

A repaint doesn't need a rebuild

The RepaintBoundary docs say it plainly: a render object's paint may be triggered even if its widgets didn't change or rebuild. A render object repaints whenever any render object that shares its layer is marked as needing paint: an ancestor scrolls, something animates, a text next to it changes. The mark travels up to the nearest , possibly the root of the app, and painting that boundary repaints all of its descendants in the same layer. Animations often skip the build phase entirely: the Flutter docs note that a change to an Animation<Color> might trigger only the paint phase.

What build() actually costs

Not pixels. A rebuild costs CPU time on the UI thread: running your build() methods, then walking the descendants until the walk meets an identical widget instance again. That's why the docs' advice stands: avoid repetitive and costly work in build(), since it can run whenever an ancestor rebuilds; call setState as low in the tree as you can; push state to the leaves; use const. Notice what none of that controls: how much gets painted.

Here's the dashboard after the team's fix, and with the missing piece:

Dart dashboard.dart
class Dashboard extends StatelessWidget {  const Dashboard({super.key, required this.points});  final List<double> points;  @override  Widget build(BuildContext context) {    return Column(children: [      const Clock(), // ticks every second; Dashboard never rebuilds      CustomPaint(        size: const Size(300, 200),        // shouldRepaint is false, yet paint() runs on every tick        painter: ChartPainter(points),      ),    ]);  }}

A widget test that counts both over ten seconds of ticks shows the gap. In the first version, Dashboard.build() ran once and the chart painted 11 times: once for the first frame and once per tick, because the clock's new text marked the shared layer for paint. In the second, the chart painted once. Going the other way, with the timer's setState moved up into the dashboard so the whole screen rebuilt every second, gave 11 builds and, with the boundary in place, still one paint.

RebuildRepaint
What runsbuild() methods, producing new widgetspaint() of render objects, recording drawing commands
Triggered bysetState, a new widget from the parent, an InheritedWidget changeA visual change, an animation, scrolling, a neighbor in the same layer
How far it goesDown the subtree, until it meets the same widget instanceThe whole layer below the nearest repaint boundary
Skipped whenThe element is clean or the new widget is identicalNothing in the layer changed its appearance
Your leverCheap build(), local setState, constRepaintBoundary around content that paints at a different rate

How to apply it in your project

  1. Measure the phase that's actually slowProfile in profile mode with the DevTools Performance view. Its Track Widget Builds option shows build() events in the timeline, and Track Paints shows paint events. They add overhead themselves, so turn them on only while you investigate.
  2. Keep build() cheap, not rareMove parsing, sorting and heavy formatting out of build() and into state that changes only when the data does. A build() that only assembles widgets can run often without hurting anyone.
  3. Keep rebuilds localGive a fast-changing value its own small widget, as the docs suggest with a dedicated clock widget. Use const where you can, and pass subtrees that don't change as a child instead of rebuilding them inside a builder.
  4. Contain repaints where painting is expensiveWrap content that's costly to paint and rarely changes, like a chart, a map or a complex illustration, when it sits next to something that repaints often. Then check the boundary's metrics, and remove it if it isn't useful.

Key takeaways

  • build() returns a description of the UI; painting is a separate phase that runs only for what changed.
  • A rebuild that produces the same output marks nothing for paint.
  • Repaints happen without rebuilds and spread to everything in the same layer, up to the nearest repaint boundary.
  • build() costs CPU time: keep it cheap and local, with const and state pushed to the leaves.
  • Use RepaintBoundary where painting is expensive and happens at a different rate, and verify that it helps.

📚 المصادر

  1. Performance best practices — Flutter docs
  2. Inside Flutter — Flutter docs
  3. StatefulWidget class — Flutter API
  4. RepaintBoundary class — Flutter API
  5. RenderRepaintBoundary.debugSymmetricPaintCount — Flutter API

التعليقات (0)