- Minimize expensive operations
- Control build() cost
- Use saveLayer() thoughtfully
- Minimize use of opacity and clipping
- Implement grids and lists thoughtfully
- Minimize layout passes caused by intrinsic operations
- Build and display frames in 16ms
Generally, Flutter applications are performant by default, so you only need to avoid common pitfalls to get excellent performance. These best practice recommendations will help you write the most performant Flutter app possible.
How do you design a Flutter app to most efficiently render your scenes? In particular, how do you ensure that the painting code generated by the framework is as efficient as possible? Some rendering and layout operations are known to be slow, but can’t always be avoided. They should be used thoughtfully, following the guidance below.
Minimize expensive operations
Some operations are more expensive than others, meaning that they consume more resources. Obviously, you want to only use these operations when necessary. How you design and implement your app’s UI can have a big impact on how efficiently it runs.
Control build() cost
Here are some things to keep in mind when designing your UI:
- Avoid repetitive and costly work in
build()can be invoked frequently when ancestor widgets rebuild.
- Avoid overly large single widgets with a large
build()function. Split them into different widgets based on encapsulation but also on how they change:
setState()is called on a
Stateobject, all descendent widgets rebuild. Therefore, localize the
setState()call to the part of the subtree whose UI actually needs to change. Avoid calling
setState()high up in the tree if the change is contained to a small part of the tree.
- The traversal to rebuild all descendents stops when the
same instance of the child widget as the previous
frame is re-encountered. This technique is heavily
used inside the framework for optimizing
animations where the animation doesn’t affect the child subtree.
TransitionBuilderpattern and the source code for
SlideTransition, which uses this principle to avoid rebuilding its descendents when animating.
constconstructors on widgets as much as possible, since they allow Flutter to short-circuit most of the rebuild work. To be automatically reminded to use
constwhen possible, enable the recommended lints from the
flutter_lintspackage. For more information, check out the
- To create reusable pieces of UIs,
prefer using a
StatelessWidgetrather than a function.
For more information, check out:
part of the
Widgets vs helper methods,
a video from the official Flutter YouTube
channel that explains why widgets
(especially widgets with
constconstructors) are more performant than functions.
Use saveLayer() thoughtfully
Some Flutter code uses
saveLayer(), an expensive operation,
to implement various visual effects in the UI.
Even if your code doesn’t explicitly call
other widgets or packages that you use might call it behind the scenes.
Perhaps your app is calling
saveLayer() more than necessary;
excessive calls to
saveLayer() can cause jank.
Why is saveLayer expensive?
saveLayer() allocates an offscreen buffer
and drawing content into the offscreen buffer might
trigger a render target switch.
The GPU wants to run like a firehose,
and a render target switch forces the GPU
to redirect that stream temporarily and then
direct it back again. On mobile GPUs this is
particularly disruptive to rendering throughput.
When is saveLayer required?
At runtime, if you need to dynamically display various shapes
coming from a server (for example), each with some transparency,
that might (or might not) overlap,
then you pretty much have to use
Debugging calls to saveLayer
How can you tell how often your app calls
either directly or indirectly?
saveLayer() method triggers
an event on the DevTools timeline; learn when
your scene uses
saveLayer by checking the
switch in the DevTools Performance view.
Minimizing calls to saveLayer
Can you avoid calls to
It might require rethinking of how you
create your visual effects:
- If the calls are coming from your code, can you
reduce or eliminate them?
For example, perhaps your UI overlaps two shapes,
each having non-zero transparency:
- If they always overlap in the same amount,
in the same way, with the same transparency,
you can precalculate what this overlapped,
semi-transparent object looks like, cache it,
and use that instead of calling
saveLayer(). This works with any static shape you can precalculate.
- Can you refactor your painting logic to avoid overlaps altogether?
- If they always overlap in the same amount, in the same way, with the same transparency, you can precalculate what this overlapped, semi-transparent object looks like, cache it, and use that instead of calling
- If the calls are coming from a package that you don’t own, contact the package owner and ask why these calls are necessary. Can they be reduced or eliminated? If not, you might need to find another package, or write your own.
Other widgets that might trigger
and are potentially costly:
Chip—might trigger a call to
disabledColorAlpha != 0xff
Text—might trigger a call to
saveLayer()if there’s an
Minimize use of opacity and clipping
Opacity is another expensive operation, as is clipping. Here are some tips you might find to be useful:
- Use the
Opacitywidget only when necessary. See the Transparent image section in the
OpacityAPI page for an example of applying opacity directly to an image, which is faster than using the
- Instead of wrapping simple shapes or text
Opacitywidget, it’s usually faster to just draw them with a semitransparent color. (Though this only works if there are no overlapping bits in the to-be-drawn shape.)
- To implement fading in an image, consider using the
FadeInImagewidget, which applies a gradual opacity using the GPU’s fragment shader. For more information, check out the
Clipping doesn’t call
saveLayer()(unless explicitly requested with
Clip.antiAliasWithSaveLayer), so these operations aren’t as expensive as
Opacity, but clipping is still costly, so use with caution. By default, clipping is disabled (
Clip.none), so you must explicitly enable it when needed.
- To create a rectangle with rounded corners,
instead of applying a clipping rectangle,
consider using the
borderRadiusproperty offered by many of the widget classes.
Implement grids and lists thoughtfully
How your grids and lists are implemented might be causing performance problems for your app. This section describes an important best practice when creating grids and lists, and how to determine whether your app uses excessive layout passes.
When building a large grid or list, use the lazy builder methods, with callbacks. That ensures that only the visible portion of the screen is built at startup time.
For more information and examples, check out:
- Working with long lists in the Cookbook
ListViewthat loads one page at a time a community article by AbdulRahman AlHamali
For information on how intrinsic passes might be causing problems with your grids and lists, see the next section.
Minimize layout passes caused by intrinsic operations
If you’ve done much Flutter programming, you are probably familiar with how layout and constraints work when creating your UI. You might even have memorized Flutter’s basic layout rule: Constraints go down. Sizes go up. Parent sets position.
For some widgets, particularly grids and lists, the layout process can be expensive. Flutter strives to perform just one layout pass over the widgets but, sometimes, a second pass (called an intrinsic pass) is needed, and that can slow performance.
What is an intrinsic pass?
An intrinsic pass happens when, for example, you want all cells to have the size of the biggest or smallest cell (or some similar calculation that requires polling all cells).
For example, consider a large grid of
A grid should have uniformly sized cells,
so the layout code performs a pass,
starting from the root of the grid (in the widget tree),
asking each card in the grid (not just the
visible cards) to return
its intrinsic size—the size
that the widget prefers, assuming no constraints.
With this information,
the framework determines a uniform cell size,
and re-visits all grid cells a second time,
telling each card what size to use.
Debugging intrinsic passes
To determine whether you have excessive intrinsic passes, enable the Track layouts option in DevTools (disabled by default), and look at the app’s stack trace to learn how many layout passes were performed. Once you enable tracking, intrinsic timeline events are labeled as ‘$runtimeType intrinsics’.
Avoiding intrinsic passes
You have a couple options for avoiding the intrinsic pass:
- Set the cells to a fixed size up front.
- Choose a particular cell to be the “anchor” cell—all cells will be sized relative to this cell. Write a custom render object that positions the child anchor first and then lays out the other children around it.
Build and display frames in 16ms
Since there are two separate threads for building and rendering, you have 16ms for building, and 16ms for rendering on a 60Hz display. If latency is a concern, build and display a frame in 16ms or less. Note that means built in 8ms or less, and rendered in 8ms or less, for a total of 16ms or less.
If your frames are rendering in well under 16ms total in profile mode, you likely don’t have to worry about performance even if some performance pitfalls apply, but you should still aim to build and render a frame as fast as possible. Why?
- Lowering the frame render time below 16ms might not make a visual difference, but it improves battery life and thermal issues.
- It might run fine on your device, but consider performance for the lowest device you are targeting.
- As 120fps devices become more widely available, you’ll want to render frames in under 8ms (total) in order to provide the smoothest experience.
If you are wondering why 60fps leads to a smooth visual experience, check out the video Why 60fps?
If you need to tune your app’s performance, or perhaps the UI isn’t as smooth as you expect, the DevTools Performance view can help!
Also, the Flutter plugin for your IDE might be useful. In the Flutter Performance window, enable the Show widget rebuild information check box. This feature helps you detect when frames are being rendered and displayed in more than 16ms. Where possible, the plugin provides a link to a relevant tip.
The following behaviors might negatively impact your app’s performance.
Avoid using the
Opacitywidget, and particularly avoid it in an animation. Use
FadeInImageinstead. For more information, check out Performance considerations for opacity animation.
When using an
AnimatedBuilder, avoid putting a subtree in the builder function that builds widgets that don’t depend on the animation. This subtree is rebuilt for every tick of the animation. Instead, build that part of the subtree once and pass it as a child to the
AnimatedBuilder. For more information, check out Performance optimizations.
Avoid clipping in an animation. If possible, pre-clip the image before animating it.
Avoid using constructors with a concrete
Listof children (such as
ListView()) if most of the children are not visible on screen to avoid the build cost.
For more performance info, check out the following resources: