flutter与原生通信
2022-12-05 本文已影响0人
今年27
1.FlutterMethodChannel 通过method invocation一次通信
flutter这边
class _MyAppState extends State<MyApp> {
final MethodChannel _oneChannel = MethodChannel('one_page');
final MethodChannel _towChannel = MethodChannel('tow_page');
String _pageIndex;
@override
void initState() {
super.initState();
_oneChannel.setMethodCallHandler((call) {
setState(() {
_pageIndex = call.method;
});
return null;
});
_towChannel.setMethodCallHandler((call) {
setState(() {
_pageIndex = call.method;
});
return null;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: _rootPage(_pageIndex),
);
}
Widget _rootPage(String pageIndex) {
switch (pageIndex) {
case 'one':
return Scaffold(
appBar: AppBar(
title: Text(pageIndex),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () {
_oneChannel.invokeMapMethod('exit');
},
child: Text(pageIndex),
),
],
),
);
case 'tow':
return Scaffold(
appBar: AppBar(
title: Text(pageIndex),
),
body: Center(
child: RaisedButton(
onPressed: () {
_towChannel.invokeMapMethod('exit');
},
child: Text(pageIndex),
)),
);
default:
return Scaffold(
appBar: AppBar(
title: Text("default"),
),
body: Center(
child: RaisedButton(
onPressed: () {
MethodChannel('default_page').invokeMapMethod('exit');
},
child: Text(pageIndex),
)),
);
}
}
}
在原生
- (IBAction)pushFlutter:(id)sender {
//告诉Flutter显示one_page
FlutterMethodChannel * methodChannel = [FlutterMethodChannel methodChannelWithName:@"one_page" binaryMessenger:self.flutterVc];
[methodChannel invokeMethod:@"one" arguments:nil];
self.flutterVc.modalPresentationStyle = UIModalPresentationFullScreen;
//弹出Flutter页面!
[self presentViewController:self.flutterVc animated:YES completion:nil];
//监听Flutter页面的回调
__weak typeof(self) weakSelf = self;
[methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
//如果是要我退出
if ([call.method isEqualToString:@"exit"]) {
[weakSelf.flutterVc dismissViewControllerAnimated:YES completion:nil];
}
}];
}
- (IBAction)pushFlutterTow:(id)sender {
//告诉Flutter显示one_page
FlutterMethodChannel * methodChannel = [FlutterMethodChannel methodChannelWithName:@"tow_page" binaryMessenger:self.flutterVc];
[methodChannel invokeMethod:@"tow" arguments:nil];
self.flutterVc.modalPresentationStyle = UIModalPresentationFullScreen;
//弹出Flutter页面!
[self presentViewController:self.flutterVc animated:YES completion:nil];
//监听Flutter页面的回调
__weak typeof(self) weakSelf = self;
[methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult _Nonnull result) {
//如果是要我退出
if ([call.method isEqualToString:@"exit"]) {
[weakSelf.flutterVc dismissViewControllerAnimated:YES completion:nil];
}
}];
}
这样就可以在原生界面完美启动不同的flutter页面了
以下两种都是属于持续通信
2.FlutterBasicMessageChannel 用于传递字符&半结构化信息
flutter这边
final BasicMessageChannel _messageChannel = BasicMessageChannel('messageChannel', StandardMessageCodec())
_messageChannel.setMessageHandler((message) {
print('收到来自iOS的:$message');
return null;
});
_messageChannel.send("hello");//发送给原生的消息
在原生那边
FlutterBasicMessageChannel* msgChannel = [FlutterBasicMessageChannel messageChannelWithName:@"messageChannel" binaryMessenger:self.flutterVC];
[msgChannel setMessageHanlder:^(id _Nullable message, FlutterReply _Nonnull callback) {
NSLog(@"收到的Flutter的:%@", message);
}];
[msgChannel sendMessage:@"hello flutter"];//发送给flutter消息
3.FlutterEventChannel 用于传输数据流(stream)