从本机SDK Android(返回活动) - 平台视图中创建Flutter插件?
我可以访问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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论