Skip to content
tezvyn:
Menu

RepaintBoundary: Isolate and Optimize Flutter Painting

Source: api.flutter.devMediumHow cards are made

RepaintBoundary acts like painter's tape for your UI, isolating a widget's paint job from its parent to prevent unnecessary repaints. Use it on complex, static widgets inside an animating view. The footgun is overuse: each boundary adds memory overhead.

Why it exists

By default, when a Flutter widget needs to repaint, it can trigger a repaint of other widgets sharing its paint layer, even if they haven't changed. This is inefficient, leading to wasted CPU/GPU cycles and potential jank, especially in complex UIs with animations or scrolling.

The mental model

Think of a RepaintBoundary as putting painter's tape around a section of your UI. It tells Flutter's rendering engine: "This area has its own paint job. Don't repaint it just because something outside this tape changes, and don't let its changes force a repaint of the entire screen." It creates a new, independent canvas for its child.

How it works

A RepaintBoundary widget creates a RenderObject that always has its own paint layer, decoupling it from its ancestors. When a widget inside the boundary needs to repaint, the "dirty" flag travels up the tree but stops at the boundary. The boundary itself is then scheduled to be repainted, but it doesn't trigger a repaint of its ancestors. The engine may even cache a complex, static boundary as a bitmap for faster rendering, though this is an optimization hint, not a guarantee.

When to use it

Use RepaintBoundary when you have a widget subtree that repaints at a different frequency than its surroundings. The classic case is a complex, mostly static widget (like a chart, a custom drawing, or a QR code) inside a container that animates or scrolls. This prevents the static widget from being repainted on every frame of the parent's animation.

When not to use it

Avoid wrapping simple widgets or widgets whose repaint schedule is already in sync with their parents. Creating a new paint layer has a memory and setup cost. Overusing RepaintBoundary can lead to worse performance than not using it at all. Profile your app first using tools like debugRepaintRainbowEnabled to visualize repaints and identify actual problem areas.

One canonical example

Flutter's Drawer widget implicitly uses a RepaintBoundary. The content inside the drawer is usually static. When you open or close the drawer, it animates its position. Without a boundary, the static content would be repainted on every frame of the slide animation. The boundary ensures the content is painted only once, and the engine just moves the resulting layer around.

Interview question

Which scenario best illustrates an appropriate and effective use of Flutter's RepaintBoundary widget?

  • a.Wrapping a simple Text widget that updates its content every second.
  • b.Applying it to every widget in a UI to guarantee the highest possible rendering performance.
  • c.Using it around a widget whose parent frequently rebuilds but whose child's paint operations are already synchronized.
  • d.Encapsulating a complex, static chart widget that is part of a larger scrolling list.Correct
Why?

RepaintBoundary is most effective for complex, static widgets within an animating or scrolling parent, as it isolates their painting and prevents unnecessary repaints. Overusing it on simple or synchronized widgets, or applying it universally, introduces memory overhead that can degrade performance rather than improve it.

Just read this? Test yourself on what you have been reading.

Read the original → api.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles