text
stringlengths
1
474
0, // OK button only
);
}<code_end>
<topic_end>
<topic_start>
Rendering native controls in a Flutter app
Because Flutter content is drawn to a texture and its widget tree is entirely
internal, there’s no place for something like an Android view to exist within
Flutter’s internal model or render interleaved within Flutter widgets. That’s a
problem for developers that would like to include existing platform components
in their Flutter apps, such as a browser control.Flutter solves this by introducing platform view widgets
(AndroidView
and UiKitView)
that let you embed this kind of content on each platform. Platform views can be
integrated with other Flutter content3. Each of
these widgets acts as an intermediary to the underlying operating system. For
example, on Android, AndroidView serves three primary functions:Inevitably, there is a certain amount of overhead associated with this
synchronization. In general, therefore, this approach is best suited for complex
controls like Google Maps where reimplementing in Flutter isn’t practical.Typically, a Flutter app instantiates these widgets in a build() method based
on a platform test. As an example, from the
google_maps_flutter plugin:Communicating with the native code underlying the AndroidView or UiKitView
typically occurs using the platform channels mechanism, as previously described.At present, platform views aren’t available for desktop platforms, but this is
not an architectural limitation; support might be added in the future.<topic_end>
<topic_start>
Hosting Flutter content in a parent app
The converse of the preceding scenario is embedding a Flutter widget in an
existing Android or iOS app. As described in an earlier section, a newly created
Flutter app running on a mobile device is hosted in an Android activity or iOS
UIViewController. Flutter content can be embedded into an existing Android or
iOS app using the same embedding API.The Flutter module template is designed for easy embedding; you can either embed
it as a source dependency into an existing Gradle or Xcode build definition, or
you can compile it into an Android Archive or iOS Framework binary for use
without requiring every developer to have Flutter installed.The Flutter engine takes a short while to initialize, because it needs to load
Flutter shared libraries, initialize the Dart runtime, create and run a Dart
isolate, and attach a rendering surface to the UI. To minimize any UI delays
when presenting Flutter content, it’s best to initialize the Flutter engine
during the overall app initialization sequence, or at least ahead of the first
Flutter screen, so that users don’t experience a sudden pause while the first
Flutter code is loaded. In addition, separating the Flutter engine allows it to
be reused across multiple Flutter screens and share the memory overhead involved
with loading the necessary libraries.More information about how Flutter is loaded into an existing Android or iOS app
can be found at the Load sequence, performance and memory
topic.<topic_end>
<topic_start>
Flutter web support
While the general architectural concepts apply to all platforms that Flutter
supports, there are some unique characteristics of Flutter’s web support that
are worthy of comment.Dart has been compiling to JavaScript for as long as the language has existed,
with a toolchain optimized for both development and production purposes. Many
important apps compile from Dart to JavaScript and run in production today,
including the advertiser tooling for Google Ads.
Because the Flutter framework is written in Dart, compiling it to JavaScript was
relatively straightforward.However, the Flutter engine, written in C++,
is designed to interface with the
underlying operating system rather than a web browser.
A different approach is therefore required.
On the web, Flutter provides a reimplementation of the
engine on top of standard browser APIs.
We currently have two options for
rendering Flutter content on the web: HTML and WebGL.
In HTML mode, Flutter uses HTML, CSS, Canvas, and SVG.
To render to WebGL, Flutter uses a version of Skia
compiled to WebAssembly called
CanvasKit.
While HTML mode offers the best code size characteristics,
CanvasKit provides the fastest path to the
browser’s graphics stack,
and offers somewhat higher graphical fidelity with the
native mobile targets4.The web version of the architectural layer diagram is as follows:Perhaps the most notable difference compared to other platforms on which Flutter
runs is that there is no need for Flutter to provide a Dart runtime. Instead,
the Flutter framework (along with any code you write) is compiled to JavaScript.
It’s also worthy to note that Dart has very few language semantic differences
across all its modes (JIT versus AOT, native versus web compilation), and most
developers will never write a line of code that runs into such a difference.During development time, Flutter web uses
dartdevc, a compiler that supports
incremental compilation and therefore allows hot restart (although not currently
hot reload) for apps. Conversely, when you are ready to create a production app
for the web, dart2js, Dart’s
highly-optimized production JavaScript compiler is used, packaging the Flutter
core and framework along with your application into a minified source file that
can be deployed to any web server. Code can be offered in a single file or split
into multiple files through deferred imports.<topic_end>
<topic_start>
Further information
For those interested in more information about the internals of Flutter, the
Inside Flutter whitepaper
provides a useful guide to the framework’s design philosophy.Footnotes:1 While the build function returns a fresh tree,
you only need to return something different if there’s some new
configuration to incorporate. If the configuration is in fact the same, you can
just return the same widget.2 This is a slight simplification for ease of
reading. In practice, the tree might be more complex.3 There are some limitations with this approach, for
example, transparency doesn’t composite the same way for a platform view as it
would for other Flutter widgets.4 One example is shadows, which have to be
approximated with DOM-equivalent primitives at the cost of some fidelity.
<topic_end>
<topic_start>Inside Flutter
This document describes the inner workings of the Flutter toolkit that make
Flutter’s API possible. Because Flutter widgets are built using aggressive
composition, user interfaces built with Flutter have a large number of
widgets. To support this workload, Flutter uses sublinear algorithms for