text
stringlengths
1
372
protocol in that parents pass constraints down to their children and
receive geometry in return. however, the constraint and geometry data
differs between the two protocols. in the sliver protocol, children
are given information about the viewport, including the amount of
visible space remaining. the geometry data they return enables a
variety of scroll-linked effects, including collapsible headers and
parallax.
different slivers fill the space available in the viewport in different
ways. for example, a sliver that produces a linear list of children lays
out each child in order until the sliver either runs out of children or
runs out of space. similarly, a sliver that produces a two-dimensional
grid of children fills only the portion of its grid that is visible.
because they are aware of how much space is visible, slivers can produce
a finite number of children even if they have the potential to produce
an unbounded number of children.
slivers can be composed to create bespoke scrollable layouts and effects.
for example, a single viewport can have a collapsible header followed
by a linear list and then a grid. all three slivers will cooperate through
the sliver layout protocol to produce only those children that are actually
visible through the viewport, regardless of whether those children belong
to the header, the list, or the grid6.
<topic_end>
<topic_start>
building widgets on demand
if flutter had a strict build-then-layout-then-paint pipeline,
the foregoing would be insufficient to implement an infinite scrolling
list because the information about how much space is visible through
the viewport is available only during the layout phase. without
additional machinery, the layout phase is too late to build the
widgets necessary to fill the space. flutter solves this problem
by interleaving the build and layout phases of the pipeline. at any
point in the layout phase, the framework can start building new
widgets on demand as long as those widgets are descendants of the
render object currently performing layout.
interleaving build and layout is possible only because of the strict
controls on information propagation in the build and layout algorithms.
specifically, during the build phase, information can propagate only
down the tree. when a render object is performing layout, the layout
walk has not visited the subtree below that render object, which means
writes generated by building in that subtree cannot invalidate any
information that has entered the layout calculation thus far. similarly,
once layout has returned from a render object, that render object will
never be visited again during this layout, which means any writes
generated by subsequent layout calculations cannot invalidate the
information used to build the render object’s subtree.
additionally, linear reconciliation and tree surgery are essential
for efficiently updating elements during scrolling and for modifying
the render tree when elements are scrolled into and out of view at
the edge of the viewport.
<topic_end>
<topic_start>
API ergonomics
being fast only matters if the framework can actually be used effectively.
to guide flutter’s API design towards greater usability, flutter has been
repeatedly tested in extensive UX studies with developers. these studies
sometimes confirmed pre-existing design decisions, sometimes helped guide
the prioritization of features, and sometimes changed the direction of the
API design. for instance, flutter’s APIs are heavily documented; UX
studies confirmed the value of such documentation, but also highlighted
the need specifically for sample code and illustrative diagrams.
this section discusses some of the decisions made in flutter’s API design
in aid of usability.
<topic_end>
<topic_start>
specializing APIs to match the developer’s mindset
the base class for nodes in flutter’s widget, element, and RenderObject
trees does not define a child model. this allows each node to be
specialized for the child model that is applicable to that node.
most widget objects have a single child widget, and therefore only expose
a single child parameter. some widgets support an arbitrary number of
children, and expose a children parameter that takes a list.
some widgets don’t have any children at all and reserve no memory,
and have no parameters for them. similarly, RenderObjects expose APIs
specific to their child model. RenderImage is a leaf node, and has no
concept of children. RenderPadding takes a single child, so it has storage
for a single pointer to a single child. RenderFlex takes an arbitrary
number of children and manages it as a linked list.
in some rare cases, more complicated child models are used. the
RenderTable render object’s constructor takes an array of arrays of
children, the class exposes getters and setters that control the number
of rows and columns, and there are specific methods to replace
individual children by x,y coordinate, to add a row, to provide a
new array of arrays of children, and to replace the entire child list
with a single array and a column count. in the implementation,
the object does not use a linked list like most render objects but
instead uses an indexable array.
the chip widgets and InputDecoration objects have fields that match
the slots that exist on the relevant controls. where a one-size-fits-all
child model would force semantics to be layered on top of a list of
children, for example, defining the first child to be the prefix value
and the second to be the suffix, the dedicated child model allows for
dedicated named properties to be used instead.
this flexibility allows each node in these trees to be manipulated in
the way most idiomatic for its role. it’s rare to want to insert a cell
in a table, causing all the other cells to wrap around; similarly,
it’s rare to want to remove a child from a flex row by index instead
of by reference.
the RenderParagraph object is the most extreme case: it has a child of
an entirely different type, TextSpan. at the RenderParagraph boundary,
the RenderObject tree transitions into being a TextSpan tree.