text
stringlengths 1
372
|
|---|
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.