进几年来flutter越来越火了,不学点感觉都要被时代给抛弃了。此文章只是记录一下学习。
在AS中新建flutter module
在settings.gradle中添加一下
setBinding(new Binding([gradle: this]))
evaluate(new File( settingsDir.parentFile, 'my_module/.android/include_flutter.groovy' ))
my_module是所键module的名字,直接替换就可以了
在activity中创建FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在activity中创建flutter的Fragment,并在生命周期中回调flutter的生命周期方法
override fun onPostResume() {
super.onPostResume()
flutterFragment!!.onPostResume()
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.let {
flutterFragment!!.onNewIntent(intent)
}
}
override fun onBackPressed() {
super.onBackPressed()
flutterFragment!!.onBackPressed()
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
flutterFragment!!.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onUserLeaveHint() {
super.onUserLeaveHint()
flutterFragment!!.onUserLeaveHint()
}
override fun onTrimMemory(level: Int) {
super.onTrimMemory(level)
flutterFragment!!.onTrimMemory(level)
}
替换fragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_flutter_view)
val fragmentLayout = findViewById<FrameLayout>(R.id.fragment_container)
val fragmentManager: FragmentManager = supportFragmentManager
flutterFragment =
fragmentManager.findFragmentByTag(TAG_FLUTTER_FRAGMENT) as FlutterFragment?
if (flutterFragment == null) {
//添加指定的view
var newFlutterFragment = FlutterFragment.withNewEngine()
.dartEntrypoint("mySpecialEntrypoint")//需要在main方法外再添加新的入口
.renderMode(RenderMode.texture)
.build<FlutterFragment>()
flutterFragment = newFlutterFragment
fragmentManager.beginTransaction()
.add(R.id.fragment_container, newFlutterFragment, TAG_FLUTTER_FRAGMENT)
.commitAllowingStateLoss()
}
}
更改flutter入口方法,默认情况下main方法,一下是自定义的方法,需要你在main.dart中添加该方法。
FlutterFragment.withNewEngine().dartEntrypoint("mySpecialEntrypoint")
main.dart中默认是执行main方法,使用dartEntrypoint配置就会执行mySpecialEntrypoint这个方法,这样就可以自定义我们要加载的那个view.
void main() => runApp(MyApp());
void mySpecialEntrypoint() {
runApp(
MaterialApp(
home: AddOriginalFragment(),
),
);
}
flutter调用原生方法
fragmentLayout.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
Log.e("onResume", "======")
fragmentLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
flutterFragment!!.flutterEngine?.let { registerFlutter(it) }
if (flutterFragment!!.flutterEngine == null) {
Log.e("onResume", "flutterFragment is null")
}
}
})
private fun registerFlutter(flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine)
val methodChannel: MethodChannel =
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, FLUTTER_USED_NAME)
methodChannel.setMethodCallHandler(object : MethodChannel.MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
//TODO("Not yet implemented")
Toast.makeText(this@AddFlutterViewActivity, "flutter 调用的", Toast.LENGTH_SHORT)
.show()
result.success("调用成功")
}
})
}
在页面加载完成后注册flutter通信通道,FLUTTER_USED_NAME是与flutter通道的句柄。在flutter中要用到
static const platormChannl = const MethodChannel("com.AddFlutterViewActivity.flutter_used_name");
void originalFunction() async{
try{
String result = (await platormChannl.invokeListMethod("flutterJumpFunction")) as String;
} on Exception catch(e){
print(e.toString());
}
}
调用原生方法要使用异步方法,flutterJumpFunction可以在原生这边call: MethodCall获取到。
这样就能走通的flutter调原生这一流程
网友评论