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.
 
 
 
 
 
 

1337 lines
42 KiB

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