text
stringlengths 1
372
|
|---|
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>
|
this approach is also used for
|
navigator, which provides
|
page routing; and
|
MediaQuery, which provides
|
access to screen metrics such as orientation, dimensions, and brightness.
|
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.
|
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,
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.