text
stringlengths
1
372
accessibility. in each of these cases, the corresponding vocabulary ends up
being large: there are hundreds of widgets and render objects, and dozens of
animation and tween types.
the class hierarchy is deliberately shallow and broad to maximize the possible
number of combinations, focusing on small, composable widgets that each do one
thing well. core features are abstract, with even basic features like padding
and alignment being implemented as separate components rather than being built
into the core. (this also contrasts with more traditional APIs where features
like padding are built in to the common core of every layout component.) so, for
example, to center a widget, rather than adjusting a notional align property,
you wrap it in a center
widget.
there are widgets for padding, alignment, rows, columns, and grids. these layout
widgets do not have a visual representation of their own. instead, their sole
purpose is to control some aspect of another widget’s layout. flutter also
includes utility widgets that take advantage of this compositional approach.
for example, container, a
commonly used widget, is made up of several widgets responsible for layout,
painting, positioning, and sizing. specifically, container is made up of the
LimitedBox,
ConstrainedBox,
align,
padding,
DecoratedBox, and
transform widgets, as you
can see by reading its source code. a defining characteristic of flutter is that
you can drill down into the source for any widget and examine it. so, rather
than subclassing container to produce a customized effect, you can compose it
and other widgets in novel ways, or just create a new widget using
container as inspiration.
<topic_end>
<topic_start>
building widgets
as mentioned earlier, you determine the visual representation of a widget by
overriding the
build() function to
return a new element tree. this tree represents the widget’s part of the user
interface in more concrete terms. for example, a toolbar widget might have a
build function that returns a horizontal
layout of some
text and
various
buttons. as needed,
the framework recursively asks each widget to build until the tree is entirely
described by concrete renderable
objects. the
framework then stitches together the renderable objects into a renderable object
tree.
a widget’s build function should be free of side effects. whenever the function
is asked to build, the widget should return a new tree of widgets1, regardless of what the widget previously returned. the
framework does the heavy lifting work to determine which build methods need to
be called based on the render object tree (described in more detail later). more
information about this process can be found in the inside flutter
topic.
on each rendered frame, flutter can recreate just the parts of the UI where the
state has changed by calling that widget’s build() method. therefore it is
important that build methods should return quickly, and heavy computational work
should be done in some asynchronous manner and then stored as part of the state
to be used by a build method.
while relatively naïve in approach, this automated comparison is quite
effective, enabling high-performance, interactive apps. and, the design of the
build function simplifies your code by focusing on declaring what a widget is
made of, rather than the complexities of updating the user interface from one
state to another.
<topic_end>
<topic_start>
widget state
the framework introduces two major classes of widget: stateful and stateless
widgets.
many widgets have no mutable state: they don’t have any properties that change
over time (for example, an icon or a label). these widgets subclass
StatelessWidget.
however, if the unique characteristics of a widget need to change based on user
interaction or other factors, that widget is stateful. for example, if a
widget has a counter that increments whenever the user taps a button, then the
value of the counter is the state for that widget. when that value changes, the
widget needs to be rebuilt to update its part of the UI. these widgets subclass
StatefulWidget, and
(because the widget itself is immutable) they store mutable state in a separate
class that subclasses state.
StatefulWidgets don’t have a build method; instead, their user interface is
built through their state object.
whenever you mutate a state object (for example, by incrementing the counter),
you must call setState()
to signal the framework to update the user interface by calling the state’s
build method again.
having separate state and widget objects lets other widgets treat both stateless
and stateful widgets in exactly the same way, without being concerned about
losing state. instead of needing to hold on to a child to preserve its state,
the parent can create a new instance of the child at any time without losing the
child’s persistent state. the framework does all the work of finding and reusing
existing state objects when appropriate.
<topic_end>
<topic_start>
state management
so, if many widgets can contain state, how is state managed and passed around
the system?
as with any other class, you can use a constructor in a widget to initialize its
data, so a build() method can ensure that any child widget is instantiated
with the data it needs: