website icon indicating copy to clipboard operation
website copied to clipboard

[PAGE ISSUE]: 'PlatformViewLink example when host native view in Android'

Open JimmyTai opened this issue 3 years ago • 0 comments

Page URL

https://docs.flutter.dev/development/platform-integration/android/platform-views#on-the-dart-side

Page source

https://github.com/flutter/website/blob/main/src/development/platform-integration/android/platform-views.md

Describe the problem

After Flutter 3.x, Hybrid composition PlatformViewLink.onCreatePlatformView should return PlatformViewsService.initExpensiveAndroidView() instead of PlatformViewsService.initSurfaceAndroidView()

Expected fix

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.initExpensiveAndroidView(
        id: params.id,
        viewType: viewType,
        layoutDirection: TextDirection.ltr,
        creationParams: creationParams,
        creationParamsCodec: const StandardMessageCodec(),
        onFocus: () {
          params.onFocusChanged(true);
        },
      )
        ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
        ..create();
    },
  );
}

Additional context

No response

JimmyTai avatar Sep 20 '22 08:09 JimmyTai