text
stringlengths
1
474
layout and building widgets as well as data structures that make tree
surgery efficient and that have a number of constant-factor optimizations.
With some additional details, this design also makes it easy for developers
to create infinite scrolling lists using callbacks that build exactly those
widgets that are visible to the user.<topic_end>
<topic_start>
Aggressive composability
One of the most distinctive aspects of Flutter is its aggressive
composability. Widgets are built by composing other widgets,
which are themselves built out of progressively more basic widgets.
For example, Padding is a widget rather than a property of other widgets.
As a result, user interfaces built with Flutter consist of many,
many widgets.The widget building recursion bottoms out in RenderObjectWidgets,
which are widgets that create nodes in the underlying render tree.
The render tree is a data structure that stores the geometry of the user
interface, which is computed during layout and used during painting and
hit testing. Most Flutter developers do not author render objects directly
but instead manipulate the render tree using widgets.In order to support aggressive composability at the widget layer,
Flutter uses a number of efficient algorithms and optimizations at
both the widget and render tree layers, which are described in the
following subsections.<topic_end>
<topic_start>
Sublinear layout
With a large number of widgets and render objects, the key to good
performance is efficient algorithms. Of paramount importance is the
performance of layout, which is the algorithm that determines the
geometry (for example, the size and position) of the render objects.
Some other toolkits use layout algorithms that are O(N²) or worse
(for example, fixed-point iteration in some constraint domain).
Flutter aims for linear performance for initial layout, and sublinear
layout performance in the common case of subsequently updating an
existing layout. Typically, the amount of time spent in layout should
scale more slowly than the number of render objects.Flutter performs one layout per frame, and the layout algorithm works
in a single pass. Constraints are passed down the tree by parent
objects calling the layout method on each of their children.
The children recursively perform their own layout and then return
geometry up the tree by returning from their layout method. Importantly,
once a render object has returned from its layout method, that render
object will not be visited again1
until the layout for the next frame. This approach combines what might
otherwise be separate measure and layout passes into a single pass and,
as a result, each render object is visited at most
twice2 during layout: once on the way
down the tree, and once on the way up the tree.Flutter has several specializations of this general protocol.
The most common specialization is RenderBox, which operates in
two-dimensional, cartesian coordinates. In box layout, the constraints
are a min and max width and a min and max height. During layout,
the child determines its geometry by choosing a size within these bounds.
After the child returns from layout, the parent decides the child’s
position in the parent’s coordinate system3.
Note that the child’s layout cannot depend on its position,
as the position is not determined until after the child
returns from the layout. As a result, the parent is free to reposition
the child without needing to recompute its layout.More generally, during layout, the only information that flows from
parent to child are the constraints and the only information that
flows from child to parent is the geometry. These invariants can reduce
the amount of work required during layout:If the child has not marked its own layout as dirty, the child can
return immediately from layout, cutting off the walk, as long as the
parent gives the child the same constraints as the child received
during the previous layout.Whenever a parent calls a child’s layout method, the parent indicates
whether it uses the size information returned from the child. If,
as often happens, the parent does not use the size information,
then the parent need not recompute its layout if the child selects
a new size because the parent is guaranteed that the new size will
conform to the existing constraints.Tight constraints are those that can be satisfied by exactly one
valid geometry. For example, if the min and max widths are equal to
each other and the min and max heights are equal to each other,
the only size that satisfies those constraints is one with that
width and height. If the parent provides tight constraints,
then the parent need not recompute its layout whenever the child
recomputes its layout, even if the parent uses the child’s size
in its layout, because the child cannot change size without new
constraints from its parent.A render object can declare that it uses the constraints provided
by the parent only to determine its geometry. Such a declaration
informs the framework that the parent of that render object does
not need to recompute its layout when the child recomputes its layout
even if the constraints are not tight and even if the parent’s
layout depends on the child’s size, because the child cannot change
size without new constraints from its parent.As a result of these optimizations, when the render object tree contains
dirty nodes, only those nodes and a limited part of the subtree around
them are visited during layout.<topic_end>
<topic_start>
Sublinear widget building
Similar to the layout algorithm, Flutter’s widget building algorithm
is sublinear. After being built, the widgets are held by the element
tree, which retains the logical structure of the user interface.
The element tree is necessary because the widgets themselves are
immutable, which means (among other things), they cannot remember their
parent or child relationships with other widgets. The element tree also
holds the state objects associated with stateful widgets.In response to user input (or other stimuli), an element can become dirty,
for example if the developer calls setState() on the associated state
object. The framework keeps a list of dirty elements and jumps directly
to them during the build phase, skipping over clean elements. During
the build phase, information flows unidirectionally down the element
tree, which means each element is visited at most once during the build
phase. Once cleaned, an element cannot become dirty again because,
by induction, all its ancestor elements are also
clean4.Because widgets are immutable, if an element has not marked itself as
dirty, the element can return immediately from build, cutting off the walk,
if the parent rebuilds the element with an identical widget. Moreover,