Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1415 lines
44 KiB

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'dart:math';
  4. import 'dart:typed_data';
  5. import 'package:cached_network_image/cached_network_image.dart';
  6. import 'package:chat/chat/download_item.dart';
  7. import 'package:chat/chat/file_msg_item.dart';
  8. import 'package:chat/chat/msg_state_widge.dart';
  9. import 'package:chat/chat/place_item.dart';
  10. import 'package:chat/chat/redbag_widget.dart';
  11. import 'package:chat/chat/upload_item.dart';
  12. import 'package:chat/chat/video_view.dart';
  13. import 'package:chat/data/UserData.dart';
  14. import 'package:chat/data/constants.dart';
  15. import 'package:chat/utils/friend_list_mgr.dart';
  16. import 'package:chat/utils/wpop/w_popup_menu.dart';
  17. import 'package:flutter/services.dart';
  18. import 'package:chat/generated/i18n.dart';
  19. import 'package:chat/home/invite_detail_page.dart';
  20. import 'package:chat/models/ChatMsg.dart';
  21. import 'package:chat/models/group_info_model.dart';
  22. import 'package:chat/models/keyboard_provider.dart';
  23. import 'package:chat/proto/chat.pbenum.dart';
  24. import 'package:chat/proto/chat.pbserver.dart';
  25. import 'package:chat/utils/CustomUI.dart';
  26. import 'package:chat/utils/MessageMgr.dart';
  27. import 'package:chat/utils/app_navigator.dart';
  28. import 'package:chat/utils/date_utils.dart';
  29. import 'package:chat/utils/group_member_model.dart';
  30. import 'package:chat/utils/msgHandler.dart';
  31. import 'package:chat/utils/screen.dart';
  32. import 'package:chat/utils/sound_util.dart';
  33. import 'package:chat/utils/upload_util.dart';
  34. import 'package:chat/utils/video_anim.dart';
  35. import 'package:dio/dio.dart';
  36. import 'package:flutter/cupertino.dart';
  37. import 'package:flutter/material.dart';
  38. import 'package:path_provider/path_provider.dart';
  39. import 'package:provider/provider.dart';
  40. import 'package:oktoast/oktoast.dart';
  41. import 'package:chat/utils/HttpUtil.dart';
  42. import 'package:share_extend/share_extend.dart';
  43. import 'package:url_launcher/url_launcher.dart';
  44. import '../r.dart';
  45. import '../utils/file_cache_mgr.dart';
  46. import 'full_img_view.dart';
  47. import 'upload_item.dart';
  48. import 'package:chat/models/ref_name_provider.dart';
  49. import 'package:chat/models/money_change.dart';
  50. import 'package:chat/models/voucher_change.dart';
  51. const double ChatRadius = 7.5;
  52. const Color SendMsgBg = Color(0xFFD4F0FF);
  53. const Color SendMsgText = Colors.black;
  54. const double TextHeight = 1.2;
  55. const double PaddingLeft = 9.5;
  56. const Color ReciveBorderColor = Color(0xFFDCDCDC);
  57. const double FontSize = 15;
  58. class GroupChatPageItem extends StatefulWidget {
  59. final MsgModel msg;
  60. final int lastMsgTime;
  61. final GroupMemberModel memberModel;
  62. final Function hideKeyboard;
  63. const GroupChatPageItem(
  64. {Key key,
  65. this.msg,
  66. this.lastMsgTime,
  67. this.hideKeyboard,
  68. this.memberModel})
  69. : assert(msg != null),
  70. super(key: key);
  71. @override
  72. _GroupChatPageItemState createState() => _GroupChatPageItemState();
  73. }
  74. class _GroupChatPageItemState extends State<GroupChatPageItem>
  75. with SingleTickerProviderStateMixin {
  76. int curTextType = 0; //文字、译文切换索引
  77. List<String> textList = [];
  78. String curSoundUrl;
  79. CancelToken _cancelToken = CancelToken();
  80. bool isLongPressed = false;
  81. @override
  82. void initState() {
  83. super.initState();
  84. if (widget.msg.from == UserData().basicInfo.userId &&
  85. widget.msg.state == MsgState.None) {
  86. print('重新发送消息');
  87. MsgHandler.sendChatMsg(widget.msg);
  88. }
  89. textList = widget.msg.getTransTextList();
  90. MessageMgr().on('Update Translate Message', updateTranslateMsg);
  91. MessageMgr().on('Cancel Request', _deleteItem);
  92. }
  93. @override
  94. void dispose() {
  95. MessageMgr().off('Cancel Request', _deleteItem);
  96. MessageMgr().off('Update Translate Message', updateTranslateMsg);
  97. super.dispose();
  98. }
  99. _deleteItem(msg) {
  100. if (msg == widget.msg) {
  101. print(widget.msg.state);
  102. if (widget.msg.state == MsgState.Uploading) {
  103. print('取消上传');
  104. UploadUtil().cancelRequests(_cancelToken);
  105. }
  106. }
  107. }
  108. updateTextList() {
  109. textList.clear();
  110. curTextType = 0;
  111. textList = widget.msg.getTransTextList();
  112. }
  113. updateTranslateMsg(msg) {
  114. if (msg.time == widget.msg.time) {
  115. if (mounted) {
  116. updateTextList();
  117. if (!mounted) {
  118. return;
  119. }
  120. setState(() {
  121. print('更新翻译文字');
  122. });
  123. }
  124. }
  125. }
  126. getTodayTime(msgDate) {
  127. String showTimeStr;
  128. var sendTime = widget.msg.time;
  129. var today = DateTime.now();
  130. //今天
  131. if (msgDate.year == today.year &&
  132. msgDate.month == today.month &&
  133. msgDate.day == today.day) {
  134. showTimeStr =
  135. DateUtils().getFormartData(timeSamp: sendTime, format: 'HH:mm');
  136. } else {
  137. showTimeStr = DateUtils()
  138. .getFormartData(timeSamp: sendTime, format: 'yyyy/MM/dd HH:mm');
  139. }
  140. return showTimeStr;
  141. }
  142. String getMsgTime() {
  143. String showTimeStr;
  144. var sendTime = widget.msg.time;
  145. var msgDate = DateTime.fromMillisecondsSinceEpoch(sendTime);
  146. if (widget.lastMsgTime == null) {
  147. showTimeStr = getTodayTime(msgDate);
  148. } else {
  149. if (sendTime - widget.lastMsgTime > 3 * 60 * 1000) {
  150. showTimeStr = getTodayTime(msgDate);
  151. }
  152. }
  153. return showTimeStr;
  154. }
  155. @override
  156. Widget build(BuildContext context) {
  157. var showTime = getMsgTime();
  158. return Container(
  159. width: Screen.width,
  160. margin: const EdgeInsets.symmetric(vertical: 10.0),
  161. child: Column(
  162. children: <Widget>[
  163. showTime == null
  164. ? SizedBox()
  165. : Container(
  166. decoration: BoxDecoration(
  167. color: Color(0xFF2C2F36).withOpacity(0.25),
  168. borderRadius: BorderRadius.all(Radius.circular(9.5))),
  169. padding:
  170. EdgeInsets.only(left: 7, right: 7, top: 3, bottom: 2),
  171. child: Text(showTime,
  172. textScaleFactor: 1.0,
  173. style: TextStyle(fontSize: 10.0, color: Colors.white)),
  174. ),
  175. SizedBox(height: 10),
  176. _msgWidget()
  177. ],
  178. ),
  179. );
  180. }
  181. _msgWidget() {
  182. if (widget.msg.from == 0) {
  183. return _serverNotifyMsg();
  184. } else {
  185. if (widget.msg.from == UserData().basicInfo.userId) {
  186. return _getSentMessageLayout(context);
  187. } else {
  188. return _getReceivedMessageLayout(context);
  189. }
  190. }
  191. }
  192. _serverNotifyMsg() {
  193. var type = widget.msg.msgType;
  194. if (type == ChatType.GroupChatNoticeType.value) {
  195. var res = GroupChatNotice.fromBuffer(widget.msg.msgContent);
  196. var groupInfoModel = Provider.of<GroupInfoModel>(context);
  197. var optId = res.operatuId;
  198. var showStr = MsgHandler.getGroupNoticeMsg(res, groupInfoModel);
  199. return Container(
  200. alignment: Alignment.center,
  201. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  202. child: extendedText(showStr,
  203. color: Constants.GreyTextColor,
  204. fontSize: 12, onSpecialTextTap: (dynamic parameter) {
  205. // print('点击事件 $parameter');
  206. if (parameter.startsWith("\$")) {
  207. // showToast('点击: ${res.operateduId[0]}');
  208. List<GroupMemberModel> list = [];
  209. for (int k = 0; k < res.operateduId.length; k++) {
  210. list.add(GroupMemberModel.fromBaseInfo(res.operateduId[k]));
  211. }
  212. AppNavigator.push(
  213. context,
  214. InviteDetailPage(
  215. originalList: list,
  216. opt: GroupMemberModel.fromBaseInfo(optId),
  217. groupId: groupInfoModel.sessionId,
  218. ));
  219. }
  220. }),
  221. );
  222. }
  223. return Container();
  224. }
  225. _textGif(List<int> msgContent) {
  226. var msg = utf8.decode(msgContent);
  227. return Container(
  228. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  229. child: extendedText(
  230. msg,
  231. selectionEnabled: false,
  232. hideKeyboard: widget.hideKeyboard,
  233. fontSize: FontSize,
  234. color: Colors.black,
  235. ),
  236. );
  237. }
  238. _textMsg(MsgModel msgModel) {
  239. var msg = utf8.decode(msgModel.msgContent);
  240. if (msg.contains('[ ')) {
  241. msg = msg.replaceAll('[ ', '[');
  242. }
  243. if (msg.contains(' ]')) {
  244. msg = msg.replaceAll(' ]', ']');
  245. }
  246. bool isUrl =false;
  247. if(textList[curTextType].contains('http') ){
  248. isUrl =true;
  249. }
  250. Widget text = Container(
  251. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  252. child: extendedText(
  253. msg,
  254. hideKeyboard: widget.hideKeyboard,
  255. fontSize: FontSize,
  256. color: isUrl?Colors.blue:SendMsgText,
  257. ),
  258. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  259. decoration: BoxDecoration(
  260. color: isLongPressed ? Colors.grey[300] : SendMsgBg,
  261. border: Border.all(color: Color(0xFFB9CBD7), width: 0.6),
  262. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  263. );
  264. if (msgModel.refMsgContent != null && msgModel.refMsgContent.length > 0) {
  265. QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msgModel.refMsgContent);
  266. return Column(
  267. mainAxisSize: MainAxisSize.min,
  268. crossAxisAlignment: CrossAxisAlignment.end,
  269. children: <Widget>[
  270. text,
  271. SizedBox(height: 2),
  272. Container(
  273. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  274. padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3),
  275. child: Text(
  276. quoteMsg.content,
  277. maxLines: 1,
  278. textScaleFactor: 1.0,
  279. overflow: TextOverflow.ellipsis,
  280. style: TextStyle(fontSize: 12),
  281. ),
  282. decoration: BoxDecoration(
  283. color: Colors.grey[300],
  284. borderRadius: BorderRadius.circular(5)),
  285. )
  286. ],
  287. );
  288. } else {
  289. return text;
  290. }
  291. }
  292. _soundMsg() {
  293. double time = widget.msg.extraInfo / 1000;
  294. if (time > 60) {
  295. time = 60.0;
  296. }
  297. bool isPlaying = false;
  298. var soundPath = widget.msg.localFile;
  299. isPlaying = SoundUtils().isPlaying(soundPath);
  300. var soundWidget = GestureDetector(
  301. child: Container(
  302. width: 120,
  303. child: Row(
  304. mainAxisAlignment: MainAxisAlignment.end,
  305. children: <Widget>[
  306. Container(
  307. alignment: Alignment.center,
  308. padding: EdgeInsets.only(bottom: 2),
  309. margin: EdgeInsets.only(right: 10),
  310. width: 25.5,
  311. height: 25.5,
  312. decoration: BoxDecoration(
  313. border:
  314. Border.all(color: const Color(0xFF1B92C7), width: 0.5),
  315. color: const Color(0xFF04A4FE),
  316. shape: BoxShape.circle),
  317. child: Icon(
  318. IconData(isPlaying ? 0xe652 : 0xe653,
  319. fontFamily: Constants.IconFontFamily),
  320. size: isPlaying ? 15 : 18,
  321. color: Colors.white,
  322. ),
  323. ),
  324. isPlaying
  325. ? Stack(
  326. children: <Widget>[
  327. Container(
  328. height: 18,
  329. width: 19,
  330. ),
  331. Positioned(
  332. bottom: 0,
  333. child: VideoAnim(
  334. begin: 18,
  335. start: 0.444,
  336. end: 4.5,
  337. )),
  338. Positioned(
  339. left: 7,
  340. bottom: 0,
  341. child: VideoAnim(
  342. begin: 4.5,
  343. end: 18,
  344. )),
  345. Positioned(
  346. left: 14,
  347. bottom: 0,
  348. child: VideoAnim(
  349. begin: 18,
  350. end: 4.5,
  351. ))
  352. ],
  353. )
  354. : Stack(
  355. children: <Widget>[
  356. Container(
  357. height: 18,
  358. width: 19,
  359. ),
  360. Positioned(
  361. bottom: 0, child: CustomUI.buildAudioContaniner(12)),
  362. Positioned(
  363. left: 7,
  364. bottom: 0,
  365. child: CustomUI.buildAudioContaniner(4.5)),
  366. Positioned(
  367. left: 14,
  368. bottom: 0,
  369. child: CustomUI.buildAudioContaniner(18))
  370. ],
  371. ),
  372. Expanded(child: SizedBox()),
  373. fixedText(time.toStringAsFixed(0), color: SendMsgText, fontSize: 16)
  374. ],
  375. ),
  376. padding: EdgeInsets.symmetric(horizontal: 15, vertical: 12),
  377. decoration: BoxDecoration(
  378. color: SendMsgBg,
  379. border: Border.all(color: Color(0xFFB9CBD7), width: 0.6),
  380. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  381. ),
  382. onTap: () async {
  383. print('播放状态 : $isPlaying');
  384. print('当前文件$soundPath ');
  385. if (isPlaying) {
  386. SoundUtils().pause();
  387. } else {
  388. SoundUtils().play(soundPath, onPlayed: () {
  389. if (mounted) {
  390. setState(() {});
  391. }
  392. }, complete: () {
  393. if (mounted) {
  394. setState(() {});
  395. }
  396. });
  397. }
  398. },
  399. );
  400. return UploadImgItem(
  401. msg: widget.msg,
  402. child: soundWidget,
  403. isShowProgress: false,
  404. );
  405. }
  406. Size _getImgSize() {
  407. double aspectRatio = widget.msg.extraInfo / 100;
  408. var maxWidth = Screen.width * 0.65;
  409. var maxHeight = Screen.height / 4;
  410. var width, height;
  411. if (maxWidth / maxHeight > aspectRatio) {
  412. height = maxHeight;
  413. width = maxHeight * aspectRatio;
  414. } else {
  415. width = maxWidth;
  416. height = maxWidth / aspectRatio;
  417. }
  418. return Size(width, height);
  419. }
  420. _imgMsg(List<int> imgData) {
  421. var imgSize = _getImgSize();
  422. return GestureDetector(
  423. child: ClipRRect(
  424. child: UploadImgItem(
  425. msg: widget.msg,
  426. cancelToken: _cancelToken,
  427. child: Container(
  428. height: imgSize.height,
  429. width: imgSize.width,
  430. child: Image(
  431. fit: BoxFit.contain,
  432. image: MemoryImage(Uint8List.fromList(imgData)),
  433. ),
  434. )),
  435. borderRadius: BorderRadius.circular(5),
  436. ),
  437. onTap: () async {
  438. showFullImg(context, widget.msg);
  439. });
  440. }
  441. _videoMsg(BuildContext context, List<int> thumbnail) {
  442. var imgSize = _getImgSize();
  443. return InkWell(
  444. child: ClipRRect(
  445. child: Stack(
  446. alignment: Alignment.center,
  447. children: <Widget>[
  448. UploadImgItem(
  449. msg: widget.msg,
  450. cancelToken: _cancelToken,
  451. child: Container(
  452. width: imgSize.width,
  453. height: imgSize.height,
  454. child: Image(
  455. fit: BoxFit.contain,
  456. image: MemoryImage(Uint8List.fromList(thumbnail)),
  457. ),
  458. ))
  459. ],
  460. ),
  461. borderRadius: BorderRadius.circular(5),
  462. ),
  463. onTap: () {
  464. showVideoPage(context, widget.msg.localFile);
  465. },
  466. );
  467. }
  468. _msgLayout(BuildContext context, MsgModel msg) {
  469. Widget item;
  470. switch (ChatType.valueOf(msg.msgType)) {
  471. case ChatType.TextChatType:
  472. item = _textMsg(msg);
  473. break;
  474. case ChatType.EmoticonType:
  475. item = _textGif(msg.msgContent);
  476. break;
  477. case ChatType.ImageChatType:
  478. item = _imgMsg(msg.msgContent);
  479. break;
  480. case ChatType.ShortVideoChatType:
  481. item = _videoMsg(context, msg.msgContent);
  482. break;
  483. case ChatType.ShortVoiceChatType:
  484. item = _soundMsg();
  485. break;
  486. case ChatType.RedWalletChatType:
  487. item = RedBagItem(
  488. Key(msg.time.toString()), UserData().basicInfo.userId, msg);
  489. break;
  490. case ChatType.PlaceChatType:
  491. item = PlaceItem(isMe: true, placeContent: msg.msgContent);
  492. break;
  493. case ChatType.FileChatType:
  494. item = _fileMsgItem();
  495. break;
  496. default:
  497. }
  498. return wrapItemWithMenu(item);
  499. }
  500. Widget _fileMsgItem() {
  501. return UploadImgItem(
  502. msg: widget.msg,
  503. cancelToken: _cancelToken,
  504. child: Container(
  505. height: 100,
  506. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  507. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  508. decoration: BoxDecoration(
  509. color: isLongPressed ? Colors.grey[300] : SendMsgBg,
  510. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  511. child: FileMsgItem(widget.msg)));
  512. }
  513. Widget wrapItemWithMenu(item) {
  514. List<Function> actionsFunc = [];
  515. List<String> actions = [
  516. I18n.of(context).reply,
  517. I18n.of(context).forward,
  518. I18n.of(context).delete,
  519. ];
  520. actionsFunc.add(() {
  521. print('发送引用的消息');
  522. MessageMgr().emit('Reply Select Message', widget.msg);
  523. });
  524. actionsFunc.add(() {
  525. print('转发消息');
  526. AppNavigator.pushForwardPage(context, widget.msg);
  527. });
  528. actionsFunc.add(() {
  529. MessageMgr().emit('Delete Select Message', widget.msg);
  530. });
  531. if (widget.msg.msgType == ChatType.FileChatType.value &&widget.msg.localFile!=null) {
  532. //分享文件
  533. actions.add('保存文件');
  534. actionsFunc.add(() async{
  535. String path = widget.msg.localFile;
  536. String type='file';
  537. if(path.contains('mp4') ||path.contains('mp3')){
  538. type = 'video';
  539. }else if(path.contains('png')||path.contains('jpg')||path.contains('jpeg')||path.contains('JPG')||path.contains('PNG')){
  540. type = 'image';
  541. }
  542. ShareExtend.share(FileCacheMgr.replacePath(path), type);
  543. });
  544. }
  545. if (widget.msg.msgType == ChatType.TextChatType.value) {
  546. actions.insert(0, I18n.of(context).copy);
  547. actionsFunc.insert(0, () {
  548. //复制当前的文字
  549. print('复制文字 ${textList[curTextType]}');
  550. ClipboardData clipboardData =
  551. ClipboardData(text: textList[curTextType]);
  552. Clipboard.setData(clipboardData);
  553. });
  554. }
  555. //如果是文件增加复制下载地址
  556. if (widget.msg.msgType == ChatType.FileChatType.value) {
  557. actions.insert(0, I18n.of(context).copy_download_url);
  558. actionsFunc.insert(0, () {
  559. UploadUtil().copyFileUrl(widget.msg, context);
  560. });
  561. }
  562. if (widget.msg.msgType == ChatType.ShortVoiceChatType.value) {
  563. var soundPlayMode =
  564. Provider.of<KeyboardIndexProvider>(context).soundPlayMode;
  565. actions.add(soundPlayMode
  566. ? I18n.of(context).handset_playback
  567. : I18n.of(context).speaker_play);
  568. actionsFunc.add(() {
  569. Provider.of<KeyboardIndexProvider>(context)
  570. .changeSoundPlayMode(!soundPlayMode);
  571. SoundUtils.instance.savePlayModeConfig(soundPlayMode);
  572. });
  573. }
  574. // String date2 = DateTime.fromMillisecondsSinceEpoch(widget.msg.time).toString();
  575. bool isUrl =false;
  576. if(widget.msg.msgType==ChatType.TextChatType.value ){
  577. if( textList[curTextType].contains('http')){
  578. isUrl =true;
  579. }
  580. }
  581. return WPopupMenu(
  582. child: item,
  583. actions: actions,
  584. onTap: ()async{
  585. if(isUrl){
  586. if (await canLaunch(textList[curTextType])) {
  587. launch(textList[curTextType]);
  588. }
  589. }
  590. },
  591. onLongPressStart: () {
  592. isLongPressed = true;
  593. setState(() {});
  594. },
  595. onLongPressEnd: () {
  596. isLongPressed = false;
  597. setState(() {});
  598. },
  599. onValueChanged: (int value) {
  600. print('选择的是$value个菜单');
  601. if (value >= 0 && value < actionsFunc.length) {
  602. actionsFunc[value]();
  603. }
  604. },
  605. );
  606. }
  607. Widget _getSentMessageLayout(BuildContext context) {
  608. bool hasHeadImg = true;
  609. if (UserData().basicInfo.headimgurl == null ||
  610. UserData().basicInfo.headimgurl.length == 0) {
  611. hasHeadImg = false;
  612. }
  613. return Row(
  614. crossAxisAlignment: CrossAxisAlignment.start,
  615. mainAxisAlignment: MainAxisAlignment.end,
  616. children: <Widget>[
  617. MsgStateWidget(widget.msg),
  618. SizedBox(width: 3),
  619. _msgLayout(context, widget.msg),
  620. SizedBox(width: 10),
  621. Column(
  622. crossAxisAlignment: CrossAxisAlignment.end,
  623. children: <Widget>[
  624. ClipRRect(
  625. borderRadius: BorderRadius.circular(8),
  626. child: hasHeadImg
  627. ? CachedNetworkImage(
  628. imageUrl: UserData().basicInfo.headimgurl,
  629. placeholder: (context, url) => Image.asset(
  630. Constants.DefaultHeadImgUrl,
  631. width: 40,
  632. height: 40,
  633. ),
  634. width: 40,
  635. height: 40,
  636. )
  637. : SizedBox(
  638. width: 40,
  639. height: 40,
  640. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  641. ],
  642. )
  643. ]);
  644. }
  645. _receiveJIF(MsgModel msg) {
  646. var text = utf8.decode(msg.msgContent);
  647. return Container(
  648. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  649. child: extendedText(text,
  650. selectionEnabled: false, hideKeyboard: widget.hideKeyboard));
  651. }
  652. double _getTextWidth(String text) {
  653. var tp = TextPainter(
  654. text: TextSpan(
  655. style: TextStyle(fontSize: FontSize), text: textList[curTextType]),
  656. textAlign: TextAlign.left,
  657. textDirection: TextDirection.ltr,
  658. textScaleFactor: 1,
  659. );
  660. tp.layout(maxWidth: Screen.width - 140);
  661. if (text.contains('[')) {
  662. if (text.length < 5 && text.startsWith('[') && text.endsWith(']')) {
  663. ///单表情
  664. return 25;
  665. }
  666. int length = text.split('[').length;
  667. if (length > 1) {
  668. ///包含表情
  669. int scale = 6;
  670. if (length > 6) scale = 7;
  671. double width = tp.width + length * scale;
  672. return width > (Screen.width - 140) ? Screen.width - 140 : width;
  673. }
  674. }
  675. return tp.width;
  676. ///单文字
  677. }
  678. _receiveText(MsgModel msg) {
  679. List<Widget> showMsg = [];
  680. if (textList.length > 0) {
  681. bool isUrl =false;
  682. if(textList[curTextType].contains('http') ){
  683. isUrl =true;
  684. }
  685. showMsg.add(Container(
  686. constraints:
  687. BoxConstraints(maxWidth: Screen.width - 140, minHeight: 22),
  688. alignment: Alignment.centerLeft,
  689. child: extendedText(
  690. textList[curTextType],
  691. color: isUrl?Colors.blue:Constants.BlackTextColor,
  692. hideKeyboard: widget.hideKeyboard,
  693. fontSize: FontSize,
  694. )));
  695. }
  696. var width = _getTextWidth(textList[curTextType]);
  697. var minWidth = width + 20;
  698. if (msg.transTag != 0) {
  699. minWidth = 200;
  700. showMsg.add(Padding(
  701. padding: EdgeInsets.symmetric(vertical: 5),
  702. child: Divider(color: Color(0xFFECECEC), height: 1)));
  703. Widget tranWidget = _transProcessWidget(msg.transTag);
  704. showMsg.add(tranWidget);
  705. }
  706. Widget text = Container(
  707. width: width + 20,
  708. constraints:
  709. BoxConstraints(maxWidth: Screen.width - 120, minWidth: minWidth),
  710. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  711. child: Column(
  712. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  713. decoration: BoxDecoration(
  714. border: Border.all(color: ReciveBorderColor, width: 0.5),
  715. color: isLongPressed ? Colors.grey[300] : Colors.white,
  716. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  717. );
  718. if (msg.refMsgContent != null && msg.refMsgContent.length > 0) {
  719. QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msg.refMsgContent);
  720. return Column(
  721. mainAxisSize: MainAxisSize.min,
  722. crossAxisAlignment: CrossAxisAlignment.start,
  723. children: <Widget>[
  724. text,
  725. SizedBox(height: 2),
  726. Container(
  727. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  728. padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3),
  729. child: Text(
  730. quoteMsg.content,
  731. maxLines: 1,
  732. textScaleFactor: 1.0,
  733. overflow: TextOverflow.ellipsis,
  734. style: TextStyle(fontSize: 12),
  735. ),
  736. decoration: BoxDecoration(
  737. color: Colors.grey[300],
  738. borderRadius: BorderRadius.circular(5)),
  739. )
  740. ],
  741. );
  742. } else {
  743. return text;
  744. }
  745. }
  746. //用户评价人工翻译,差评
  747. rateTranslateResult() async {
  748. return await HttpUtil().rateTranslateResult(widget.msg, () {
  749. if (mounted) {
  750. setState(() {});
  751. }
  752. });
  753. }
  754. _translateItemWidget(int code, String title, Function onTap) {
  755. Color color = onTap == null ? Constants.GreyTextColor : Color(0xFF087FF3);
  756. return InkWell(
  757. onTap: onTap,
  758. splashColor: Colors.red,
  759. child: Container(
  760. width: 80,
  761. child: Row(
  762. children: <Widget>[
  763. Icon(
  764. IconData(code, fontFamily: Constants.IconFontFamily),
  765. color: color,
  766. size: 20,
  767. ),
  768. SizedBox(width: 5),
  769. Expanded(
  770. child: SizedBox(
  771. child: Text(
  772. title,
  773. style: TextStyle(color: color, fontSize: 10),
  774. textScaleFactor: 1.0,
  775. maxLines: 1,
  776. overflow: TextOverflow.ellipsis,
  777. ),
  778. ))
  779. ],
  780. )));
  781. }
  782. bool isTranslating = false;
  783. _transProcessWidget(int transTag) {
  784. double width = 160;
  785. Widget userTranslateWidget;
  786. Widget machineTranslateWidget;
  787. if (transTag == 1) {
  788. //机器翻译中
  789. userTranslateWidget =
  790. _translateItemWidget(0xe670, I18n.of(context).man_retranslate, null);
  791. machineTranslateWidget =
  792. _translateItemWidget(0xe671, I18n.of(context).robotTranslate, null);
  793. } else if (transTag == 2) {
  794. //人工翻译中
  795. userTranslateWidget =
  796. _translateItemWidget(0xe670, I18n.of(context).ManTranslate, null);
  797. machineTranslateWidget = _translateItemWidget(
  798. 0xe671,
  799. I18n.of(context).robot_retranslate,
  800. textList.length <= 1
  801. ? null
  802. : () {
  803. setState(() {
  804. curTextType += 1;
  805. curTextType %= textList.length;
  806. });
  807. });
  808. } else if (transTag == 3) {
  809. //机器翻译完成
  810. userTranslateWidget = _translateItemWidget(
  811. 0xe670,
  812. I18n.of(context).man_retranslate,
  813. isTranslating
  814. ? null
  815. : () async {
  816. isTranslating = true;
  817. int money =
  818. Provider.of<MoneyChangeProvider>(context, listen: false)
  819. .money;
  820. int voucher =
  821. Provider.of<VoucherChangeProvider>(context, listen: false)
  822. .voucher;
  823. int needMoney = widget.msg.getNeedMoney();
  824. if (needMoney > voucher + money) {
  825. showToast('翻译券和H币不足');
  826. return;
  827. }
  828. var res = await HttpUtil().getPersonalTranslate(widget.msg);
  829. if (res) {
  830. print('请求人工翻译成功,进行扣费');
  831. setState(() {
  832. widget.msg.transTag = 2;
  833. //优先扣券
  834. if (voucher > 0) {
  835. int costQuan = min(voucher, needMoney);
  836. Provider.of<VoucherChangeProvider>(context,
  837. listen: false)
  838. .subVoucher(costQuan);
  839. }
  840. //不足的话再扣H币
  841. if (needMoney > voucher) {
  842. Provider.of<MoneyChangeProvider>(context, listen: false)
  843. .subMoney(needMoney - voucher);
  844. }
  845. });
  846. }
  847. });
  848. machineTranslateWidget = _translateItemWidget(
  849. 0xe671,
  850. I18n.of(context).robot_retranslate,
  851. textList.length <= 1
  852. ? null
  853. : () {
  854. setState(() {
  855. curTextType += 1;
  856. curTextType %= textList.length;
  857. });
  858. });
  859. } else if (transTag == 4 || transTag == 10) {
  860. //4人工翻译完成,未评论 10人工翻译完成已评论
  861. userTranslateWidget = InkWell(
  862. onTap: () {
  863. setState(() {
  864. curTextType = 0;
  865. });
  866. },
  867. child: Container(
  868. width: width / 2,
  869. child: Row(
  870. children: <Widget>[
  871. InkWell(
  872. child: Icon(
  873. IconData(0xe641, fontFamily: Constants.IconFontFamily),
  874. size: 18,
  875. color:
  876. transTag == 10 ? Colors.grey : Color(0xFF087FF3)),
  877. onTap: transTag == 10
  878. ? null
  879. : () {
  880. CustomUI.showIosDialog(
  881. context, I18n.of(context).bad_ev, () async {
  882. bool isSuccess = await rateTranslateResult();
  883. if (isSuccess) {
  884. Navigator.of(context).pop(true);
  885. showToast(I18n.of(context).success);
  886. } else {
  887. showToast(I18n.of(context).fail);
  888. Navigator.of(context).pop();
  889. }
  890. }, () {
  891. Navigator.of(context).pop();
  892. });
  893. },
  894. ),
  895. SizedBox(width: 5),
  896. Expanded(
  897. child: SizedBox(
  898. child: Text(
  899. I18n.of(context).over,
  900. style: TextStyle(color: Color(0xFF087FF3), fontSize: 10),
  901. textScaleFactor: 1.0,
  902. maxLines: 1,
  903. overflow: TextOverflow.ellipsis,
  904. ),
  905. ))
  906. ],
  907. )));
  908. machineTranslateWidget =
  909. _translateItemWidget(0xe675, I18n.of(context).see_original, () {
  910. setState(() {
  911. curTextType = textList.length - 1;
  912. });
  913. });
  914. }
  915. return Container(
  916. height: 26,
  917. alignment: Alignment.center,
  918. child: Row(
  919. children: <Widget>[
  920. userTranslateWidget,
  921. Expanded(
  922. child: SizedBox(child: VerticalDivider(), height: 20, width: 2)),
  923. machineTranslateWidget
  924. ],
  925. ),
  926. );
  927. }
  928. _receiveImg(BuildContext context, List<int> imgData, {String downloadData}) {
  929. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  930. var imgSize = _getImgSize();
  931. return GestureDetector(
  932. child: Container(
  933. alignment: Alignment.centerLeft,
  934. width: imgSize.width,
  935. height: imgSize.height,
  936. child: ClipRRect(
  937. child: Image(
  938. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  939. ),
  940. borderRadius: BorderRadius.circular(5),
  941. )),
  942. onTap: () async {
  943. widget.hideKeyboard();
  944. showFullImg(context, widget.msg);
  945. });
  946. }
  947. _receiveVideo(BuildContext context, List<int> imgData,
  948. {String downloadData}) {
  949. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  950. var imgSize = _getImgSize();
  951. return InkWell(
  952. onTap: () {
  953. if (widget.msg.localFile != null) {
  954. widget.hideKeyboard();
  955. showVideoPage(context, widget.msg.localFile);
  956. }
  957. },
  958. child: DownloadItem(
  959. isAutoDown: false,
  960. msg: widget.msg,
  961. child: Container(
  962. width: imgSize.width,
  963. height: imgSize.height,
  964. child: ClipRRect(
  965. child: Image(
  966. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  967. ),
  968. borderRadius: BorderRadius.circular(5),
  969. ),
  970. ),
  971. ));
  972. }
  973. showVideoPage(BuildContext context, String filePath) {
  974. Navigator.push(context,
  975. MaterialPageRoute<void>(builder: (BuildContext context) {
  976. return VideoPage(videoPath: filePath);
  977. }));
  978. }
  979. _receiveSound(BuildContext context, List<int> soundData) {
  980. print(' build 收到的语音消息');
  981. MsgModel msg = widget.msg;
  982. var time = widget.msg.extraInfo / 1000;
  983. if (time > 60) {
  984. time = 60.0;
  985. }
  986. bool isPlaying = false;
  987. if (curSoundUrl != null) {
  988. isPlaying = SoundUtils().isPlaying(curSoundUrl);
  989. }
  990. var soundWidget = InkWell(
  991. onTap: () async {
  992. bool isLocal = true;
  993. if (msg.localFile != null) {
  994. isLocal = true;
  995. curSoundUrl = msg.localFile;
  996. } else {
  997. isLocal = false;
  998. var sessionId = msg.sessionId;
  999. curSoundUrl = UploadUtil()
  1000. .getFullUrl(msg.extraFile, sessionId, msg.channelType);
  1001. }
  1002. print('当前文件$curSoundUrl 本地?$isLocal');
  1003. if (isPlaying) {
  1004. await SoundUtils().pause();
  1005. } else {
  1006. print('开始播放');
  1007. if (widget.msg.soundListened == 0) {
  1008. widget.msg.updateSoundListened();
  1009. }
  1010. await SoundUtils().play(curSoundUrl, isLocal: isLocal, onPlayed: () {
  1011. if (mounted) {
  1012. this.setState(() {});
  1013. }
  1014. }, complete: () {
  1015. if (mounted) {
  1016. this.setState(() {});
  1017. }
  1018. });
  1019. }
  1020. },
  1021. child: Container(
  1022. width: 130,
  1023. child: Row(children: <Widget>[
  1024. Container(
  1025. alignment: Alignment.center,
  1026. padding: EdgeInsets.only(bottom: 2),
  1027. margin: EdgeInsets.only(right: 10),
  1028. width: 25.5,
  1029. height: 25.5,
  1030. decoration: BoxDecoration(
  1031. border:
  1032. Border.all(color: const Color(0xFF1B92C7), width: 0.5),
  1033. color: const Color(0xFF04A4FE),
  1034. shape: BoxShape.circle),
  1035. child: Icon(
  1036. IconData(isPlaying ? 0xe652 : 0xe653,
  1037. fontFamily: Constants.IconFontFamily),
  1038. size: isPlaying ? 15 : 18,
  1039. color: Colors.white,
  1040. ),
  1041. ),
  1042. isPlaying
  1043. ? Stack(
  1044. children: <Widget>[
  1045. Container(
  1046. height: 18,
  1047. width: 19,
  1048. ),
  1049. Positioned(
  1050. bottom: 0,
  1051. child: VideoAnim(
  1052. begin: 18,
  1053. start: 0.444,
  1054. end: 4.5,
  1055. )),
  1056. Positioned(
  1057. left: 7,
  1058. bottom: 0,
  1059. child: VideoAnim(
  1060. begin: 4.5,
  1061. end: 18,
  1062. )),
  1063. Positioned(
  1064. left: 14,
  1065. bottom: 0,
  1066. child: VideoAnim(
  1067. begin: 18,
  1068. end: 4.5,
  1069. ))
  1070. ],
  1071. )
  1072. : Stack(
  1073. children: <Widget>[
  1074. Container(
  1075. height: 18,
  1076. width: 19,
  1077. ),
  1078. Positioned(
  1079. bottom: 0, child: CustomUI.buildAudioContaniner(12)),
  1080. Positioned(
  1081. left: 7,
  1082. bottom: 0,
  1083. child: CustomUI.buildAudioContaniner(4.5)),
  1084. Positioned(
  1085. left: 14,
  1086. bottom: 0,
  1087. child: CustomUI.buildAudioContaniner(18))
  1088. ],
  1089. ),
  1090. Expanded(child: SizedBox()),
  1091. fixedText(time.toStringAsFixed(0),
  1092. color: Constants.BlackTextColor, fontSize: 16)
  1093. ])),
  1094. );
  1095. List<Widget> showMsg = [];
  1096. showMsg.add(DownloadItem(
  1097. msg: widget.msg,
  1098. child: soundWidget,
  1099. isShowProgress: false,
  1100. ));
  1101. double width = 130;
  1102. double minWidth = 0;
  1103. if (textList.length > 0) {
  1104. width = _getTextWidth(textList[curTextType]) + 20;
  1105. width = min(width, Screen.width - 120);
  1106. minWidth = max(width, 150);
  1107. showMsg.add(Padding(
  1108. padding: EdgeInsets.symmetric(vertical: 5),
  1109. child: Divider(
  1110. height: 1,
  1111. )));
  1112. showMsg.add(Container(
  1113. child: extendedText(
  1114. textList[curTextType],
  1115. color: Constants.BlackTextColor,
  1116. hideKeyboard: widget.hideKeyboard,
  1117. fontSize: FontSize,
  1118. ),
  1119. alignment: Alignment.centerLeft,
  1120. constraints:
  1121. BoxConstraints(maxWidth: Screen.width - 120, minHeight: 22),
  1122. ));
  1123. }
  1124. if (msg.transTag != 0) {
  1125. minWidth = 200;
  1126. showMsg.add(Divider(color: Color(0xFFECECEC), height: 3));
  1127. Widget tranWidget = _transProcessWidget(msg.transTag);
  1128. showMsg.add(tranWidget);
  1129. }
  1130. return Container(
  1131. width: width + 20,
  1132. constraints:
  1133. msg.transTag != 0 ? BoxConstraints(minWidth: minWidth) : null,
  1134. child: Column(
  1135. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  1136. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1137. decoration: BoxDecoration(
  1138. color: Colors.white,
  1139. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1140. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1141. );
  1142. }
  1143. void showFullImg(BuildContext context, MsgModel msg) {
  1144. print('显示图片');
  1145. Navigator.push(context,
  1146. MaterialPageRoute<void>(builder: (BuildContext context) {
  1147. return PhotoPage(msg: msg);
  1148. }));
  1149. }
  1150. _reveiveMsg(BuildContext context) {
  1151. Widget item;
  1152. switch (ChatType.valueOf(widget.msg.msgType)) {
  1153. case ChatType.TextChatType:
  1154. item = _receiveText(widget.msg);
  1155. break;
  1156. case ChatType.EmoticonType:
  1157. item = _receiveJIF(widget.msg);
  1158. break;
  1159. case ChatType.ImageChatType:
  1160. if (widget.msg.extraFile != null) {
  1161. item = _receiveImg(context, widget.msg.msgContent,
  1162. downloadData: widget.msg.extraFile);
  1163. } else {
  1164. item = _receiveImg(context, widget.msg.msgContent);
  1165. }
  1166. break;
  1167. case ChatType.ShortVideoChatType:
  1168. item = _receiveVideo(context, widget.msg.msgContent,
  1169. downloadData: widget.msg.extraFile);
  1170. break;
  1171. case ChatType.ShortVoiceChatType:
  1172. item = _receiveSound(context, widget.msg.msgContent);
  1173. break;
  1174. case ChatType.PlaceChatType:
  1175. item = PlaceItem(isMe: false, placeContent: widget.msg.msgContent);
  1176. break;
  1177. case ChatType.FileChatType:
  1178. item = _receiveFileMsgItem();
  1179. break;
  1180. default:
  1181. }
  1182. return wrapItemWithMenu(item);
  1183. }
  1184. Widget _receiveFileMsgItem() {
  1185. return DownloadItem(
  1186. isAutoDown: false,
  1187. msg: widget.msg,
  1188. onComplete: () {
  1189. if (mounted) {
  1190. setState(() {});
  1191. }
  1192. },
  1193. child: Container(
  1194. height: 100,
  1195. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  1196. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1197. decoration: BoxDecoration(
  1198. color: Colors.white,
  1199. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1200. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1201. child: FileMsgItem(widget.msg)));
  1202. }
  1203. Widget _getReceivedMessageLayout(BuildContext context) {
  1204. bool hasHeadImg = true;
  1205. var memberModel = widget.memberModel;
  1206. if (memberModel == null) {
  1207. return Container();
  1208. }
  1209. if (memberModel.avtar == null || memberModel.avtar.length == 0) {
  1210. hasHeadImg = false;
  1211. }
  1212. GroupInfoModel infoModel = Provider.of<GroupInfoModel>(context);
  1213. var refName = Provider.of<RefNameProvider>(context)
  1214. .getGroupRefName(infoModel.sessionId, memberModel.memberId);
  1215. bool isShowSoundSate =
  1216. widget.msg.msgType == ChatType.ShortVoiceChatType.value &&
  1217. widget.msg.soundListened == 0;
  1218. return Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  1219. Container(
  1220. margin: const EdgeInsets.only(right: 8.0),
  1221. child: GestureDetector(
  1222. child: ClipRRect(
  1223. borderRadius: BorderRadius.circular(8),
  1224. child: hasHeadImg
  1225. ? CachedNetworkImage(
  1226. imageUrl: memberModel.avtar,
  1227. placeholder: (context, url) => Image.asset(
  1228. Constants.DefaultHeadImgUrl,
  1229. width: 40,
  1230. height: 40,
  1231. ),
  1232. width: 40,
  1233. height: 40,
  1234. )
  1235. : SizedBox(
  1236. width: 40,
  1237. height: 40,
  1238. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  1239. onTap: () {
  1240. AppNavigator.pushProfileInfoPage(context, memberModel.memberId,
  1241. fromWhere: 2,
  1242. addMode: !FriendListMgr().isMyFriend(memberModel.memberId)
  1243. ? 1
  1244. : 0);
  1245. },
  1246. onLongPress: () {
  1247. print('long press user');
  1248. MessageMgr().emit('Alter User Message', memberModel);
  1249. },
  1250. )),
  1251. infoModel.isShowName > 0
  1252. ? Column(
  1253. crossAxisAlignment: CrossAxisAlignment.start,
  1254. children: <Widget>[
  1255. Container(
  1256. padding: EdgeInsets.only(left: 9),
  1257. child: fixedText(refName, fontSize: 12, color: Colors.grey),
  1258. ),
  1259. SizedBox(height: 2),
  1260. _reveiveMsg(context),
  1261. ],
  1262. )
  1263. : _reveiveMsg(context),
  1264. isShowSoundSate
  1265. ? Container(
  1266. margin: EdgeInsets.only(
  1267. left: 8, top: infoModel.isShowName > 0 ? 38 : 20),
  1268. child: CircleAvatar(
  1269. radius: 3.5,
  1270. backgroundColor: Colors.red,
  1271. ))
  1272. : Container()
  1273. ]);
  1274. }
  1275. }