text stringlengths 1 474 |
|---|
}<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, |
); |
typedef MessageBoxDart = int Function( |
int hWnd, |
Pointer<Utf16> lpText, |
Pointer<Utf16> lpCaption, |
int uType, |
); |
void exampleFfi() { |
final user32 = DynamicLibrary.open('user32.dll'); |
final messageBox = |
user32.lookupFunction<MessageBoxNative, MessageBoxDart>('MessageBoxW'); |
final result = messageBox( |
0, // No owner window |
'Test message'.toNativeUtf16(), // Message |
'Window caption'.toNativeUtf16(), // Window title |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.