iOS已有项目集成flutter,网上大把资料,这里主要记录互相跳转传值
OC跳转到flutter界面,如跳转到HomePage
OC代码
// 1.初始化flutter控制器,并指定路由 “home”,flutter中根据该路由标识显示对应的界面
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil initialRoute:@"home" nibName:nil bundle:nil];
//2. 设置flutter HomePage标题
flutterViewController.navigationItem.title = @"原生项目设置的首页title";
//3. 跳转
[self.navigationController pushViewController:flutterViewController animated:YES];
flutter代码
import 'dart:ui' as ui;
void main() {
// 获取原生跳转的路由,根据路由显示对应的界面,ui.window.defaultRouteName就是上面设置的 “home”路由标识
runApp(run(ui.window.defaultRouteName));
}
//根据路由标识返回对应的Widget,这里需要注意的是如果是单独的界面,必须使用MaterialApp包裹住,不然跳转过来后显示不了当前ui
Widget run(String name){
switch (name) {
case "detail":
return DetailPage();
break;
case "home":
return HomePage();
break;
case "myApp":
return MyApp();
break;
}
return Center(
child: Text('Unknown route: $name'),
);
}
OC跳转到flutter界面时携带一个字典参数
OC代码
//1. 创建事件通道对象,唯一标识 “hometest”,到时flutter是根据该标识来监听原生发送给flutter的参数信息
FlutterEventChannel *evenChannel = [FlutterEventChannel eventChannelWithName:@"hometest" binaryMessenger:flutterViewController.binaryMessenger];
//2. 当原生跳往flutter时会触发下面的- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events回调方法,可以在该方法中给flutter传递参数
[evenChannel setStreamHandler:self];
//原生跳转flutter时,会触发该方法,在该方法中可以传递参数给flutter界面,需要注意的是flutter代码中必须写上对应的监听代码,这里才会被执行
- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events{
if (events) {
events(@{@"key":@"value"});
}
return nil;
}
flutter接受参数的代码
String str = "flutter之前的参数";
// 用于监听原生调用flutter
static const EventChannel homeChannel = const EventChannel('hometest');
@override
void initState() {
// TODO: implement initState
super.initState();
//_onEventHome 监听回调方法
homeChannel.receiveBroadcastStream(12345).listen(_onEventHome,onError: _onErrorHome);
}
// 回调事件 首页
void _onEventHome(Object event) {
if (event is Map){
str = event["key"];
}else if(event is String){
str = event.toString();
}else{
str = "其他类型";
}
setState(() {
});
}
// 错误返回
void _onErrorHome(Object error) {
}
flutter调用OC,并携带一个字典参数
flutter代码
// 用于调用原生方法 "hometestmethod"标识符与OC中的监听标识符保持一致
var homechannelmethod = MethodChannel("hometestmethod");
onTap: (){
//给原生发送消息并传入参数,原生根据标识homePageCallNativeMethond来做对应的处理
homechannelmethod.invokeMethod('homePageCallNativeMethond',{"key":"value","key1":"value1"});
},
OC代码
// 1.创建方法通道对象,用于监听flutter调用原生时的回调,唯一标识“hometestmethod”与flutter要保持一致
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"hometestmethod" binaryMessenger:flutterViewController.binaryMessenger];
//2. 设置监听回调block,flutter端通过通道调用原生方法时会进入以下回调
[channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
//call的属性method是flutter调用原生方法的方法名,我们进行字符串判断然后写入不同的逻辑
if ([call.method isEqualToString:@"homePageCallNativeMethond"]) {
//flutter传给原生的参数
id para = call.arguments;
NSLog(@"flutter传给原生的参数:%@", para);
//可以做界面跳转
[self.navigationController pushViewController:[TestViewController new] animated:YES];
//获取一个字符串
NSString *nativeFinalStr = @"原生给flutter回传的值";
if (nativeFinalStr!=nil) {
//把获取到的字符串传值给flutter
result(nativeFinalStr);
}else{
//异常(比如改方法是调用原生的getString获取一个字符串,但是返回的是nil(空值),这显然是不对的,就可以向flutter抛出异常 进入catch处理)
result([FlutterError errorWithCode:@"001" message:[NSString stringWithFormat:@"进入异常处理"] details:@"进入flutter的trycatch方法的catch方法"]);
}
}else{
//调用的方法原生没有对应的处理 抛出未实现的异常
result(FlutterMethodNotImplemented);
}
}];