說明/注意點:
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保存起來,在需要傳值的地方觸發回調就好了。