1.FlutterView
结合Lifecycle自定义的view
import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor
class FlutterLifecycleView : FlutterView, LifecycleObserver {
companion object {
fun createEngine(
context: Context,
flutterEngineId: String,
dartEntrypointFunctionName: String = ""
): FlutterEngine {
var engine = FlutterEngineCache.getInstance().get(flutterEngineId)
if (engine == null) {
engine = FlutterEngine(context.applicationContext)
val createDefault = if (dartEntrypointFunctionName == "") {
DartExecutor.DartEntrypoint.createDefault()
} else {
DartExecutor.DartEntrypoint(
App.instance!!.flutterLoader.findAppBundlePath(),
dartEntrypointFunctionName
)
}
engine.dartExecutor.executeDartEntrypoint(createDefault)
FlutterEngineCache.getInstance().put(flutterEngineId, engine)
}
return engine
}
}
var mLifecycle: Lifecycle? = null
var mEngine: FlutterEngine? = null
/**
*
* constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
}
*/
constructor(context: Context, engine: FlutterEngine, lifecycle: Lifecycle) : super(context) {
lifecycle.addObserver(this)
attachToFlutterEngine(engine)
mLifecycle = lifecycle
mEngine = engine
mEngine?.lifecycleChannel?.appIsInactive()
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
mEngine?.lifecycleChannel?.appIsResumed()
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
mEngine?.lifecycleChannel?.appIsPaused()
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
mEngine?.lifecycleChannel?.appIsDetached()
detachFromFlutterEngine()
mLifecycle?.removeObserver(this)
}
}
在activity上使用
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import io.flutter.embedding.android.FlutterView
class MainActivity : AppCompatActivity() {
private val flutterEngineId = "flutterEngineId"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createFlutterView()
}
private fun createFlutterView(): FlutterView {
val engine = FlutterLifecycleView.createEngine(this, flutterEngineId)
val flutterView = FlutterLifecycleView(this, engine, lifecycle)
val params = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
addContentView(flutterView, params)
return flutterView
}
}
在application初始化flutter
import android.app.Application
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.loader.FlutterLoader
class App : Application() {
lateinit var flutterLoader: FlutterLoader
companion object {
var instance: App? = null
}
override fun onCreate() {
super.onCreate()
instance = this
/**
*
// FlutterMain.startInitialization(this)
// FlutterLoader.getInstance().startInitialization(this)
*/
flutterLoader = FlutterInjector.Builder()
.build()
.flutterLoader()
flutterLoader.startInitialization(this)
}
}
在setting.gradle上增加
setBinding(new Binding([gradle: this]))
evaluate(new File(
settingsDir.parentFile,
'module_flutter/.android/include_flutter.groovy'
))
原生项目同级目录下执行 以下语句
flutter create -t module module_flutter
androidx 项目也可以使用 flutter create --androidx -t module module_flutter
最终效果
2. FlutterFragment
import io.flutter.embedding.android.FlutterFragment
class BaseFlutterFragment : FlutterFragment() {
companion object {
fun create(): FlutterFragment {
// return createDefault()
return withNewEngine()
//可写可不写
.appBundlePath(App.instance!!.flutterLoader.findAppBundlePath())
//指定加载的方法名字
.dartEntrypoint("mai22")
//路由名字对于dart里面的 window.defaultRouteName
.initialRoute("route2")
.build()
}
}
}
在activity上使用
import android.os.Bundle
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import io.flutter.embedding.android.FlutterView
class MainActivity : AppCompatActivity() {
private val flutterEngineId = "flutterEngineId"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// createFlutterView()
supportFragmentManager.beginTransaction()
.replace(R.id.ffContainer, BaseFlutterFragment.create())
.commit()
}
}