Hibok
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1415 строки
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( I18n.of(context).copy_download_url);
  534. actionsFunc.add(() async{
  535. //如果是文件增加复制下载地址
  536. String path = widget.msg.localFile;
  537. UploadUtil().copyFileUrl(widget.msg, context);
  538. String type='file';
  539. if(path.contains('mp4') ||path.contains('mp3')){
  540. type = 'video';
  541. }else if(path.contains('png')||path.contains('jpg')||path.contains('jpeg')||path.contains('JPG')||path.contains('PNG')){
  542. type = 'image';
  543. }
  544. ShareExtend.share(FileCacheMgr.replacePath(path), type);
  545. });
  546. }
  547. if (widget.msg.msgType == ChatType.TextChatType.value) {
  548. actions.insert(0, I18n.of(context).copy);
  549. actionsFunc.insert(0, () {
  550. //复制当前的文字
  551. print('复制文字 ${textList[curTextType]}');
  552. ClipboardData clipboardData =
  553. ClipboardData(text: textList[curTextType]);
  554. Clipboard.setData(clipboardData);
  555. });
  556. }
  557. if (widget.msg.msgType == ChatType.ShortVoiceChatType.value) {
  558. var soundPlayMode =
  559. Provider.of<KeyboardIndexProvider>(context).soundPlayMode;
  560. actions.add(soundPlayMode
  561. ? I18n.of(context).handset_playback
  562. : I18n.of(context).speaker_play);
  563. actionsFunc.add(() {
  564. Provider.of<KeyboardIndexProvider>(context)
  565. .changeSoundPlayMode(!soundPlayMode);
  566. SoundUtils.instance.savePlayModeConfig(soundPlayMode);
  567. });
  568. }
  569. // String date2 = DateTime.fromMillisecondsSinceEpoch(widget.msg.time).toString();
  570. bool isUrl =false;
  571. if(widget.msg.msgType==ChatType.TextChatType.value ){
  572. if( textList[curTextType].contains('http')){
  573. isUrl =true;
  574. }
  575. }
  576. return WPopupMenu(
  577. child: item,
  578. actions: actions,
  579. onTap: ()async{
  580. if(isUrl){
  581. if (await canLaunch(textList[curTextType])) {
  582. launch(textList[curTextType]);
  583. }
  584. }
  585. },
  586. onLongPressStart: () {
  587. isLongPressed = true;
  588. setState(() {});
  589. },
  590. onLongPressEnd: () {
  591. isLongPressed = false;
  592. setState(() {});
  593. },
  594. onValueChanged: (int value) {
  595. print('选择的是$value个菜单');
  596. if (value >= 0 && value < actionsFunc.length) {
  597. actionsFunc[value]();
  598. }
  599. },
  600. );
  601. }
  602. Widget _getSentMessageLayout(BuildContext context) {
  603. bool hasHeadImg = true;
  604. if (UserData().basicInfo.headimgurl == null ||
  605. UserData().basicInfo.headimgurl.length == 0) {
  606. hasHeadImg = false;
  607. }
  608. return Row(
  609. crossAxisAlignment: CrossAxisAlignment.start,
  610. mainAxisAlignment: MainAxisAlignment.end,
  611. children: <Widget>[
  612. MsgStateWidget(widget.msg),
  613. SizedBox(width: 3),
  614. _msgLayout(context, widget.msg),
  615. SizedBox(width: 10),
  616. Column(
  617. crossAxisAlignment: CrossAxisAlignment.end,
  618. children: <Widget>[
  619. ClipRRect(
  620. borderRadius: BorderRadius.circular(8),
  621. child: hasHeadImg
  622. ? CachedNetworkImage(
  623. imageUrl: UserData().basicInfo.headimgurl,
  624. placeholder: (context, url) => Image.asset(
  625. Constants.DefaultHeadImgUrl,
  626. width: 40,
  627. height: 40,
  628. ),
  629. width: 40,
  630. height: 40,
  631. )
  632. : SizedBox(
  633. width: 40,
  634. height: 40,
  635. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  636. ],
  637. )
  638. ]);
  639. }
  640. _receiveJIF(MsgModel msg) {
  641. var text = utf8.decode(msg.msgContent);
  642. return Container(
  643. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  644. child: extendedText(text,
  645. selectionEnabled: false, hideKeyboard: widget.hideKeyboard));
  646. }
  647. double _getTextWidth(String text) {
  648. var tp = TextPainter(
  649. text: TextSpan(
  650. style: TextStyle(fontSize: FontSize), text: textList[curTextType]),
  651. textAlign: TextAlign.left,
  652. textDirection: TextDirection.ltr,
  653. textScaleFactor: 1,
  654. );
  655. tp.layout(maxWidth: Screen.width - 140);
  656. if (text.contains('[')) {
  657. if (text.length < 5 && text.startsWith('[') && text.endsWith(']')) {
  658. ///单表情
  659. return 25;
  660. }
  661. int length = text.split('[').length;
  662. if (length > 1) {
  663. ///包含表情
  664. int scale = 6;
  665. if (length > 6) scale = 7;
  666. double width = tp.width + length * scale;
  667. return width > (Screen.width - 140) ? Screen.width - 140 : width;
  668. }
  669. }
  670. return tp.width;
  671. ///单文字
  672. }
  673. _receiveText(MsgModel msg) {
  674. List<Widget> showMsg = [];
  675. if (textList.length > 0) {
  676. bool isUrl =false;
  677. if(textList[curTextType].contains('http') ){
  678. isUrl =true;
  679. }
  680. showMsg.add(Container(
  681. constraints:
  682. BoxConstraints(maxWidth: Screen.width - 140, minHeight: 22),
  683. alignment: Alignment.centerLeft,
  684. child: extendedText(
  685. textList[curTextType],
  686. color: isUrl?Colors.blue:Constants.BlackTextColor,
  687. hideKeyboard: widget.hideKeyboard,
  688. fontSize: FontSize,
  689. )));
  690. }
  691. var width = _getTextWidth(textList[curTextType]);
  692. var minWidth = width + 20;
  693. if (msg.transTag != 0) {
  694. minWidth = 200;
  695. showMsg.add(Padding(
  696. padding: EdgeInsets.symmetric(vertical: 5),
  697. child: Divider(color: Color(0xFFECECEC), height: 1)));
  698. Widget tranWidget = _transProcessWidget(msg.transTag);
  699. showMsg.add(tranWidget);
  700. }
  701. Widget text = Container(
  702. width: width + 20,
  703. constraints:
  704. BoxConstraints(maxWidth: Screen.width - 120, minWidth: minWidth),
  705. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  706. child: Column(
  707. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  708. decoration: BoxDecoration(
  709. border: Border.all(color: ReciveBorderColor, width: 0.5),
  710. color: isLongPressed ? Colors.grey[300] : Colors.white,
  711. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  712. );
  713. if (msg.refMsgContent != null && msg.refMsgContent.length > 0) {
  714. QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msg.refMsgContent);
  715. return Column(
  716. mainAxisSize: MainAxisSize.min,
  717. crossAxisAlignment: CrossAxisAlignment.start,
  718. children: <Widget>[
  719. text,
  720. SizedBox(height: 2),
  721. Container(
  722. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  723. padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3),
  724. child: Text(
  725. quoteMsg.content,
  726. maxLines: 1,
  727. textScaleFactor: 1.0,
  728. overflow: TextOverflow.ellipsis,
  729. style: TextStyle(fontSize: 12),
  730. ),
  731. decoration: BoxDecoration(
  732. color: Colors.grey[300],
  733. borderRadius: BorderRadius.circular(5)),
  734. )
  735. ],
  736. );
  737. } else {
  738. return text;
  739. }
  740. }
  741. //用户评价人工翻译,差评
  742. rateTranslateResult() async {
  743. return await HttpUtil().rateTranslateResult(widget.msg, () {
  744. if (mounted) {
  745. setState(() {});
  746. }
  747. });
  748. }
  749. _translateItemWidget(int code, String title, Function onTap) {
  750. Color color = onTap == null ? Constants.GreyTextColor : Color(0xFF087FF3);
  751. return InkWell(
  752. onTap: onTap,
  753. splashColor: Colors.red,
  754. child: Container(
  755. width: 80,
  756. child: Row(
  757. children: <Widget>[
  758. Icon(
  759. IconData(code, fontFamily: Constants.IconFontFamily),
  760. color: color,
  761. size: 20,
  762. ),
  763. SizedBox(width: 5),
  764. Expanded(
  765. child: SizedBox(
  766. child: Text(
  767. title,
  768. style: TextStyle(color: color, fontSize: 10),
  769. textScaleFactor: 1.0,
  770. maxLines: 1,
  771. overflow: TextOverflow.ellipsis,
  772. ),
  773. ))
  774. ],
  775. )));
  776. }
  777. bool isTranslating = false;
  778. _transProcessWidget(int transTag) {
  779. double width = 160;
  780. Widget userTranslateWidget;
  781. Widget machineTranslateWidget;
  782. if (transTag == 1) {
  783. //机器翻译中
  784. userTranslateWidget =
  785. _translateItemWidget(0xe670, I18n.of(context).man_retranslate, null);
  786. machineTranslateWidget =
  787. _translateItemWidget(0xe671, I18n.of(context).robotTranslate, null);
  788. } else if (transTag == 2) {
  789. //人工翻译中
  790. userTranslateWidget =
  791. _translateItemWidget(0xe670, I18n.of(context).ManTranslate, null);
  792. machineTranslateWidget = _translateItemWidget(
  793. 0xe671,
  794. I18n.of(context).robot_retranslate,
  795. textList.length <= 1
  796. ? null
  797. : () {
  798. setState(() {
  799. curTextType += 1;
  800. curTextType %= textList.length;
  801. });
  802. });
  803. } else if (transTag == 3) {
  804. //机器翻译完成
  805. userTranslateWidget = _translateItemWidget(
  806. 0xe670,
  807. I18n.of(context).man_retranslate,
  808. isTranslating
  809. ? null
  810. : () async {
  811. isTranslating = true;
  812. int money =
  813. Provider.of<MoneyChangeProvider>(context, listen: false)
  814. .money;
  815. int voucher =
  816. Provider.of<VoucherChangeProvider>(context, listen: false)
  817. .voucher;
  818. int needMoney = widget.msg.getNeedMoney();
  819. if (needMoney > voucher + money) {
  820. showToast('翻译券和H币不足');
  821. return;
  822. }
  823. var res = await HttpUtil().getPersonalTranslate(widget.msg);
  824. if (res) {
  825. print('请求人工翻译成功,进行扣费');
  826. setState(() {
  827. widget.msg.transTag = 2;
  828. //优先扣券
  829. if (voucher > 0) {
  830. int costQuan = min(voucher, needMoney);
  831. Provider.of<VoucherChangeProvider>(context,
  832. listen: false)
  833. .subVoucher(costQuan);
  834. }
  835. //不足的话再扣H币
  836. if (needMoney > voucher) {
  837. Provider.of<MoneyChangeProvider>(context, listen: false)
  838. .subMoney(needMoney - voucher);
  839. }
  840. });
  841. }
  842. });
  843. machineTranslateWidget = _translateItemWidget(
  844. 0xe671,
  845. I18n.of(context).robot_retranslate,
  846. textList.length <= 1
  847. ? null
  848. : () {
  849. setState(() {
  850. curTextType += 1;
  851. curTextType %= textList.length;
  852. });
  853. });
  854. } else if (transTag == 4 || transTag == 10) {
  855. //4人工翻译完成,未评论 10人工翻译完成已评论
  856. userTranslateWidget = InkWell(
  857. onTap: () {
  858. setState(() {
  859. curTextType = 0;
  860. });
  861. },
  862. child: Container(
  863. width: width / 2,
  864. child: Row(
  865. children: <Widget>[
  866. InkWell(
  867. child: Icon(
  868. IconData(0xe641, fontFamily: Constants.IconFontFamily),
  869. size: 18,
  870. color:
  871. transTag == 10 ? Colors.grey : Color(0xFF087FF3)),
  872. onTap: transTag == 10
  873. ? null
  874. : () {
  875. CustomUI.showIosDialog(
  876. context, I18n.of(context).bad_ev, () async {
  877. bool isSuccess = await rateTranslateResult();
  878. if (isSuccess) {
  879. Navigator.of(context).pop(true);
  880. showToast(I18n.of(context).success);
  881. } else {
  882. showToast(I18n.of(context).fail);
  883. Navigator.of(context).pop();
  884. }
  885. }, () {
  886. Navigator.of(context).pop();
  887. });
  888. },
  889. ),
  890. SizedBox(width: 5),
  891. Expanded(
  892. child: SizedBox(
  893. child: Text(
  894. I18n.of(context).over,
  895. style: TextStyle(color: Color(0xFF087FF3), fontSize: 10),
  896. textScaleFactor: 1.0,
  897. maxLines: 1,
  898. overflow: TextOverflow.ellipsis,
  899. ),
  900. ))
  901. ],
  902. )));
  903. machineTranslateWidget =
  904. _translateItemWidget(0xe675, I18n.of(context).see_original, () {
  905. setState(() {
  906. curTextType = textList.length - 1;
  907. });
  908. });
  909. }
  910. return Container(
  911. height: 26,
  912. alignment: Alignment.center,
  913. child: Row(
  914. children: <Widget>[
  915. userTranslateWidget,
  916. Expanded(
  917. child: SizedBox(child: VerticalDivider(), height: 20, width: 2)),
  918. machineTranslateWidget
  919. ],
  920. ),
  921. );
  922. }
  923. _receiveImg(BuildContext context, List<int> imgData, {String downloadData}) {
  924. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  925. var imgSize = _getImgSize();
  926. return DownloadItem(
  927. isAutoDown: false,
  928. msg: widget.msg,
  929. onFinishTap: (){
  930. widget.hideKeyboard();
  931. showFullImg(context, widget.msg);
  932. },
  933. child: Container(
  934. alignment: Alignment.centerLeft,
  935. width: imgSize.width,
  936. height: imgSize.height,
  937. child: ClipRRect(
  938. child: Image(
  939. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  940. ),
  941. borderRadius: BorderRadius.circular(5),
  942. ),
  943. ),
  944. );
  945. }
  946. _receiveVideo(BuildContext context, List<int> imgData,
  947. {String downloadData}) {
  948. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  949. var imgSize = _getImgSize();
  950. return InkWell(
  951. onTap: () {
  952. if (widget.msg.localFile != null) {
  953. widget.hideKeyboard();
  954. showVideoPage(context, widget.msg.localFile);
  955. }
  956. },
  957. child: DownloadItem(
  958. isAutoDown: false,
  959. msg: widget.msg,
  960. child: Container(
  961. width: imgSize.width,
  962. height: imgSize.height,
  963. child: ClipRRect(
  964. child: Image(
  965. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  966. ),
  967. borderRadius: BorderRadius.circular(5),
  968. ),
  969. ),
  970. ));
  971. }
  972. showVideoPage(BuildContext context, String filePath) {
  973. Navigator.push(context,
  974. MaterialPageRoute<void>(builder: (BuildContext context) {
  975. return VideoPage(videoPath: filePath);
  976. }));
  977. }
  978. _receiveSound(BuildContext context, List<int> soundData) {
  979. print(' build 收到的语音消息');
  980. MsgModel msg = widget.msg;
  981. var time = widget.msg.extraInfo / 1000;
  982. if (time > 60) {
  983. time = 60.0;
  984. }
  985. bool isPlaying = false;
  986. if (curSoundUrl != null) {
  987. isPlaying = SoundUtils().isPlaying(curSoundUrl);
  988. }
  989. var soundWidget = InkWell(
  990. onTap: () async {
  991. bool isLocal = true;
  992. if (msg.localFile != null) {
  993. isLocal = true;
  994. curSoundUrl = msg.localFile;
  995. } else {
  996. isLocal = false;
  997. var sessionId = msg.sessionId;
  998. curSoundUrl = UploadUtil()
  999. .getFullUrl(msg.extraFile, sessionId, msg.channelType);
  1000. }
  1001. print('当前文件$curSoundUrl 本地?$isLocal');
  1002. if (isPlaying) {
  1003. await SoundUtils().pause();
  1004. } else {
  1005. print('开始播放');
  1006. if (widget.msg.soundListened == 0) {
  1007. widget.msg.updateSoundListened();
  1008. }
  1009. await SoundUtils().play(curSoundUrl, isLocal: isLocal, onPlayed: () {
  1010. if (mounted) {
  1011. this.setState(() {});
  1012. }
  1013. }, complete: () {
  1014. if (mounted) {
  1015. this.setState(() {});
  1016. }
  1017. });
  1018. }
  1019. },
  1020. child: Container(
  1021. width: 130,
  1022. child: Row(children: <Widget>[
  1023. Container(
  1024. alignment: Alignment.center,
  1025. padding: EdgeInsets.only(bottom: 2),
  1026. margin: EdgeInsets.only(right: 10),
  1027. width: 25.5,
  1028. height: 25.5,
  1029. decoration: BoxDecoration(
  1030. border:
  1031. Border.all(color: const Color(0xFF1B92C7), width: 0.5),
  1032. color: const Color(0xFF04A4FE),
  1033. shape: BoxShape.circle),
  1034. child: Icon(
  1035. IconData(isPlaying ? 0xe652 : 0xe653,
  1036. fontFamily: Constants.IconFontFamily),
  1037. size: isPlaying ? 15 : 18,
  1038. color: Colors.white,
  1039. ),
  1040. ),
  1041. isPlaying
  1042. ? Stack(
  1043. children: <Widget>[
  1044. Container(
  1045. height: 18,
  1046. width: 19,
  1047. ),
  1048. Positioned(
  1049. bottom: 0,
  1050. child: VideoAnim(
  1051. begin: 18,
  1052. start: 0.444,
  1053. end: 4.5,
  1054. )),
  1055. Positioned(
  1056. left: 7,
  1057. bottom: 0,
  1058. child: VideoAnim(
  1059. begin: 4.5,
  1060. end: 18,
  1061. )),
  1062. Positioned(
  1063. left: 14,
  1064. bottom: 0,
  1065. child: VideoAnim(
  1066. begin: 18,
  1067. end: 4.5,
  1068. ))
  1069. ],
  1070. )
  1071. : Stack(
  1072. children: <Widget>[
  1073. Container(
  1074. height: 18,
  1075. width: 19,
  1076. ),
  1077. Positioned(
  1078. bottom: 0, child: CustomUI.buildAudioContaniner(12)),
  1079. Positioned(
  1080. left: 7,
  1081. bottom: 0,
  1082. child: CustomUI.buildAudioContaniner(4.5)),
  1083. Positioned(
  1084. left: 14,
  1085. bottom: 0,
  1086. child: CustomUI.buildAudioContaniner(18))
  1087. ],
  1088. ),
  1089. Expanded(child: SizedBox()),
  1090. fixedText(time.toStringAsFixed(0),
  1091. color: Constants.BlackTextColor, fontSize: 16)
  1092. ])),
  1093. );
  1094. List<Widget> showMsg = [];
  1095. showMsg.add(DownloadItem(
  1096. msg: widget.msg,
  1097. child: soundWidget,
  1098. isShowProgress: false,
  1099. ));
  1100. double width = 130;
  1101. double minWidth = 0;
  1102. if (textList.length > 0) {
  1103. width = _getTextWidth(textList[curTextType]) + 20;
  1104. width = min(width, Screen.width - 120);
  1105. minWidth = max(width, 150);
  1106. showMsg.add(Padding(
  1107. padding: EdgeInsets.symmetric(vertical: 5),
  1108. child: Divider(
  1109. height: 1,
  1110. )));
  1111. showMsg.add(Container(
  1112. child: extendedText(
  1113. textList[curTextType],
  1114. color: Constants.BlackTextColor,
  1115. hideKeyboard: widget.hideKeyboard,
  1116. fontSize: FontSize,
  1117. ),
  1118. alignment: Alignment.centerLeft,
  1119. constraints:
  1120. BoxConstraints(maxWidth: Screen.width - 120, minHeight: 22),
  1121. ));
  1122. }
  1123. if (msg.transTag != 0) {
  1124. minWidth = 200;
  1125. showMsg.add(Divider(color: Color(0xFFECECEC), height: 3));
  1126. Widget tranWidget = _transProcessWidget(msg.transTag);
  1127. showMsg.add(tranWidget);
  1128. }
  1129. return Container(
  1130. width: width + 20,
  1131. constraints:
  1132. msg.transTag != 0 ? BoxConstraints(minWidth: minWidth) : null,
  1133. child: Column(
  1134. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  1135. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1136. decoration: BoxDecoration(
  1137. color: Colors.white,
  1138. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1139. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1140. );
  1141. }
  1142. void showFullImg(BuildContext context, MsgModel msg) {
  1143. print('显示图片');
  1144. Navigator.push(context,
  1145. MaterialPageRoute<void>(builder: (BuildContext context) {
  1146. return PhotoPage(msg: msg);
  1147. }));
  1148. }
  1149. _reveiveMsg(BuildContext context) {
  1150. Widget item;
  1151. switch (ChatType.valueOf(widget.msg.msgType)) {
  1152. case ChatType.TextChatType:
  1153. item = _receiveText(widget.msg);
  1154. break;
  1155. case ChatType.EmoticonType:
  1156. item = _receiveJIF(widget.msg);
  1157. break;
  1158. case ChatType.ImageChatType:
  1159. if (widget.msg.extraFile != null) {
  1160. item = _receiveImg(context, widget.msg.msgContent,
  1161. downloadData: widget.msg.extraFile);
  1162. } else {
  1163. item = _receiveImg(context, widget.msg.msgContent);
  1164. }
  1165. break;
  1166. case ChatType.ShortVideoChatType:
  1167. item = _receiveVideo(context, widget.msg.msgContent,
  1168. downloadData: widget.msg.extraFile);
  1169. break;
  1170. case ChatType.ShortVoiceChatType:
  1171. item = _receiveSound(context, widget.msg.msgContent);
  1172. break;
  1173. case ChatType.PlaceChatType:
  1174. item = PlaceItem(isMe: false, placeContent: widget.msg.msgContent);
  1175. break;
  1176. case ChatType.FileChatType:
  1177. item = _receiveFileMsgItem();
  1178. break;
  1179. default:
  1180. }
  1181. return wrapItemWithMenu(item);
  1182. }
  1183. Widget _receiveFileMsgItem() {
  1184. return DownloadItem(
  1185. isAutoDown: false,
  1186. msg: widget.msg,
  1187. onComplete: () {
  1188. if (mounted) {
  1189. setState(() {});
  1190. }
  1191. },
  1192. child: Container(
  1193. height: 100,
  1194. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  1195. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1196. decoration: BoxDecoration(
  1197. color: Colors.white,
  1198. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1199. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1200. child: FileMsgItem(widget.msg)));
  1201. }
  1202. Widget _getReceivedMessageLayout(BuildContext context) {
  1203. bool hasHeadImg = true;
  1204. var memberModel = widget.memberModel;
  1205. if (memberModel == null) {
  1206. return Container();
  1207. }
  1208. if (memberModel.avtar == null || memberModel.avtar.length == 0) {
  1209. hasHeadImg = false;
  1210. }
  1211. GroupInfoModel infoModel = Provider.of<GroupInfoModel>(context);
  1212. var refName = Provider.of<RefNameProvider>(context)
  1213. .getGroupRefName(infoModel.sessionId, memberModel.memberId);
  1214. bool isShowSoundSate =
  1215. widget.msg.msgType == ChatType.ShortVoiceChatType.value &&
  1216. widget.msg.soundListened == 0;
  1217. return Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  1218. Container(
  1219. margin: const EdgeInsets.only(right: 8.0),
  1220. child: GestureDetector(
  1221. child: ClipRRect(
  1222. borderRadius: BorderRadius.circular(8),
  1223. child: hasHeadImg
  1224. ? CachedNetworkImage(
  1225. imageUrl: memberModel.avtar,
  1226. placeholder: (context, url) => Image.asset(
  1227. Constants.DefaultHeadImgUrl,
  1228. width: 40,
  1229. height: 40,
  1230. ),
  1231. width: 40,
  1232. height: 40,
  1233. )
  1234. : SizedBox(
  1235. width: 40,
  1236. height: 40,
  1237. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  1238. onTap: () {
  1239. AppNavigator.pushProfileInfoPage(context, memberModel.memberId,
  1240. fromWhere: 2,
  1241. addMode: !FriendListMgr().isMyFriend(memberModel.memberId)
  1242. ? 1
  1243. : 0);
  1244. },
  1245. onLongPress: () {
  1246. print('long press user');
  1247. MessageMgr().emit('Alter User Message', memberModel);
  1248. },
  1249. )),
  1250. infoModel.isShowName > 0
  1251. ? Column(
  1252. crossAxisAlignment: CrossAxisAlignment.start,
  1253. children: <Widget>[
  1254. Container(
  1255. padding: EdgeInsets.only(left: 9),
  1256. child: fixedText(refName, fontSize: 12, color: Colors.grey),
  1257. ),
  1258. SizedBox(height: 2),
  1259. _reveiveMsg(context),
  1260. ],
  1261. )
  1262. : _reveiveMsg(context),
  1263. isShowSoundSate
  1264. ? Container(
  1265. margin: EdgeInsets.only(
  1266. left: 8, top: infoModel.isShowName > 0 ? 38 : 20),
  1267. child: CircleAvatar(
  1268. radius: 3.5,
  1269. backgroundColor: Colors.red,
  1270. ))
  1271. : Container()
  1272. ]);
  1273. }
  1274. }