text
stringlengths
1
474
designed to be optional and replaceable.To the underlying operating system, Flutter applications are packaged in the
same way as any other native application. A platform-specific embedder provides
an entrypoint; coordinates with the underlying operating system for access to
services like rendering surfaces, accessibility, and input; and manages the
message event loop. The embedder is written in a language that is appropriate
for the platform: currently Java and C++ for Android, Objective-C/Objective-C++
for iOS and macOS, and C++ for Windows and Linux. Using the embedder, Flutter
code can be integrated into an existing application as a module, or the code may
be the entire content of the application. Flutter includes a number of embedders
for common target platforms, but other embedders also
exist.At the core of Flutter is the Flutter engine,
which is mostly written in C++ and supports
the primitives necessary to support all Flutter applications.
The engine is responsible for rasterizing composited scenes
whenever a new frame needs to be painted.
It provides the low-level implementation of Flutter’s core API,
including graphics (through Impeller on iOS and coming to Android,
and Skia on other platforms) text layout,
file and network I/O, accessibility support,
plugin architecture, and a Dart runtime
and compile toolchain.The engine is exposed to the Flutter framework through
dart:ui,
which wraps the underlying C++ code in Dart classes. This library
exposes the lowest-level primitives, such as classes for driving input,
graphics, and text rendering subsystems.Typically, developers interact with Flutter through the Flutter framework,
which provides a modern, reactive framework written in the Dart language. It
includes a rich set of platform, layout, and foundational libraries, composed of
a series of layers. Working from the bottom to the top, we have:The Flutter framework is relatively small; many higher-level features that
developers might use are implemented as packages, including platform plugins
like camera and
webview, as well as platform-agnostic
features like characters,
http, and
animations that build upon the core Dart and
Flutter libraries. Some of these packages come from the broader ecosystem,
covering services like in-app
payments, Apple
authentication, and
animations.The rest of this overview broadly navigates down the layers, starting with the
reactive paradigm of UI development. Then, we describe how widgets are composed
together and converted into objects that can be rendered as part of an
application. We describe how Flutter interoperates with other code at a platform
level, before giving a brief summary of how Flutter’s web support differs from
other targets.<topic_end>
<topic_start>
Anatomy of an app
The following diagram gives an overview of the pieces
that make up a regular Flutter app generated by flutter create.
It shows where the Flutter Engine sits in this stack,
highlights API boundaries, and identifies the repositories
where the individual pieces live. The legend below clarifies
some of the terminology commonly used to describe the
pieces of a Flutter app.Dart AppFramework (source code)Engine (source code)Embedder (source code)Runner<topic_end>
<topic_start>
Reactive user interfaces
On the surface, Flutter is a reactive, declarative UI framework,
in which the developer provides a mapping from application state to interface
state, and the framework takes on the task of updating the interface at runtime
when the application state changes. This model is inspired by
work that came from Facebook for their own React framework,
which includes a rethinking of many traditional design principles.In most traditional UI frameworks, the user interface’s initial state is
described once and then separately updated by user code at runtime, in response
to events. One challenge of this approach is that, as the application grows in
complexity, the developer needs to be aware of how state changes cascade
throughout the entire UI. For example, consider the following UI:There are many places where the state can be changed: the color box, the hue
slider, the radio buttons. As the user interacts with the UI, changes must be
reflected in every other place. Worse, unless care is taken, a minor change to
one part of the user interface can cause ripple effects to seemingly unrelated
pieces of code.One solution to this is an approach like MVC, where you push data changes to the
model via the controller, and then the model pushes the new state to the view
via the controller. However, this also is problematic, since creating and
updating UI elements are two separate steps that can easily get out of sync.Flutter, along with other reactive frameworks, takes an alternative approach to
this problem, by explicitly decoupling the user interface from its underlying
state. With React-style APIs, you only create the UI description, and the
framework takes care of using that one configuration to both create and/or
update the user interface as appropriate.In Flutter, widgets (akin to components in React) are represented by immutable
classes that are used to configure a tree of objects. These widgets are used to
manage a separate tree of objects for layout, which is then used to manage a
separate tree of objects for compositing. Flutter is, at its core, a series of
mechanisms for efficiently walking the modified parts of trees, converting trees
of objects into lower-level trees of objects, and propagating changes across
these trees.A widget declares its user interface by overriding the build() method, which
is a function that converts state to UI:The build() method is by design fast to execute and should be free of side
effects, allowing it to be called by the framework whenever needed (potentially
as often as once per rendered frame).This approach relies on certain characteristics of a language runtime (in
particular, fast object instantiation and deletion). Fortunately, Dart is
particularly well suited for this
task.<topic_end>
<topic_start>
Widgets
As mentioned, Flutter emphasizes widgets as a unit of composition. Widgets are
the building blocks of a Flutter app’s user interface, and each widget is an
immutable declaration of part of the user interface.Widgets form a hierarchy based on composition. Each widget nests inside its
parent and can receive context from the parent. This structure carries all the
way up to the root widget (the container that hosts the Flutter app, typically
MaterialApp or CupertinoApp), as this trivial example shows:
<code_start>import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {