Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。
效果图:
详情请参考 Flutter高仿微信-第29篇-单聊 , 这里只是提取图片实现的部分代码。
实现代码:
//打开相册权限 void _openAblumPermission() async { bool isPhotosGranted = await Permission.photos.isGranted; bool isPhotosDenied = await Permission.photos.isDenied; if(isPhotosGranted){ _openAblum(); } else { if(isPhotosDenied){ _openAblum(); } else { //跳转到设置页面提示 _showPhotosConfirmationAlert(context); } } }
// 为正常拍摄,请前往设备中的【设置】> 【隐私】> 【相机】中允许无他相机使用 _showPhotosConfirmationAlert(BuildContext context) { showPlatformDialog( context: context, builder: (_) => BasicDialogAlert( title: Text("无法使用相册"), content: Text("为编辑照片,请前往设备中的【设置】> 【隐私】> 【照片】中允许${AppManager.getInstance().appName}使用"), actions:[ BasicDialogAction( title: Text("知道了"), onPressed: () { Navigator.pop(context); }, ), BasicDialogAction( title: Text("去设置"), onPressed: () { // 跳转到系统设置页 AppSettings.openAppSettings(); }, ), ], ), ); }
//打开相册 void _openAblum() { ListselectedAssets = []; AssetPicker.pickAssets( context, pickerConfig: AssetPickerConfig( maxAssets: 1, selectedAssets: selectedAssets, ), ).then((imageList) { if(imageList == null){ return; } imageList as List ; for(int i = 0; i < imageList.length; i++){ AssetEntity ae = imageList[i]; ae.file.then((file) async { String resultFilePath = file?.path??""; _processVideoAndPicture(resultFilePath); }); } }); }
//处理图片和小视频(相册、拍照) void _processVideoAndPicture(String resultFilePath) async { if(resultFilePath == null || "" == resultFilePath){ return; } String messageId = UUID.getUUID(); if(CommonUtils.isImage(resultFilePath)){ //压缩图片完成再发送 String compressImagePath = await CompressImageUtils.compressFile(fileName: resultFilePath); widget.sendMedialCallback(CommonUtils.CHAT_CONTENT_TYPE_IMG, compressImagePath,0 ,messageId); widget.refreshMediaCallback(CommonUtils.CHAT_CONTENT_TYPE_IMG, compressImagePath, "",0, messageId); }
}
接收到消息:
String serverImagePath = CommonUtils.BASE_URL_UPLOAD + content; String localImagePath = await FileUtils.getBaseFile("${DateUtil.getNowDateMs()}.jpg"); //先下载图片 await DownloadUtils.getInstance().downloadFile(serverImagePath, localImagePath); chatBean.imgPath = serverImagePath; chatBean.imgPathLocal = localImagePath;
//通知栏提示 NotificationUtils.getInstance().showNotification(chatBean); //插入本地数据库 ChatRepository.getInstance().insertChat(chatBean); //EventBus刷新页面 eventBus.emit(chatBean);