Flutter--调用原生功能
Flutter工程调用原生功能--相册相机 demo
1 使用methodChannel
Flutter工程中创建MethodChannel
MethodChannel _methodChannel = MethodChannel('MethodChannelDemo'); //创建methodChannel需传入标识,一般可以使用类名来做标识methodChannel调用方法
_methodChannel.invokeMapMethod('chooseImage', 'photo'); //传入方法名, 参数原生工程中,监听FlutterMethodChannel
FlutterViewController *flutterVC = (FlutterViewController *)self.window.rootViewController;原生工程,imagePickerDelegate回调,通过invokeMethod回调Flutter
self.methodChannel = [FlutterMethodChannel methodChannelWithName:@"MethodChannelDemo" binaryMessenger:flutterVC];
UIImagePickerController *imageVC = [[UIImagePickerController alloc] init];
imageVC.delegate=self;
//监听flutter消息
[self.methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult_Nonnull result) {
if([call.methodisEqualToString:@"chooseImage"]) {
if([call.argumentsisEqual:@"photo"]) {
imageVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
if([call.argumentsisEqual:@"camera"]) {
imageVC.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[flutterVCpresentViewController:imageVC animated:YES completion:nil];
}
}];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionaryFlutter工程,接收回调,更换图片*)info {
[pickerdismissViewControllerAnimated:YES completion:^{
NSString *imagePath = [NSString stringWithFormat:@"%@", info[@"UIImagePickerControllerImageURL"]];
[self.methodChannel invokeMethod:@"imagePath" arguments:imagePath];
}];
}
File_imageFile;
@override
void initState() {
// TODO: implement initState
super.initState();
_methodChannel.setMethodCallHandler((handler) {
if (handler.method =='imagePath') {
【Flutter--调用原生功能】String imagePath = handler.arguments.toString().substring(7);
setState(() {
_imageFile =File(imagePath);
});
}
return null;
});
}
Image(image: _imageFile ==null ? AssetImage('images/badge.png') : FileImage(_imageFile)),
2 使用image_picker 添加image_picker, Pug get,拉取包文件
文章图片
void chooseImage()async {
PickedFile file = awaitImagePicker().getImage(source: ImageSource.gallery);
setState(() {
_imageFile =File(file.path);
});
}
推荐阅读
- SpringBoot调用公共模块的自定义注解失效的解决
- thinkphp|thinkphp 3.2 如何调用第三方类库
- NeuVector 会是下一个爆款云原生安全神器吗()
- 原生家庭之痛与超越
- 分享!如何分分钟实现微信扫二维码调用外部浏览器打开指定页面的功能
- WKWebview|WKWebview js 调用oc 和oc调用js
- 自我探索之原生家庭1
- H5、js调用手机通话|H5、js调用手机通话,短信
- Swift高级应用|Swift高级应用 -01
- 云原生微服务技术趋势解读