Create Flutter plugin from native sdk android (return activities) - Platform view?

487 Views Asked by At

I have access to a map SDK for Android but I'd like to use it in Flutter. (a second step is to integrate the iOS part)

So I thought a solution could be build a plugin that returns a map widget (even if i'm not a kotlin/swift master).

I understood I have to use platform view and build native code, following these guides:

official documentation

medium I found

but I was only able to return a text doing something like this:

/* MyMapWidgetPlugin.kt */
class MyMapWidgetPlugin: FlutterPlugin {
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
 binding
  .platformViewRegistry
  .registerViewFactory("my_map_widget", MyMapFactory())
}
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding)
} 

/* MyMapFactory.kt */
class MyMapFactory : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
  override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
    val creationParams = args as Map<String?, Any?>?
      return MyMapView(context, viewId, creationParams)
  }
}

/* MyMapView.kt */
internal class MyMapView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {

private var layoutInflater: LayoutInflater? = null
var myNativeView: View?;

init {
    layoutInflater = LayoutInflater.from(context)
    myNativeView = layoutInflater?.inflate(R.layout.test, null) // -> just a linear layout with a button inside
}

override fun getView(): View? {
    return myNativeView
}

override fun dispose() {}
}

Do you have any example where I can understand the flow?

I also already have a native android app with all xml, activities I want but I'm not able to understand how make a flutter widget from them...

Any help?

(I'm using latest stable flutter release)

EXTRA: This is the widget I'm returning at the moment:

class MyMapWidget extends StatelessWidget {
  final bool useAndroidViewSurface;
  final TextDirection textDirection;

  const MyMapWidget({
    Key? key,
    this.useAndroidViewSurface = true,
    this.textDirection = TextDirection.ltr,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    const String viewType = 'my_map_widget';
    const Map<String, dynamic> creationParams = <String, dynamic>{};
    if (defaultTargetPlatform == TargetPlatform.android) {
      if (useAndroidViewSurface) {
        return PlatformViewLink(
          viewType: viewType,
          surfaceFactory:
              (BuildContext context, PlatformViewController controller) {
            return AndroidViewSurface(
              controller: controller as AndroidViewController,
              gestureRecognizers: const <
                  Factory<OneSequenceGestureRecognizer>>{},
              hitTestBehavior: PlatformViewHitTestBehavior.opaque,
            );
          },
          onCreatePlatformView: (PlatformViewCreationParams params) {
            return PlatformViewsService.initSurfaceAndroidView(
              id: params.id,
              viewType: viewType,
              layoutDirection: textDirection,
              creationParams: creationParams,
              creationParamsCodec: const StandardMessageCodec(),
              onFocus: () => params.onFocusChanged(true),
            )
              ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
              ..create();
          },
        );
      }
    {...}
  }
}
0

There are 0 best solutions below