Hibok
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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