我有一个PlatformView
用于在Flutter应用程序中显示地图。平台视图位于插件中,因此它可以访问PluginRegistry
,并且可以使用方法PlatformViewRegistry platformViewRegistry()
注册平台视图。
但是,我想将其从插件移到主项目中,以便将所有内容保留在一个项目中。
有可能吗,在那种情况下我应该如何访问platformViewRegistry
?
答案 0 :(得分:5)
我是这样做的,使用您在主要活动中获得的flutterEngine
参考来获得注册表:
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
YourPlugin.registerWith(flutterEngine)
}
import io.flutter.embedding.engine.FlutterEngine
object YourPlugin{
fun registerWith(engine: FlutterEngine) {
engine
.platformViewsController.registry
.registerViewFactory(
YourView.VIEW_NAME, YourViewFactory(engine.dartExecutor.binaryMessenger))
}
}
import android.content.Context
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory
class YourViewFactory(private val messenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(context: Context, id: Int, o: Any?): PlatformView {
return YourView(context, messenger, id)
}
}