从本机SDK Android(返回活动) - 平台视图中创建Flutter插件?

发布于 2025-01-23 07:06:20 字数 3333 浏览 0 评论 0原文

我可以访问Android的MAP SDK,但我想将其用于颤动。 (第二步是集成iOS零件)

因此,我认为可以构建一个插件的解决方案(即使我不是Kotlin/Swift Master)。

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

我找到的媒介

但我只能返回这样做这样的文字:

/* 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() {}
}

您有任何示例我能理解流程吗?

我也已经拥有一个带有所有XML的本地Android应用程序,我想要的活动,但我无法理解如何从中制作flutter脚的小部件...

有什么帮助吗?

(我正在使用最新的稳定扑来版本)

额外: 这是我目前要返回的小部件:

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();
          },
        );
      }
    {...}
  }
}

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();
          },
        );
      }
    {...}
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文