text
stringlengths
1
372
underlying rendering tree.
this API abstracts out the tree manipulation by combining the tree
creation and tree mutation steps into a single tree description (build)
step, where, after each change to the system state, the new configuration
of the user interface is described by the developer and the framework
computes the series of tree mutations necessary to reflect this new
configuration.
<topic_end>
<topic_start>
interpolation
since flutter’s framework encourages developers to describe the interface
configuration matching the current application state, a mechanism exists
to implicitly animate between these configurations.
for example, suppose that in state s1 the interface consists
of a circle, but in state s2 it consists of a square.
without an animation mechanism, the state change would have a jarring
interface change. an implicit animation allows the circle to be smoothly
squared over several frames.
each feature that can be implicitly animated has a stateful widget that
keeps a record of the current value of the input, and begins an animation
sequence whenever the input value changes, transitioning from the current
value to the new value over a specified duration.
this is implemented using lerp (linear interpolation) functions using
immutable objects. each state (circle and square, in this case)
is represented as an immutable object that is configured with
appropriate settings (color, stroke width, etc) and knows how to paint
itself. when it is time to draw the intermediate steps during the animation,
the start and end values are passed to the appropriate lerp function
along with a t value representing the point along the animation,
where 0.0 represents the start and 1.0 represents the
end8,
and the function returns a third immutable object representing the
intermediate stage.
for the circle-to-square transition, the lerp function would return
an object representing a “rounded square” with a radius described as
a fraction derived from the t value, a color interpolated using the
lerp function for colors, and a stroke width interpolated using the
lerp function for doubles. that object, which implements the
same interface as circles and squares, would then be able to paint
itself when requested to.
this technique allows the state machinery, the mapping of states to
configurations, the animation machinery, the interpolation machinery,
and the specific logic relating to how to paint each frame to be
entirely separated from each other.
this approach is broadly applicable. in flutter, basic types like
color and shape can be interpolated, but so can much more elaborate
types such as decoration, TextStyle, or theme. these are
typically constructed from components that can themselves be interpolated,
and interpolating the more complicated objects is often as simple as
recursively interpolating all the values that describe the complicated
objects.
some interpolatable objects are defined by class hierarchies. for example,
shapes are represented by the ShapeBorder interface, and there exists a
variety of shapes, including BeveledRectangleBorder, BoxBorder,
CircleBorder, RoundedRectangleBorder, and StadiumBorder. a single
lerp function can’t anticipate all possible types,
and therefore the interface instead defines lerpFrom and lerpTo methods,
which the static lerp method defers to. when told to interpolate from
a shape a to a shape b, first b is asked if it can lerpFrom a, then,
if it cannot, a is instead asked if it can lerpTo b. (if neither is
possible, then the function returns a from values of t less than 0.5,
and returns b otherwise.)
this allows the class hierarchy to be arbitrarily extended, with later
additions being able to interpolate between previously-known values
and themselves.
in some cases, the interpolation itself cannot be described by any of
the available classes, and a private class is defined to describe the
intermediate stage. this is the case, for instance, when interpolating
between a CircleBorder and a RoundedRectangleBorder.
this mechanism has one further added advantage: it can handle interpolation
from intermediate stages to new values. for example, half-way through
a circle-to-square transition, the shape could be changed once more,
causing the animation to need to interpolate to a triangle. so long as
the triangle class can lerpFrom the rounded-square intermediate class,
the transition can be seamlessly performed.
<topic_end>
<topic_start>
conclusion
flutter’s slogan, “everything is a widget,” revolves around building
user interfaces by composing widgets that are, in turn, composed of
progressively more basic widgets. the result of this aggressive
composition is a large number of widgets that require carefully
designed algorithms and data structures to process efficiently.
with some additional design, these data structures also make it
easy for developers to create infinite scrolling lists that build
widgets on demand when they become visible.
footnotes:
1 for layout, at least. it might be revisited
for painting, for building the accessibility tree if necessary,
and for hit testing if necessary.
2 reality, of course, is a bit more
complicated. some layouts involve intrinsic dimensions or baseline
measurements, which do involve an additional walk of the relevant subtree
(aggressive caching is used to mitigate the potential for quadratic
performance in the worst case). these cases, however, are surprisingly
rare. in particular, intrinsic dimensions are not required for the
common case of shrink-wrapping.
3 technically, the child’s position is not
part of its RenderBox geometry and therefore need not actually be
calculated during layout. many render objects implicitly position