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.
 
 
 
 
 
 

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