说明/注意点:
1.Flutter模块建立之后会生成隐藏文件.android和.iOS,混合混的就是这个。
2.Android需要跳转到flutter页面最简单的方式是startActivity(FlutterActivity.createDefaultIntent(this));这里FlutterActivity来自io.flutter.embedding.android.FlutterActivity,切勿弄错。
3.Android三种跳转方式(核心:FlutterEngine):
a.进入到默认flutter - main 页面
startActivity(FlutterActivity.createDefaultIntent(this));
b.进入到flutter - 指定route页面
startActivity(FlutterActivity.withNewEngine().initialRoute("123").build(this));
c.进入到指定方法页面 - 通过执行指定到方法进入到页面,默认指定的是main,所以这个算是对a,b的复杂化写法
先得创建一个FlutterEngine去启动这个方法
FlutterEngine engine =new FlutterEngine(this);
DartExecutor.DartEntrypoint dartEntrypoint =new DartExecutor.DartEntrypoint(FlutterMain.findAppBundlePath(),"test");
engine.getDartExecutor().executeDartEntrypoint(dartEntrypoint);
FlutterEngineCache.getInstance().put("test",engine);
startActivity(FlutterActivity.withCachedEngine("test").build(this));
4.iOS三种跳转方式(核心:FlutterEngine)
a.进入到默认flutter - main 页面
FlutterEngine *flutterEngine = [[FlutterEngine alloc] initWithName:@"engine"];
[flutterEngine run];
FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
b.进入到flutter - 指定route页面 - !!不能创建engine,如果要获取engine从 flutterViewController.engine - 官方api在setInitialRoute有说明
FlutterViewController *flutterViewController = [[FlutterViewController alloc]initWithProject:[[FlutterDartProject alloc]init] nibName:nil bundle:nil];
[flutterViewController setInitialRoute:@"123"];
[self presentViewController:flutterViewController animated:YES completion:nil];
c.进入到指定方法页面 - 通过执行指定到方法进入到页面,默认指定的是main,所以这个算是对a的复杂化写法
FlutterEngine *flutterEngine = [[FlutterEngine alloc] initWithName:@"engine1"];;
[flutterEngine runWithEntrypoint:@"test" libraryURI:nil];
FlutterViewController*flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nilbundle:nil];
[self presentViewController:flutterViewController animated:YES completion:nil];
注意:Android 中 new FlutterEngine 就必须指定这个engine执行哪个方法,即executeDartEntrypoint,所以c特殊;iOS 中run就是默认main,但是如果需要指定路由进入则不能手动调用run,所以b特殊。
5.交互:Flutter和Android方使用的是MethodChannel和EventChannel; iOS方使用的是FlutterMethodChannel和FlutterEventChannel。核心依然是FlutterEngine,Engine就是一个贯穿始终的驱动器,用哪个Engine驱动的页面也应该用哪个Engine去交互。
6.Flutter传输到Native直接创建一个MethodChannel然后methodChannel.invokeMethod()就可以;Native注意channelName和methodName保持一致,可以通过result回调函数回调结果给flutter。
7.Native传输到Flutter通过创建一个EventChannel,flutter通过eventChannel.receiveBroadcastStream().listen,涉及到stream,那么怎么把东西添加到stream呢,自然是sink。所以Native setStreamHandler 将接口/协议中的EventSink保存起来,在需要传值的地方触发回调就好了。