text stringlengths 1 474 |
|---|
The same is true for Flutter on other native platforms, |
such as Windows or macOS.info Note |
Flutter 3.10 set Impeller as the default |
rendering engine on iOS. It’s in preview |
for Android behind a flag.<topic_end> |
<topic_start> |
From user input to the GPU |
The overriding principle that Flutter applies to its rendering pipeline is that |
simple is fast. Flutter has a straightforward pipeline for how data flows to |
the system, as shown in the following sequencing diagram:Let’s take a look at some of these phases in greater detail.<topic_end> |
<topic_start> |
Build: from Widget to Element |
Consider this code fragment that demonstrates a widget hierarchy: |
<code_start>Container( |
color: Colors.blue, |
child: Row( |
children: [ |
Image.network('https://www.example.com/1.png'), |
const Text('A'), |
], |
), |
);<code_end> |
When Flutter needs to render this fragment, it calls the build() method, which |
returns a subtree of widgets that renders UI based on the current app state. |
During this process, the build() method can introduce new widgets, as |
necessary, based on its state. As an example, in the preceding code |
fragment, Container has color and child properties. From looking at the |
source |
code |
for Container, you can see that if the color is not null, it inserts a |
ColoredBox representing the color:Correspondingly, the Image and Text widgets might insert child widgets such |
as RawImage and RichText during the build process. The eventual widget |
hierarchy may therefore be deeper than what the code represents, as in this |
case2:This explains why, when you examine the tree through a debug tool such as the |
Flutter inspector, part of the |
Dart DevTools, you might see a structure that is considerably deeper than what |
is in your original code.During the build phase, Flutter translates the widgets expressed in code into a |
corresponding element tree, with one element for every widget. Each element |
represents a specific instance of a widget in a given location of the tree |
hierarchy. There are two basic types of elements:RenderObjectElements are an intermediary between their widget analog and the |
underlying RenderObject, which we’ll come to later.The element for any widget can be referenced through its BuildContext, which |
is a handle to the location of a widget in the tree. This is the context in a |
function call such as Theme.of(context), and is supplied to the build() |
method as a parameter.Because widgets are immutable, including the parent/child relationship between |
nodes, any change to the widget tree (such as changing Text('A') to |
Text('B') in the preceding example) causes a new set of widget objects to be |
returned. But that doesn’t mean the underlying representation must be rebuilt. |
The element tree is persistent from frame to frame, and therefore plays a |
critical performance role, allowing Flutter to act as if the widget hierarchy is |
fully disposable while caching its underlying representation. By only walking |
through the widgets that changed, Flutter can rebuild just the parts of the |
element tree that require reconfiguration.<topic_end> |
<topic_start> |
Layout and rendering |
It would be a rare application that drew only a single widget. An important part |
of any UI framework is therefore the ability to efficiently lay out a hierarchy |
of widgets, determining the size and position of each element before they are |
rendered on the screen.The base class for every node in the render tree is |
RenderObject, which |
defines an abstract model for layout and painting. This is extremely general: it |
does not commit to a fixed number of dimensions or even a Cartesian coordinate |
system (demonstrated by this example of a polar coordinate |
system). Each |
RenderObject knows its parent, but knows little about its children other than |
how to visit them and their constraints. This provides RenderObject with |
sufficient abstraction to be able to handle a variety of use cases.During the build phase, Flutter creates or updates an object that inherits from |
RenderObject for each RenderObjectElement in the element tree. |
RenderObjects are primitives: |
RenderParagraph |
renders text, |
RenderImage renders |
an image, and |
RenderTransform |
applies a transformation before painting its child.Most Flutter widgets are rendered by an object that inherits from the |
RenderBox subclass, which represents a RenderObject of fixed size in a 2D |
Cartesian space. RenderBox provides the basis of a box constraint model, |
establishing a minimum and maximum width and height for each widget to be |
rendered.To perform layout, Flutter walks the render tree in a depth-first traversal and |
passes down size constraints from parent to child. In determining its size, |
the child must respect the constraints given to it by its parent. Children |
respond by passing up a size to their parent object within the constraints |
the parent established.At the end of this single walk through the tree, every object has a defined size |
within its parent’s constraints and is ready to be painted by calling the |
paint() |
method.The box constraint model is very powerful as a way to layout objects in O(n) |
time:This model works even when a child object needs to know how much space it has |
available to decide how it will render its content. By using a |
LayoutBuilder widget, |
the child object can examine the passed-down constraints and use those to |
determine how it will use them, for example: |
<code_start>Widget build(BuildContext context) { |
return LayoutBuilder( |
builder: (context, constraints) { |
if (constraints.maxWidth < 600) { |
return const OneColumnLayout(); |
} else { |
return const TwoColumnLayout(); |
} |
}, |
); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.