text
stringlengths 1
474
|
|---|
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:As widget trees get deeper, however, passing state information up and down the
|
tree hierarchy becomes cumbersome. So, a third widget type,
|
InheritedWidget,
|
provides an easy way to grab data from a shared ancestor. You can use
|
InheritedWidget to create a state widget that wraps a common ancestor in the
|
widget tree, as shown in this example:Whenever one of the ExamWidget or GradeWidget objects needs data from
|
StudentState, it can now access it with a command such as:The of(context) call takes the build context (a handle to the current widget
|
location), and returns the nearest ancestor in the
|
tree
|
that matches the StudentState type. InheritedWidgets also offer an
|
updateShouldNotify() method, which Flutter calls to determine whether a state
|
change should trigger a rebuild of child widgets that use it.Flutter itself uses InheritedWidget extensively as part of the framework for
|
shared state, such as the application’s visual theme, which includes
|
properties like color and type
|
styles that are
|
pervasive throughout an application. The MaterialApp build() method inserts
|
a theme in the tree when it builds, and then deeper in the hierarchy a widget
|
can use the .of() method to look up the relevant theme data, for example:
|
<code_start>Container(
|
color: Theme.of(context).secondaryHeaderColor,
|
child: Text(
|
'Text with a background color',
|
style: Theme.of(context).textTheme.titleLarge,
|
),
|
);<code_end>
|
As applications grow, more advanced state management approaches that reduce the
|
ceremony of creating and using stateful widgets become more attractive. Many
|
Flutter apps use utility packages like
|
provider, which provides a wrapper around
|
InheritedWidget. Flutter’s layered architecture also enables alternative
|
approaches to implement the transformation of state into UI, such as the
|
flutter_hooks package.<topic_end>
|
<topic_start>
|
Rendering and layout
|
This section describes the rendering pipeline, which is the series of steps that
|
Flutter takes to convert a hierarchy of widgets into the actual pixels painted
|
onto a screen.<topic_end>
|
<topic_start>
|
Flutter’s rendering model
|
You may be wondering: if Flutter is a cross-platform framework, then how can it
|
offer comparable performance to single-platform frameworks?It’s useful to start by thinking about how traditional
|
Android apps work. When drawing,
|
you first call the Java code of the Android framework.
|
The Android system libraries provide the components
|
responsible for drawing themselves to a Canvas object,
|
which Android can then render with Skia,
|
a graphics engine written in C/C++ that calls the
|
CPU or GPU to complete the drawing on the device.Cross-platform frameworks typically work by creating
|
an abstraction layer over the underlying native
|
Android and iOS UI libraries, attempting to smooth out the
|
inconsistencies of each platform representation.
|
App code is often written in an interpreted language like JavaScript,
|
which must in turn interact with the Java-based
|
Android or Objective-C-based iOS system libraries to display UI.
|
All this adds overhead that can be significant,
|
particularly where there is a lot of
|
interaction between the UI and the app logic.By contrast, Flutter minimizes those abstractions,
|
bypassing the system UI widget libraries in favor
|
of its own widget set. The Dart code that paints
|
Flutter’s visuals is compiled into native code,
|
which uses Skia (or, in the future, Impeller) for rendering.
|
Flutter also embeds its own copy of Skia as part of the engine,
|
allowing the developer to upgrade their app to stay
|
updated with the latest performance improvements
|
even if the phone hasn’t been updated with a new Android version.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.