我正在做一个 Flutter 项目,我必须添加 第三方库 来扫描护照和身份证。该库包含在 Android 和 iOS 原生 SDK 中。 (我们已经购买了,目前没有替代品)
所以,我决定使用 AndroidView 和 UIKitView 在 Flutter 中呈现平台特定的功能。
为了清楚起见,我可以使用 PlatformView 将 Native TextView、颜色和其他布局加载到 Flutter 端。 (这是工作)
internal class NativeView(context: Context, id: Int, creationParams: Map<String?, Any?>?) : PlatformView {
private val textView: TextView
override fun getView(): View {
return textView
}
override fun dispose() {}
init {
textView = TextView(context)
textView.textSize = 72f
textView.setBackgroundColor(Color.rgb(255, 255, 255))
textView.text = "Rendered on a native Android view (id: $id)"
}
}
实际问题: 我没有加载 TextView,而是尝试从第三方库加载片段
init {
val id = View.generateViewId()
val vParams: ViewGroup.LayoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT
)
container = FrameLayout(context)
container.layoutParams = vParams
container.id = id
//fragment from Third party SDK
val fragment: Fragment = DocumentAutoCaptureContainerFragment()
//finding the root fragment activity
val fragmentActivity = context.getFragmentActivityOrThrow()
//replacing the layout of container with fragment
val fm = fragmentActivity.supportFragmentManager
fm.beginTransaction().replace(id, fragment).commit()
}
令你惊讶的是,上面的代码确实有效..我可以看到相机预览(准备扫描文档)
现在你可能会说那是什么问题呢? 好吧,让我再解释一下。
我可以看到 Camera 的预览(来自库 SDK),但它还没有初始化它的一些功能,我看不到文档覆盖、边框等
但是你猜怎么着,我也有解决办法。 如果我只添加这行代码,我可以获得包含所有边框和功能的相机预览(似乎触发了一些生命周期事件)
..
..
val fragmentActivity = context.getFragmentActivityOrThrow()
fragmentActivity.setContentView(container)
..
..
但还是有问题。如果我
setContentView
与 fragment
那么整个屏幕将被片段 UI 覆盖,我将看不到任何 flutter UI(甚至硬件后退按钮都不起作用)
最终,这就是问题所在。
fragment
(缺少一些用户界面和功能)setContentView
来填充fragment
UI(覆盖整个屏幕)我需要一些指导来正确地将 Android Fragment UI 填充到平台视图中,以便它在 Flutter AndroidView 上完美运行。