美文网首页
不考虑内存问题FLutter和Native简单交互(iOS)

不考虑内存问题FLutter和Native简单交互(iOS)

作者: 扫地僧练级 | 来源:发表于2020-11-24 14:16 被阅读0次
创建 FlutterEngine 和 FlutterViewController

appdelegate中didFinishLaunchingWithOptions方法增加:

_fEngine = [[FlutterEngine alloc] initWithName:@"xxxxxx.flutterengine"];//唯一就行
[_fEngine run];//默认执行main.dart 也可以指定flutter入口(flutter 项目中要对应,否则会报错)

测试Native调用Flutter,(模拟从native跳转flutte页面)

FlutterViewController *fvc = [[FlutterViewController alloc] initWithProject:nil initialRoute:@"/detail/1234567" nibName:nil bundle:nil];
return fvc;

"/detail/1234567"是flutter的一个路由,123456是参数

测试Flutter调用Native,(模拟从flutte页面返回native页面)

FlutterViewController *fvc = [[FlutterViewController alloc] initWithProject:nil initialRoute:@"/detail/1234567" nibName:nil bundle:nil];
FlutterMethodChannel * messageChannel = [FlutterMethodChannel methodChannelWithName:@"xxxxxx.flutter" binaryMessenger:fvc];
[messageChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
NSLog(@"flutter call native:\n method=%@ \n arguments = %@",call.method,call.arguments);
if ([call.method isEqualToString:@"dismiss"]) {
if (result) {
// 回掉给flutter
result(@"flutter view dismiss");
}
[fvc.navigationController popViewControllerAnimated:YES];
}
}];
return fvc;

Flutter侧实现

onPressed: (){
// 和native交互-返回
MethodChannel(MethodChannelName).invokeMethod('dismiss','').then((value) {});
},

Flutter路由实现

class Routers {
static String root = "/";
static String detailPage = "/detial/:id";
static void configureRouter(FluroRouter router) {
router.notFoundHandler = Handler(
handlerFunc: (BuildContext context, Map<String, List<String>> params) {
return UnKnownPage();
},
);
// detailPage路由页面配置
router.define(detailPage, handler: detailHanderl);
}

路由使用随便,上面用的是Fluro

  • 以上是 MethodChanel的交互,还有两种 EventChannel和MessageChannel的交互后面再续*

相关文章

网友评论

      本文标题:不考虑内存问题FLutter和Native简单交互(iOS)

      本文链接:https://www.haomeiwen.com/subject/pfuiiktx.html