text
stringlengths
1
474
On the Dart side
On the Dart side, create a Widget
and add one of the following build implementations.<topic_end>
<topic_start>Hybrid composition
In your Dart file,
for example native_view_example.dart,
use the following instructions:Add the following imports:
<code_start>import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';<code_end>
Implement a build() method:
<code_start>Widget build(BuildContext context) {
// This is used in the platform side to register the view.
const String viewType = '<platform-view-type>';
// Pass parameters to the platform side.
const Map<String, dynamic> creationParams = <String, dynamic>{};
return PlatformViewLink(
viewType: viewType,
surfaceFactory: (context, controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () {
params.onFocusChanged(true);
},
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
);
}<code_end>
For more information, see the API docs for:<topic_end>
<topic_start>Virtual display
In your Dart file,
for example native_view_example.dart,
use the following instructions:Add the following imports:
<code_start>import 'package:flutter/material.dart';
import 'package:flutter/services.dart';<code_end>
Implement a build() method:
<code_start>Widget build(BuildContext context) {
// This is used in the platform side to register the view.
const String viewType = '<platform-view-type>';
// Pass parameters to the platform side.
final Map<String, dynamic> creationParams = <String, dynamic>{};
return AndroidView(
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}<code_end>
For more information, see the API docs for:<topic_end>
<topic_start>
On the platform side
On the platform side, use the standard
io.flutter.plugin.platform package
in either Java or Kotlin:In your native code, implement the following:Extend io.flutter.plugin.platform.PlatformView
to provide a reference to the android.view.View
(for example, NativeView.kt):Create a factory class that creates an instance of the
NativeView created earlier
(for example, NativeViewFactory.kt):Finally, register the platform view.
You can do this in an app or a plugin.For app registration,
modify the app’s main activity
(for example, MainActivity.kt):For plugin registration,
modify the plugin’s main class
(for example, PlatformViewPlugin.kt):In your native code, implement the following:Extend io.flutter.plugin.platform.PlatformView
to provide a reference to the android.view.View
(for example, NativeView.java):Create a factory class that creates an
instance of the NativeView created earlier
(for example, NativeViewFactory.java):Finally, register the platform view.
You can do this in an app or a plugin.For app registration,
modify the app’s main activity
(for example, MainActivity.java):For plugin registration,
modify the plugin’s main file
(for example, PlatformViewPlugin.java):For more information, see the API docs for:Finally, modify your build.gradle file
to require one of the minimal Android SDK versions:<topic_end>
<topic_start>Manual view invalidation
Certain Android Views do not invalidate themselves when their content changes.
Some example views include SurfaceView and SurfaceTexture.
When your Platform View includes these views you are required to
manually invalidate the view after they have been drawn to
(or more specifically: after the swap chain is flipped).
Manual view invalidation is done by calling invalidate on the View
or one of its parent views.<topic_end>
<topic_start>
Performance
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,