text
stringlengths
1
372
a fresh element at that location, the framework will check the hash
table and reparent the existing element from its previous location to
its new location, preserving the entire subtree.
the render objects in the reparented subtree are able to preserve
their layout information because the layout constraints are the only
information that flows from parent to child in the render tree.
the new parent is marked dirty for layout because its child list has
changed, but if the new parent passes the child the same layout
constraints the child received from its old parent, the child can
return immediately from layout, cutting off the walk.
global keys and non-local tree mutations are used extensively by
developers to achieve effects such as hero transitions and navigation.
<topic_end>
<topic_start>
constant-factor optimizations
in addition to these algorithmic optimizations, achieving aggressive
composability also relies on several important constant-factor
optimizations. these optimizations are most important at the leaves of
the major algorithms discussed above.
child-model agnostic. unlike most toolkits, which use child lists,
flutter’s render tree does not commit to a specific child model.
for example, the RenderBox class has an abstract visitChildren()
method rather than a concrete firstChild and nextSibling interface.
many subclasses support only a single child, held directly as a member
variable, rather than a list of children. for example, RenderPadding
supports only a single child and, as a result, has a simpler layout
method that takes less time to execute.
visual render tree, logical widget tree. in flutter, the render
tree operates in a device-independent, visual coordinate system,
which means smaller values in the x coordinate are always towards
the left, even if the current reading direction is right-to-left.
the widget tree typically operates in logical coordinates, meaning
with start and end values whose visual interpretation depends
on the reading direction. the transformation from logical to visual
coordinates is done in the handoff between the widget tree and the
render tree. this approach is more efficient because layout and
painting calculations in the render tree happen more often than the
widget-to-render tree handoff and can avoid repeated coordinate conversions.
text handled by a specialized render object. the vast majority
of render objects are ignorant of the complexities of text. instead,
text is handled by a specialized render object, RenderParagraph,
which is a leaf in the render tree. rather than subclassing a
text-aware render object, developers incorporate text into their
user interface using composition. this pattern means RenderParagraph
can avoid recomputing its text layout as long as its parent supplies
the same layout constraints, which is common, even during tree surgery.
observable objects. flutter uses both the model-observation and
the reactive paradigms. obviously, the reactive paradigm is dominant,
but flutter uses observable model objects for some leaf data structures.
for example, animations notify an observer list when their value changes.
flutter hands off these observable objects from the widget tree to the
render tree, which observes them directly and invalidates only the
appropriate stage of the pipeline when they change. for example,
a change to an Animation<Color> might trigger only the paint phase
rather than both the build and paint phases.
taken together and summed over the large trees created by aggressive
composition, these optimizations have a substantial effect on performance.
<topic_end>
<topic_start>
separation of the element and RenderObject trees
the RenderObject and element (widget) trees in flutter are isomorphic
(strictly speaking, the RenderObject tree is a subset of the element
tree). an obvious simplification would be to combine these trees into
one tree. however, in practice there are a number of benefits to having
these trees be separate:
performance. when the layout changes, only the relevant parts of
the layout tree need to be walked. due to composition, the element
tree frequently has many additional nodes that would have to be skipped.
clarity. the clearer separation of concerns allows the widget
protocol and the render object protocol to each be specialized to
their specific needs, simplifying the API surface and thus lowering
the risk of bugs and the testing burden.
type safety. the render object tree can be more type safe since it
can guarantee at runtime that children will be of the appropriate type
(each coordinate system, e.g. has its own type of render object).
composition widgets can be agnostic about the coordinate system used
during layout (for example, the same widget exposing a part of the app
model could be used in both a box layout and a sliver layout), and thus
in the element tree, verifying the type of render objects would require
a tree walk.
<topic_end>
<topic_start>
infinite scrolling
infinite scrolling lists are notoriously difficult for toolkits.
flutter supports infinite scrolling lists with a simple interface
based on the builder pattern, in which a ListView uses a callback
to build widgets on demand as they become visible to the user during
scrolling. supporting this feature requires viewport-aware layout
and building widgets on demand.
<topic_end>
<topic_start>
viewport-aware layout
like most things in flutter, scrollable widgets are built using
composition. the outside of a scrollable widget is a viewport,
which is a box that is “bigger on the inside,” meaning its children
can extend beyond the bounds of the viewport and can be scrolled into
view. however, rather than having RenderBox children, a viewport has
RenderSliver children, known as slivers, which have a viewport-aware
layout protocol.
the sliver layout protocol matches the structure of the box layout