Platform views allow you to embed native views in a Flutter app, so you can apply transforms, clips, and opacity to the native view from Dart.
This allows you, for example, to use the native Google Maps from the Android SDK directly inside your Flutter app.
Note: This page discusses how to host your own native views within a Flutter app. If you’d like to embed native iOS views in your Flutter app, see Hosting native iOS views.
Flutter supports two modes: Virtual displays and Hybrid composition.
Which one to use depends on the use case. Let’s take a look:
-
Virtual displays render the android.view.View instance to a texture, so it’s not embedded within the Android Activity’s view hierachy. Certain platform interactions such as keyboard handling and accessibility features might not work.
-
Hybrid composition appends the native android.view.View to the view hierarchy. Therefore, keyboard handling, and accessibility work out of the box. Prior to Android 10, this mode might significantly reduce the frame throughput (FPS) of the Flutter UI. See performance for more info.
Read more: Bearded george clooney
To create a platform view on Android, use the following steps:
On the Dart side
On the Dart side, create a Widget and add the following build implementation:
Warning: For this to work, your plugin or app must use Android embedding v2. If you haven’t updated your plugin, see the plugin migration guide.
Read more: Modern restaurant exterior
In your Dart file, for example native_view_example.dart, use the following instructions:
-
Add the following imports:
-
Implement a build() method:
For more information, see the API docs for:
- PlatformViewLink
- AndroidViewSurface
- PlatformViewsService
Virtual Display
Read more: Modern restaurant exterior
In your Dart file, for example native_view_example.dart, use the following instructions:
-
Add the following imports:
-
Implement a build() method:
For more information, see the API docs for:
- AndroidView
On the platform side
On the platform side, use the standard io.flutter.plugin.platform package in either Java or Kotlin:
- Kotlin
- Java
For more information, see the API docs for:
- FlutterPlugin
- PlatformViewRegistry
- PlatformViewFactory
- PlatformView
Finally, modify your build.gradle file to require one of the minimal Android SDK versions:
Performance
Read more: Unable to authenticate your authentication token seems to be invalid
Platform views in Flutter come with performance trade-offs.
For example, in a typical Flutter app, the Flutter UI is composed on a dedicated raster thread. This allows Flutter apps to be fast, as the main platform thread is rarely blocked.
While a platform view is rendered with Hybrid composition, the Flutter UI is composed from the platform thread, which competes with other tasks like handling OS or plugin messages.
Prior to Android 10, Hybrid composition copied each Flutter frame out of the graphic memory into main memory, and then copied it back to a GPU texture. As this copy happens per frame, the performance of the entire Flutter UI might be impacted. In Android 10 or above, the graphics memory is copied once.
Virtual display, on the other hand, makes each pixel of the native view flow through additional intermediate graphic buffers, which cost graphic memory and drawing performance.
For complex cases, there are some techniques that can be used to mitigate these issues.
For example, you could use a placeholder texture while an animation is happening in Dart. In other words, if an animation is slow while a platform view is rendered, then consider taking a screenshot of the native view and rendering it as a texture.
For more information, see:
- TextureLayer
- TextureRegistry
- FlutterTextureRegistry
- FlutterImageView