1.IOS原生跳转flutter页面时的传值。在flutter页面初始化之前注册通知,在flutter页面初始化里监听事件(此时flutter也可以回传参数给原生IOS)。
代码:
IOS的点击事件代码:
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
flutterViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[flutterViewControllersetInitialRoute:@"Page"];
// 要与main.dart中一致
NSString *channelName = @"com.xxx.xx/Page";
FlutterEventChannel *evenChannal = [FlutterEventChannel eventChannelWithName:channelName binaryMessenger:flutterViewController.binaryMessenger];
// 代理FlutterStreamHandler
[evenChannalsetStreamHandler:self];
[self presentViewController:flutterViewController animated:YES completion:nil];
#pragma mark -
// // 这个onListen是Flutter端开始监听这个channel时的回调,第二个参数 EventSink是用来传数据的载体。
- (FlutterError *)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events{
// arguments flutter给native的参数
// 回调给flutter, 建议使用实例指向,因为该block可以使用多次
// self.eventSink = events;
if(events) {
events(@"Content_title");
}
NSLog(@"接收到的值: %@",arguments);
return nil;
}
/// flutter不再接收
- (FlutterError*_Nullable)onCancelWithArguments:(id_Nullable)arguments {
// arguments flutter给native的参数
NSLog(@"%@", arguments);
return nil;
}
Flutter代码:
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
class ADList extends StatefulWidget {
@override
_State createState() =>_State();
}
// ignore: must_be_immutable
class _State extends State{
// 注册一个通知
static const EventChannel eventChannel =const EventChannel('com.xxx.xx/Page');
@override
void initState() {
super.initState();
print('init state 初始化方法');
// 监听事件,同时发送回传参数666 Flutter端 EventChannel监听Native传来的消息
eventChannel.receiveBroadcastStream(666).listen(_onEvent,onError: _onError);
}
String naviTitles ='titles' ;
// 回调事件
void _onEvent(Object event) {
setState(() {
naviTitles =event.toString();
print(event.toString());
});
}
// 错误返回
void _onError(Object error) {
}