text
stringlengths
1
372
the flutter framework (along with any code you write) is compiled to JavaScript.
it’s also worthy to note that dart has very few language semantic differences
across all its modes (jit versus AOT, native versus web compilation), and most
developers will never write a line of code that runs into such a difference.
during development time, flutter web uses
dartdevc, a compiler that supports
incremental compilation and therefore allows hot restart (although not currently
hot reload) for apps. conversely, when you are ready to create a production app
for the web, dart2js, dart’s
highly-optimized production JavaScript compiler is used, packaging the flutter
core and framework along with your application into a minified source file that
can be deployed to any web server. code can be offered in a single file or split
into multiple files through deferred imports.
<topic_end>
<topic_start>
further information
for those interested in more information about the internals of flutter, the
inside flutter whitepaper
provides a useful guide to the framework’s design philosophy.
footnotes:
1 while the build function returns a fresh tree,
you only need to return something different if there’s some new
configuration to incorporate. if the configuration is in fact the same, you can
just return the same widget.
2 this is a slight simplification for ease of
reading. in practice, the tree might be more complex.
3 there are some limitations with this approach, for
example, transparency doesn’t composite the same way for a platform view as it
would for other flutter widgets.
4 one example is shadows, which have to be
approximated with DOM-equivalent primitives at the cost of some fidelity.
<topic_end>
<topic_start>
inside flutter
this document describes the inner workings of the flutter toolkit that make
flutter’s API possible. because flutter widgets are built using aggressive
composition, user interfaces built with flutter have a large number of
widgets. to support this workload, flutter uses sublinear algorithms for
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