如果没有引入FlutterBoost,可以使用FlutterView
// 通过FlutterView引入Flutter编写的页面
FlutterView flutterView = new FlutterView(this);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
FrameLayout flContainer = findViewById(R.id.fl_container);
flContainer.addView(flutterView, lp);
FlutterEngine flutterEngine = new FlutterEngine(this);
flutterEngine.getDartExecutor().executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
);
// 关键代码,将Flutter页面显示到FlutterView中
flutterView.attachToFlutterEngine(flutterEngine);
详情见https://blog.csdn.net/ruiruiddd/article/details/115318612
如果引入了FlutterBoost
,由于FlutterBoost
暂不支持FlutterView这种级别
Issue: [Android] Boost新增FlutterView级别支持
所以,就需要使用Fragment了。
我们需要新建一个DialogFragment
,代码如下所示
class MyTestFragmentDialog : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.layout_fragment_container,container,false)
return root
}
override fun onStart() {
super.onStart()
val window = dialog!!.window
if (window != null) {
val params = window.attributes
params.gravity = Gravity.BOTTOM
params.width = ScreenUtils.getAppScreenWidth()
window.attributes = params
}
val fragment =
FlutterBoostFragment.CachedEngineFragmentBuilder(FlutterBoostFragment::class.java)
.url("testPage") //TODO ---> 这里需要修改为你自己项目中的URL
.build<FlutterBoostFragment>()
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.layout_container, fragment)
transaction.commit()
}
}
进行调用
MyTestFragmentDialog().show(supportFragmentManager,"tag")
效果如下所示