text stringlengths 1 474 |
|---|
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 |
their single child at 0,0 relative to their own origin, which |
requires no computation or storage at all. Some render objects |
avoid computing the position of their children until the last |
possible moment (for example, during the paint phase), to avoid |
the computation entirely if they are not subsequently painted.4 There exists one exception to this rule. |
As discussed in the Building widgets on demand |
section, some widgets can be rebuilt as a result of a change in layout |
constraints. If a widget marked itself dirty for unrelated reasons in |
the same frame that it also is affected by a change in layout constraints, |
it will be updated twice. This redundant build is limited to the |
widget itself and does not impact its descendants.5 A key is an opaque object optionally |
associated with a widget whose equality operator is used to influence |
the reconciliation algorithm.6 For accessibility, and to give applications |
a few extra milliseconds between when a widget is built and when it |
appears on the screen, the viewport creates (but does not paint) |
widgets for a few hundred pixels before and after the visible widgets.7 This approach was first made popular by |
Facebook’s React library.8 In practice, the t value is allowed |
to extend past the 0.0-1.0 range, and does so for some curves. For |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.