text stringlengths 1 474 |
|---|
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 |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.