From 4caa092156f1378549ed6f5a9ed586a51a20c3e7 Mon Sep 17 00:00:00 2001 From: KerwinZheng <772112648@qq.com> Date: Tue, 31 Mar 2020 09:22:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=95=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E6=A0=B7=E5=BC=8F=E5=92=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E5=88=B0=E5=BC=95=E7=94=A8=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/chat/group_chat_item.dart | 328 +++++++++++++++++++++------------- lib/chat/group_chat_view.dart | 52 +++--- 2 files changed, 229 insertions(+), 151 deletions(-) diff --git a/lib/chat/group_chat_item.dart b/lib/chat/group_chat_item.dart index d629302..d548976 100644 --- a/lib/chat/group_chat_item.dart +++ b/lib/chat/group_chat_item.dart @@ -92,6 +92,8 @@ class _GroupChatPageItemState extends State bool isLongPressed = false; + double maxWidth = Screen.width - 140; + @override void initState() { super.initState(); @@ -277,6 +279,8 @@ class _GroupChatPageItemState extends State } _textMsg(MsgModel msgModel) { + List showMsg = []; + var width = 0.0; var msg = utf8.decode(msgModel.msgContent); if (msg.contains('[ ')) { msg = msg.replaceAll('[ ', '['); @@ -284,53 +288,89 @@ class _GroupChatPageItemState extends State if (msg.contains(' ]')) { msg = msg.replaceAll(' ]', ']'); } - bool isUrl =false; - if(textList[curTextType].contains('http') ){ - isUrl =true; + bool isUrl = false; + if (textList[curTextType].contains('http')) { + isUrl = true; } - Widget text = Container( + + if (msgModel.refMsgContent != null && msgModel.refMsgContent.length > 0) { + QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msgModel.refMsgContent); + + var refName = Provider.of(context) + .getGroupRefName(msgModel.sessionId, quoteMsg.sendUserId); + + var msgDate = + DateTime.fromMillisecondsSinceEpoch(quoteMsg.sendTime.toInt()); + var showTimeStr = getTodayTime(msgDate); + + var timetWidth = _getRealTextWidht(showTimeStr, fontSize: 13); + var nameWidhth = _getRealTextWidht(refName, fontSize: 13); + width = timetWidth + (nameWidhth > 70 ? 70 : nameWidhth) + 36; + if (width > maxWidth) { + width = maxWidth; + } + showMsg.add(InkWell( + onTap: () { + MessageMgr().emit('Jump to Msg', quoteMsg.sendTime.toInt()); + }, + child: Container( + padding: EdgeInsets.all(8), + margin: EdgeInsets.only(bottom: 5), + decoration: BoxDecoration( + color: Color(0xFFC4E0F5), + borderRadius: BorderRadius.circular(5.5)), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row(children: [ + Container( + constraints: BoxConstraints(maxWidth: 70), + child: Text( + refName, + style: TextStyle(fontSize: 13), + overflow: TextOverflow.ellipsis, + )), + SizedBox(width: 20), + Text( + showTimeStr, + style: TextStyle(fontSize: 13), + ) + ]), + SizedBox(height: 7.5), + Text( + quoteMsg.content.substring(quoteMsg.content.indexOf(':') + 2), + style: TextStyle(fontSize: 13), + ) + ], + ), + ))); + } + var contentWidth = _getTextWidth(textList[curTextType]); + if (contentWidth > width && contentWidth <= maxWidth) { + width = contentWidth; + } + + showMsg.add(Container( + constraints: BoxConstraints(maxWidth: maxWidth, minHeight: 22), + alignment: Alignment.centerLeft, + child: extendedText( + textList[curTextType], + color: isUrl ? Colors.blue : Constants.BlackTextColor, + hideKeyboard: widget.hideKeyboard, + fontSize: FontSize, + ))); + + return Container( constraints: BoxConstraints(maxWidth: Screen.width - 120), - child: extendedText( - msg, - hideKeyboard: widget.hideKeyboard, - fontSize: FontSize, - color: isUrl?Colors.blue:SendMsgText, - ), + width: width + 20, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, children: showMsg), padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5), decoration: BoxDecoration( color: isLongPressed ? Colors.grey[300] : SendMsgBg, border: Border.all(color: Color(0xFFB9CBD7), width: 0.6), borderRadius: BorderRadius.all(Radius.circular(ChatRadius))), ); - - if (msgModel.refMsgContent != null && msgModel.refMsgContent.length > 0) { - QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msgModel.refMsgContent); - - return Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - text, - SizedBox(height: 2), - Container( - constraints: BoxConstraints(maxWidth: Screen.width - 120), - padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3), - child: Text( - quoteMsg.content, - maxLines: 1, - textScaleFactor: 1.0, - overflow: TextOverflow.ellipsis, - style: TextStyle(fontSize: 12), - ), - decoration: BoxDecoration( - color: Colors.grey[300], - borderRadius: BorderRadius.circular(5)), - ) - ], - ); - } else { - return text; - } } _soundMsg() { @@ -472,7 +512,9 @@ class _GroupChatPageItemState extends State _imgMsg(List imgData) { var imgSize = _getImgSize(); - ImageProvider provider = MemoryImage(widget.msg.localFile==null?Uint8List.fromList(imgData):File(widget.msg.localFile).readAsBytesSync()); + ImageProvider provider = MemoryImage(widget.msg.localFile == null + ? Uint8List.fromList(imgData) + : File(widget.msg.localFile).readAsBytesSync()); return GestureDetector( child: ClipRRect( @@ -484,7 +526,7 @@ class _GroupChatPageItemState extends State width: imgSize.width, child: Image( fit: BoxFit.contain, - image:provider, + image: provider, ), )), borderRadius: BorderRadius.circular(5), @@ -586,46 +628,46 @@ class _GroupChatPageItemState extends State MessageMgr().emit('Delete Select Message', widget.msg); }); - ///转发 - if (widget.msg.msgType == ChatType.TextChatType.value|| - widget.msg.msgType == ChatType.ImageChatType.value|| - widget.msg.msgType == ChatType.ShortVideoChatType.value|| - widget.msg.msgType == ChatType.PlaceChatType.value|| - widget.msg.msgType == ChatType.EmoticonType.value|| - widget.msg.msgType == ChatType.FileChatType.value - ){ - actions.add( I18n.of(context).forward); + if (widget.msg.msgType == ChatType.TextChatType.value || + widget.msg.msgType == ChatType.ImageChatType.value || + widget.msg.msgType == ChatType.ShortVideoChatType.value || + widget.msg.msgType == ChatType.PlaceChatType.value || + widget.msg.msgType == ChatType.EmoticonType.value || + widget.msg.msgType == ChatType.FileChatType.value) { + actions.add(I18n.of(context).forward); actionsFunc.add(() { print('转发消息'); - if(widget.msg.msgType == ChatType.FileChatType.value && widget.msg.localFile==null){ + if (widget.msg.msgType == ChatType.FileChatType.value && + widget.msg.localFile == null) { showToast('请先下载文件'); - return ; + return; } AppNavigator.pushForwardPage(context, widget.msg); }); } - - if (widget.msg.msgType == ChatType.FileChatType.value &&widget.msg.localFile!=null) { + if (widget.msg.msgType == ChatType.FileChatType.value && + widget.msg.localFile != null) { //分享文件 - actions.add( I18n.of(context).copy_download_url); - actionsFunc.add(() async{ + actions.add(I18n.of(context).copy_download_url); + actionsFunc.add(() async { //如果是文件增加复制下载地址 - String path = widget.msg.localFile; - UploadUtil().copyFileUrl(widget.msg, context); - String type='file'; - if(path.contains('mp4') ||path.contains('mp3')){ - type = 'video'; - }else if(path.contains('png')||path.contains('jpg')||path.contains('jpeg')||path.contains('JPG')||path.contains('PNG')){ - type = 'image'; - } - - ShareExtend.share(FileCacheMgr.replacePath(path), type); - + String path = widget.msg.localFile; + UploadUtil().copyFileUrl(widget.msg, context); + String type = 'file'; + if (path.contains('mp4') || path.contains('mp3')) { + type = 'video'; + } else if (path.contains('png') || + path.contains('jpg') || + path.contains('jpeg') || + path.contains('JPG') || + path.contains('PNG')) { + type = 'image'; + } + ShareExtend.share(FileCacheMgr.replacePath(path), type); }); - } if (widget.msg.msgType == ChatType.TextChatType.value) { @@ -639,8 +681,6 @@ class _GroupChatPageItemState extends State }); } - - if (widget.msg.msgType == ChatType.ShortVoiceChatType.value) { var soundPlayMode = Provider.of(context).soundPlayMode; @@ -656,23 +696,22 @@ class _GroupChatPageItemState extends State } // String date2 = DateTime.fromMillisecondsSinceEpoch(widget.msg.time).toString(); - bool isUrl =false; - if(widget.msg.msgType==ChatType.TextChatType.value ){ - if( textList[curTextType].contains('http')){ - isUrl =true; + bool isUrl = false; + if (widget.msg.msgType == ChatType.TextChatType.value) { + if (textList[curTextType].contains('http')) { + isUrl = true; } - } return WPopupMenu( child: item, actions: actions, - onTap: ()async{ - if(isUrl){ - if (await canLaunch(textList[curTextType])) { - launch(textList[curTextType]); + onTap: () async { + if (isUrl) { + if (await canLaunch(textList[curTextType])) { + launch(textList[curTextType]); + } } - } }, onLongPressStart: () { isLongPressed = true; @@ -739,16 +778,27 @@ class _GroupChatPageItemState extends State selectionEnabled: false, hideKeyboard: widget.hideKeyboard)); } - double _getTextWidth(String text) { + double _getRealTextWidht(String text, {double fontSize = FontSize}) { + TextPainter _textPainter = TextPainter( + maxLines: 2, + text: TextSpan( + text: text, + style: TextStyle(fontSize: fontSize, color: Colors.black)), + textDirection: TextDirection.ltr) + ..layout(); + return _textPainter.width; + } + + double _getTextWidth(String text, {double fontSize = FontSize}) { var tp = TextPainter( text: TextSpan( - style: TextStyle(fontSize: FontSize), text: textList[curTextType]), + style: TextStyle(fontSize: fontSize), text: textList[curTextType]), textAlign: TextAlign.left, textDirection: TextDirection.ltr, textScaleFactor: 1, ); - tp.layout(maxWidth: Screen.width - 140); + tp.layout(maxWidth: maxWidth); if (text.contains('[')) { if (text.length < 5 && text.startsWith('[') && text.endsWith(']')) { ///单表情 @@ -763,7 +813,7 @@ class _GroupChatPageItemState extends State if (length > 6) scale = 7; double width = tp.width + length * scale; - return width > (Screen.width - 140) ? Screen.width - 140 : width; + return width > (maxWidth) ? maxWidth : width; } } return tp.width; @@ -785,10 +835,70 @@ class _GroupChatPageItemState extends State _receiveText(MsgModel msg) { List showMsg = []; + double width = 0.0; if (textList.length > 0) { - bool isUrl =false; - if(textList[curTextType].contains('http') ){ - isUrl =true; + bool isUrl = false; + if (textList[curTextType].contains('http')) { + isUrl = true; + } + + if (msg.refMsgContent != null && msg.refMsgContent.length > 0) { + QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msg.refMsgContent); + + var refName = Provider.of(context) + .getGroupRefName(msg.sessionId, quoteMsg.sendUserId); + + var msgDate = + DateTime.fromMillisecondsSinceEpoch(quoteMsg.sendTime.toInt()); + var showTimeStr = getTodayTime(msgDate); + + var timetWidth = _getRealTextWidht(showTimeStr, fontSize: 13); + var nameWidhth = _getRealTextWidht(refName, fontSize: 13); + width = timetWidth + (nameWidhth > 70 ? 70 : nameWidhth) + 36; + if (width > maxWidth) { + width = maxWidth; + } + showMsg.add(InkWell( + onTap: () { + MessageMgr().emit('Jump to Msg', quoteMsg.sendTime.toInt()); + }, + child: Container( + padding: EdgeInsets.all(8), + margin: EdgeInsets.only(bottom: 5), + decoration: BoxDecoration( + color: Color(0xFFC4E0F5), + borderRadius: BorderRadius.circular(5.5)), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Row(children: [ + Container( + constraints: BoxConstraints(maxWidth: 70), + child: Text( + refName, + style: TextStyle(fontSize: 13), + overflow: TextOverflow.ellipsis, + )), + SizedBox(width: 20), + Text( + showTimeStr, + style: TextStyle(fontSize: 13), + ) + ]), + SizedBox(height: 7.5), + Text( + quoteMsg.content + .substring(quoteMsg.content.indexOf(':') + 2), + style: TextStyle(fontSize: 13), + ) + ], + ), + ))); + } + + var contentWidth = _getTextWidth(textList[curTextType]); + if (contentWidth > width && contentWidth <= maxWidth) { + width = contentWidth; } showMsg.add(InkWell( @@ -805,8 +915,7 @@ class _GroupChatPageItemState extends State } }, child: Container( - constraints: - BoxConstraints(maxWidth: Screen.width - 140, minHeight: 22), + constraints: BoxConstraints(maxWidth: maxWidth, minHeight: 22), alignment: Alignment.centerLeft, child: extendedText( textList[curTextType], @@ -816,8 +925,6 @@ class _GroupChatPageItemState extends State )))); } - var width = _getTextWidth(textList[curTextType]); - var minWidth = width + 20; if (msg.transTag != 0) { minWidth = 200; @@ -841,7 +948,7 @@ class _GroupChatPageItemState extends State color: isLongPressed ? Colors.grey[300] : Colors.white, borderRadius: BorderRadius.all(Radius.circular(ChatRadius))), ), - msg.transTag != 1 && msg.transTag !=0 + msg.transTag != 1 && msg.transTag != 0 ? Positioned( right: 5, top: 5, @@ -855,34 +962,7 @@ class _GroupChatPageItemState extends State : Container() ]); - if (msg.refMsgContent != null && msg.refMsgContent.length > 0) { - QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msg.refMsgContent); - - return Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - text, - SizedBox(height: 2), - Container( - constraints: BoxConstraints(maxWidth: Screen.width - 120), - padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3), - child: Text( - quoteMsg.content, - maxLines: 1, - textScaleFactor: 1.0, - overflow: TextOverflow.ellipsis, - style: TextStyle(fontSize: 12), - ), - decoration: BoxDecoration( - color: Colors.grey[300], - borderRadius: BorderRadius.circular(5)), - ) - ], - ); - } else { - return text; - } + return text; } //用户评价人工翻译,差评 @@ -1083,14 +1163,16 @@ class _GroupChatPageItemState extends State _receiveImg(BuildContext context, List imgData, {String downloadData}) { // ImageProvider provider = MemoryImage(Uint8List.fromList(imgData)); - ImageProvider provider = MemoryImage(widget.msg.localFile==null?Uint8List.fromList(imgData):File(widget.msg.localFile).readAsBytesSync()); + ImageProvider provider = MemoryImage(widget.msg.localFile == null + ? Uint8List.fromList(imgData) + : File(widget.msg.localFile).readAsBytesSync()); var imgSize = _getImgSize(); return DownloadItem( isAutoDown: false, msg: widget.msg, - onFinishTap: (){ + onFinishTap: () { widget.hideKeyboard(); showFullImg(context, widget.msg); }, @@ -1106,7 +1188,6 @@ class _GroupChatPageItemState extends State ), ), ); - } _receiveVideo(BuildContext context, List imgData, @@ -1116,7 +1197,6 @@ class _GroupChatPageItemState extends State return InkWell( onTap: () { if (widget.msg.localFile != null) { - showVideoPage(context, widget.msg.localFile); } }, diff --git a/lib/chat/group_chat_view.dart b/lib/chat/group_chat_view.dart index 8d6a1ff..ac3288e 100644 --- a/lib/chat/group_chat_view.dart +++ b/lib/chat/group_chat_view.dart @@ -84,6 +84,7 @@ class _GroupChatPageState extends State { msgMgr.off('Keyboard Hide', dealWithKeyboardHide); msgMgr.off('Update Group Info', updateGroupInfo); msgMgr.off('Delete Select Message', _deleteItem); + msgMgr.off('Jump to Msg', jumpToMsg); MsgHandler.curActiveSession = 0; SoundUtils().stop(); @@ -126,6 +127,7 @@ class _GroupChatPageState extends State { msgMgr.on('Keyboard Hide', dealWithKeyboardHide); msgMgr.on('Update Group Info', updateGroupInfo); msgMgr.on('Delete Select Message', _deleteItem); + msgMgr.on('Jump to Msg', jumpToMsg); WidgetsBinding.instance.addPostFrameCallback((_) { if (widget.enterType == 1) { @@ -157,6 +159,24 @@ class _GroupChatPageState extends State { }); } + jumpToMsg(time) { + var screenItemNums = itemPositionListener.itemPositions.value.length; + int jumIndex = 0; + for (int i = 0; i < msgList.length; i++) { + if (time == msgList[i].time) { + jumIndex = i - screenItemNums + 2; + break; + } + } + if (jumIndex < 0) { + jumIndex = 0; + } + if (jumIndex > msgList.length - 1) { + jumIndex = msgList.length - 1; + } + itemScrollController.jumpTo(index: jumIndex); + } + void _sendFile(File file) async { // File file = await FilePicker.getFile(); int fileSize = file.lengthSync(); @@ -253,7 +273,7 @@ class _GroupChatPageState extends State { Map refMap = Provider.of(context).refMap; - bool isHaveUnreadNews = unreadTime !=null; + bool isHaveUnreadNews = unreadTime != null; bool isAlterYou = alterTime != null; return MultiProvider( @@ -301,31 +321,9 @@ class _GroupChatPageState extends State { right: 0, child: InkWell( onTap: () { - var screenItemNums = - itemPositionListener - .itemPositions.value.length; - int jumIndex = 0; - if (isAlterYou) { - for (int i = 0; - i < msgList.length; - i++) { - if (alterTime == msgList[i].time) { - jumIndex = i - screenItemNums + 2; - break; - } - } - } else { - for (int i = 0; - i < msgList.length; - i++) { - if (unreadTime == msgList[i].time) { - jumIndex = i - screenItemNums + 2; - break; - } - } - } - itemScrollController.jumpTo( - index: jumIndex); + jumpToMsg(isAlterYou + ? alterTime + : unreadTime); unreadTime = null; setState(() {}); }, @@ -349,7 +347,7 @@ class _GroupChatPageState extends State { child: Row( children: [ Icon( - Icons.file_upload, + Icons.arrow_upward, color: Color(0xFF3875E9), size: 20, ),