
A Rebuild Is Not a Repaint: What build() Really Costs in Flutter
- كاتب موثّق
- Flutter 3.x
محتويات المقال
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
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.
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
setState marks one element dirty for the build phase. Nothing in it says "paint this".
A rebuild that changes nothing paints nothing
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.
build() ran 11 times and the chart's paint() ran once.
A repaint doesn't need a rebuild
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
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.
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), ), ]); }}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(), RepaintBoundary( // Its own layer: the clock's repaints stop outside it child: CustomPaint( size: const Size(300, 200), painter: ChartPainter(points), ), ), ]); }}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.
build() methods, producing new widgets | paint() of render objects, recording drawing commands | |
setState, a new widget from the parent, an InheritedWidget change | ||
build(), local setState, const | RepaintBoundary around content that paints at a different rate |
How to apply it in your project
Measure the phase that's actually slow Profile 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.Keep build() cheap, not rare Move parsing, sorting and heavy formatting out of build()and into state that changes only when the data does. Abuild()that only assembles widgets can run often without hurting anyone.Keep rebuilds local Give a fast-changing value its own small widget, as the docs suggest with a dedicated clock widget. Use constwhere you can, and pass subtrees that don't change as achildinstead of rebuilding them inside a builder.Contain repaints where painting is expensive Wrap 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, withconstand state pushed to the leaves.Use RepaintBoundarywhere painting is expensive and happens at a different rate, and verify that it helps.
التعليقات (0)