text
stringlengths
1
372
);
}
<code_end>
more information about the constraint and layout system,
along with working examples, can be found in the
understanding constraints topic.
the root of all RenderObjects is the RenderView, which represents the total
output of the render tree. when the platform demands a new frame to be rendered
(for example, because of a
vsync or because
a texture decompression/upload is complete), a call is made to the
compositeFrame() method, which is part of the RenderView object at the root
of the render tree. this creates a SceneBuilder to trigger an update of the
scene. when the scene is complete, the RenderView object passes the composited
scene to the window.render() method in dart:ui, which passes control to the
GPU to render it.
further details of the composition and rasterization stages of the pipeline are
beyond the scope of this high-level article, but more information can be found
in this talk on the flutter rendering
pipeline.
<topic_end>
<topic_start>
platform embedding
as we’ve seen, rather than being translated into the equivalent OS widgets,
flutter user interfaces are built, laid out, composited, and painted by flutter
itself. the mechanism for obtaining the texture and participating in the app
lifecycle of the underlying operating system inevitably varies depending on the
unique concerns of that platform. the engine is platform-agnostic, presenting a
stable ABI (application binary
interface)
that provides a platform embedder with a way to set up and use flutter.
the platform embedder is the native OS application that hosts all flutter
content, and acts as the glue between the host operating system and flutter.
when you start a flutter app, the embedder provides the entrypoint, initializes
the flutter engine, obtains threads for UI and rastering, and creates a texture
that flutter can write to. the embedder is also responsible for the app
lifecycle, including input gestures (such as mouse, keyboard, touch), window
sizing, thread management, and platform messages. flutter includes platform
embedders for android, iOS, windows, macOS, and linux; you can also create a
custom platform embedder, as in this worked
example that supports remoting
flutter sessions through a VNC-style framebuffer or this worked example for
raspberry pi.
each platform has its own set of APIs and constraints. some brief
platform-specific notes:
<topic_end>
<topic_start>
integrating with other code
flutter provides a variety of interoperability mechanisms, whether you’re
accessing code or APIs written in a language like kotlin or swift, calling a
native c-based API, embedding native controls in a flutter app, or embedding
flutter in an existing application.
<topic_end>
<topic_start>
platform channels
for mobile and desktop apps, flutter allows you to call into custom code through
a platform channel, which is a mechanism for communicating between your
dart code and the platform-specific code of your host app. by creating a common
channel (encapsulating a name and a codec), you can send and receive messages
between dart and a platform component written in a language like kotlin or
swift. data is serialized from a dart type like map into a standard format,
and then deserialized into an equivalent representation in kotlin (such as
HashMap) or swift (such as dictionary).
the following is a short platform channel example of a dart call to a receiving
event handler in kotlin (android) or swift (ios):
<code_start>
// dart side
const channel = MethodChannel('foo');
final greeting = await channel.invokeMethod('bar', 'world') as string;
print(greeting);
<code_end>
further examples of using platform channels, including examples for desktop
platforms, can be found in the flutter/packages
repository. there are also thousands of plugins
already available for flutter that cover many common
scenarios, ranging from firebase to ads to device hardware like camera and
bluetooth.
<topic_end>
<topic_start>
foreign function interface
for c-based APIs, including those that can be generated for code written in
modern languages like rust or go, dart provides a direct mechanism for binding
to native code using the dart:ffi library. the foreign function interface
(ffi) model can be considerably faster than platform channels, because no
serialization is required to pass data. instead, the dart runtime provides the
ability to allocate memory on the heap that is backed by a dart object and make
calls to statically or dynamically linked libraries. FFI is available for all
platforms other than web, where the js package
serves an equivalent purpose.
to use FFI, you create a typedef for each of the dart and unmanaged method
signatures, and instruct the dart VM to map between them. as an example,
here’s a fragment of code to call the traditional win32 MessageBox() API:
<code_start>
import 'dart:ffi';
import 'package:ffi/ffi.dart'; // contains .tonativeutf16() extension method
typedef MessageBoxNative = int32 function(
IntPtr hWnd,
Pointer<Utf16> lpText,
Pointer<Utf16> lpCaption,
int32 uType,