Hibok
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1340 Zeilen
43 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 =
  735. _translateItemWidget(0xe670, I18n.of(context).man_retranslate, null);
  736. machineTranslateWidget =
  737. _translateItemWidget(0xe671, I18n.of(context).robotTranslate, null);
  738. } else if (transTag == 2) {
  739. //人工翻译中
  740. userTranslateWidget =
  741. _translateItemWidget(0xe670, I18n.of(context).ManTranslate, null);
  742. machineTranslateWidget = _translateItemWidget(
  743. 0xe671,
  744. I18n.of(context).robot_retranslate,
  745. textList.length <= 1
  746. ? null
  747. : () {
  748. setState(() {
  749. curTextType += 1;
  750. curTextType %= textList.length;
  751. });
  752. });
  753. } else if (transTag == 3) {
  754. //机器翻译完成
  755. userTranslateWidget = _translateItemWidget(
  756. 0xe670,
  757. I18n.of(context).man_retranslate,
  758. isTranslating
  759. ? null
  760. : () async {
  761. isTranslating = true;
  762. int money =
  763. Provider.of<MoneyChangeProvider>(context, listen: false)
  764. .money;
  765. int voucher =
  766. Provider.of<VoucherChangeProvider>(context, listen: false)
  767. .voucher;
  768. int needMoney = widget.msg.getNeedMoney();
  769. if (needMoney > voucher + money) {
  770. showToast('翻译券和H币不足');
  771. return;
  772. }
  773. var res = await HttpUtil().getPersonalTranslate(widget.msg);
  774. if (res) {
  775. print('请求人工翻译成功,进行扣费');
  776. setState(() {
  777. widget.msg.transTag = 2;
  778. //优先扣券
  779. if (voucher > 0) {
  780. int costQuan = min(voucher, needMoney);
  781. Provider.of<VoucherChangeProvider>(context,
  782. listen: false)
  783. .subVoucher(costQuan);
  784. }
  785. //不足的话再扣H币
  786. if (needMoney > voucher) {
  787. Provider.of<MoneyChangeProvider>(context, listen: false)
  788. .subMoney(needMoney - voucher);
  789. }
  790. });
  791. }
  792. });
  793. machineTranslateWidget = _translateItemWidget(
  794. 0xe671,
  795. I18n.of(context).robot_retranslate,
  796. textList.length <= 1
  797. ? null
  798. : () {
  799. setState(() {
  800. curTextType += 1;
  801. curTextType %= textList.length;
  802. });
  803. });
  804. } else if (transTag == 4 || transTag == 10) {
  805. //4人工翻译完成,未评论 10人工翻译完成已评论
  806. userTranslateWidget = InkWell(
  807. onTap: () {
  808. setState(() {
  809. curTextType = 0;
  810. });
  811. },
  812. child: Container(
  813. width: width / 2,
  814. child: Row(
  815. children: <Widget>[
  816. InkWell(
  817. child: Icon(
  818. IconData(0xe641, fontFamily: Constants.IconFontFamily),
  819. size: 18,
  820. color:
  821. transTag == 10 ? Colors.grey : Color(0xFF087FF3)),
  822. onTap: transTag == 10
  823. ? null
  824. : () {
  825. CustomUI.showIosDialog(
  826. context, I18n.of(context).bad_ev, () async {
  827. bool isSuccess = await rateTranslateResult();
  828. if (isSuccess) {
  829. Navigator.of(context).pop(true);
  830. showToast(I18n.of(context).success);
  831. } else {
  832. showToast(I18n.of(context).fail);
  833. Navigator.of(context).pop();
  834. }
  835. }, () {
  836. Navigator.of(context).pop();
  837. });
  838. },
  839. ),
  840. SizedBox(width: 5),
  841. Expanded(
  842. child: SizedBox(
  843. child: Text(
  844. I18n.of(context).over,
  845. style: TextStyle(color: Color(0xFF087FF3), fontSize: 10),
  846. textScaleFactor: 1.0,
  847. maxLines: 1,
  848. overflow: TextOverflow.ellipsis,
  849. ),
  850. ))
  851. ],
  852. )));
  853. machineTranslateWidget =
  854. _translateItemWidget(0xe675, I18n.of(context).see_original, () {
  855. setState(() {
  856. curTextType = textList.length - 1;
  857. });
  858. });
  859. }
  860. return Container(
  861. height: 26,
  862. alignment: Alignment.center,
  863. child: Row(
  864. children: <Widget>[
  865. userTranslateWidget,
  866. Expanded(
  867. child: SizedBox(child: VerticalDivider(), height: 20, width: 2)),
  868. machineTranslateWidget
  869. ],
  870. ),
  871. );
  872. }
  873. _receiveImg(BuildContext context, List<int> imgData, {String downloadData}) {
  874. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  875. var imgSize = _getImgSize();
  876. return GestureDetector(
  877. child: Container(
  878. alignment: Alignment.centerLeft,
  879. width: imgSize.width,
  880. height: imgSize.height,
  881. child: ClipRRect(
  882. child: Image(
  883. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  884. ),
  885. borderRadius: BorderRadius.circular(5),
  886. )),
  887. onTap: () async {
  888. widget.hideKeyboard();
  889. showFullImg(context, widget.msg);
  890. });
  891. }
  892. _receiveVideo(BuildContext context, List<int> imgData,
  893. {String downloadData}) {
  894. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  895. var imgSize = _getImgSize();
  896. return InkWell(
  897. onTap: () {
  898. if (widget.msg.localFile != null) {
  899. widget.hideKeyboard();
  900. showVideoPage(context, widget.msg.localFile);
  901. }
  902. },
  903. child: DownloadItem(
  904. isAutoDown: false,
  905. msg: widget.msg,
  906. child: Container(
  907. width: imgSize.width,
  908. height: imgSize.height,
  909. child: ClipRRect(
  910. child: Image(
  911. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  912. ),
  913. borderRadius: BorderRadius.circular(5),
  914. ),
  915. ),
  916. ));
  917. }
  918. showVideoPage(BuildContext context, String filePath) {
  919. Navigator.push(context,
  920. MaterialPageRoute<void>(builder: (BuildContext context) {
  921. return VideoPage(videoPath: filePath);
  922. }));
  923. }
  924. _receiveSound(BuildContext context, List<int> soundData) {
  925. print(' build 收到的语音消息');
  926. MsgModel msg = widget.msg;
  927. var time = widget.msg.extraInfo / 1000;
  928. if (time > 60) {
  929. time = 60.0;
  930. }
  931. bool isPlaying = false;
  932. if (curSoundUrl != null) {
  933. isPlaying = SoundUtils().isPlaying(curSoundUrl);
  934. }
  935. var soundWidget = InkWell(
  936. onTap: () async {
  937. bool isLocal = true;
  938. if (msg.localFile != null) {
  939. isLocal = true;
  940. curSoundUrl = msg.localFile;
  941. } else {
  942. isLocal = false;
  943. var sessionId = msg.sessionId;
  944. curSoundUrl = UploadUtil()
  945. .getFullUrl(msg.extraFile, sessionId, msg.channelType);
  946. }
  947. print('当前文件$curSoundUrl 本地?$isLocal');
  948. if (isPlaying) {
  949. await SoundUtils().pause();
  950. } else {
  951. print('开始播放');
  952. if (widget.msg.soundListened == 0) {
  953. widget.msg.updateSoundListened();
  954. }
  955. await SoundUtils().play(curSoundUrl, isLocal: isLocal, onPlayed: () {
  956. if (mounted) {
  957. this.setState(() {});
  958. }
  959. }, complete: () {
  960. if (mounted) {
  961. this.setState(() {});
  962. }
  963. });
  964. }
  965. },
  966. child: Container(
  967. width: 130,
  968. child: Row(children: <Widget>[
  969. Container(
  970. alignment: Alignment.center,
  971. padding: EdgeInsets.only(bottom: 2),
  972. margin: EdgeInsets.only(right: 10),
  973. width: 25.5,
  974. height: 25.5,
  975. decoration: BoxDecoration(
  976. border:
  977. Border.all(color: const Color(0xFF1B92C7), width: 0.5),
  978. color: const Color(0xFF04A4FE),
  979. shape: BoxShape.circle),
  980. child: Icon(
  981. IconData(isPlaying ? 0xe652 : 0xe653,
  982. fontFamily: Constants.IconFontFamily),
  983. size: isPlaying ? 15 : 18,
  984. color: Colors.white,
  985. ),
  986. ),
  987. isPlaying
  988. ? Stack(
  989. children: <Widget>[
  990. Container(
  991. height: 18,
  992. width: 19,
  993. ),
  994. Positioned(
  995. bottom: 0,
  996. child: VideoAnim(
  997. begin: 18,
  998. start: 0.444,
  999. end: 4.5,
  1000. )),
  1001. Positioned(
  1002. left: 7,
  1003. bottom: 0,
  1004. child: VideoAnim(
  1005. begin: 4.5,
  1006. end: 18,
  1007. )),
  1008. Positioned(
  1009. left: 14,
  1010. bottom: 0,
  1011. child: VideoAnim(
  1012. begin: 18,
  1013. end: 4.5,
  1014. ))
  1015. ],
  1016. )
  1017. : Stack(
  1018. children: <Widget>[
  1019. Container(
  1020. height: 18,
  1021. width: 19,
  1022. ),
  1023. Positioned(
  1024. bottom: 0, child: CustomUI.buildAudioContaniner(12)),
  1025. Positioned(
  1026. left: 7,
  1027. bottom: 0,
  1028. child: CustomUI.buildAudioContaniner(4.5)),
  1029. Positioned(
  1030. left: 14,
  1031. bottom: 0,
  1032. child: CustomUI.buildAudioContaniner(18))
  1033. ],
  1034. ),
  1035. Expanded(child: SizedBox()),
  1036. fixedText(time.toStringAsFixed(0),
  1037. color: Constants.BlackTextColor, fontSize: 16)
  1038. ])),
  1039. );
  1040. List<Widget> showMsg = [];
  1041. showMsg.add(DownloadItem(
  1042. msg: widget.msg,
  1043. child: soundWidget,
  1044. isShowProgress: false,
  1045. ));
  1046. double width = 130;
  1047. double minWidth = 0;
  1048. if (textList.length > 0) {
  1049. width = _getTextWidth(textList[curTextType]) + 20;
  1050. width = min(width, Screen.width - 120);
  1051. minWidth = max(width, 150);
  1052. showMsg.add(Padding(
  1053. padding: EdgeInsets.symmetric(vertical: 5),
  1054. child: Divider(
  1055. height: 1,
  1056. )));
  1057. showMsg.add(Container(
  1058. child: extendedText(
  1059. textList[curTextType],
  1060. color: Constants.BlackTextColor,
  1061. hideKeyboard: widget.hideKeyboard,
  1062. fontSize: FontSize,
  1063. ),
  1064. alignment: Alignment.centerLeft,
  1065. constraints:
  1066. BoxConstraints(maxWidth: Screen.width - 120, minHeight: 22),
  1067. ));
  1068. }
  1069. if (msg.transTag != 0) {
  1070. minWidth = 200;
  1071. showMsg.add(Divider(color: Color(0xFFECECEC), height: 3));
  1072. Widget tranWidget = _transProcessWidget(msg.transTag);
  1073. showMsg.add(tranWidget);
  1074. }
  1075. return Container(
  1076. width: width + 20,
  1077. constraints:
  1078. msg.transTag != 0 ? BoxConstraints(minWidth: minWidth) : null,
  1079. child: Column(
  1080. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  1081. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1082. decoration: BoxDecoration(
  1083. color: Colors.white,
  1084. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1085. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1086. );
  1087. }
  1088. void showFullImg(BuildContext context, MsgModel msg) {
  1089. print('显示图片');
  1090. Navigator.push(context,
  1091. MaterialPageRoute<void>(builder: (BuildContext context) {
  1092. return PhotoPage(msg: msg);
  1093. }));
  1094. }
  1095. _reveiveMsg(BuildContext context) {
  1096. Widget item;
  1097. switch (ChatType.valueOf(widget.msg.msgType)) {
  1098. case ChatType.TextChatType:
  1099. item = _receiveText(widget.msg);
  1100. break;
  1101. case ChatType.EmoticonType:
  1102. item = _receiveJIF(widget.msg);
  1103. break;
  1104. case ChatType.ImageChatType:
  1105. if (widget.msg.extraFile != null) {
  1106. item = _receiveImg(context, widget.msg.msgContent,
  1107. downloadData: widget.msg.extraFile);
  1108. } else {
  1109. item = _receiveImg(context, widget.msg.msgContent);
  1110. }
  1111. break;
  1112. case ChatType.ShortVideoChatType:
  1113. item = _receiveVideo(context, widget.msg.msgContent,
  1114. downloadData: widget.msg.extraFile);
  1115. break;
  1116. case ChatType.ShortVoiceChatType:
  1117. item = _receiveSound(context, widget.msg.msgContent);
  1118. break;
  1119. case ChatType.PlaceChatType:
  1120. item = PlaceItem(isMe: false, placeContent: widget.msg.msgContent);
  1121. break;
  1122. case ChatType.FileChatType:
  1123. item = _receiveFileMsgItem();
  1124. break;
  1125. default:
  1126. }
  1127. return wrapItemWithMenu(item);
  1128. }
  1129. Widget _receiveFileMsgItem() {
  1130. return DownloadItem(
  1131. isAutoDown: false,
  1132. msg: widget.msg,
  1133. onComplete: () {
  1134. if (mounted) {
  1135. setState(() {});
  1136. }
  1137. },
  1138. child: Container(
  1139. height: 100,
  1140. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  1141. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1142. decoration: BoxDecoration(
  1143. color: Colors.white,
  1144. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1145. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1146. child: FileMsgItem(widget.msg)));
  1147. }
  1148. Widget _getReceivedMessageLayout(BuildContext context) {
  1149. bool hasHeadImg = true;
  1150. var memberModel = widget.memberModel;
  1151. if (memberModel == null) {
  1152. return Container();
  1153. }
  1154. if (memberModel.avtar == null || memberModel.avtar.length == 0) {
  1155. hasHeadImg = false;
  1156. }
  1157. GroupInfoModel infoModel = Provider.of<GroupInfoModel>(context);
  1158. var refName = Provider.of<RefNameProvider>(context)
  1159. .getGroupRefName(infoModel.sessionId, memberModel.memberId);
  1160. bool isShowSoundSate =
  1161. widget.msg.msgType == ChatType.ShortVoiceChatType.value &&
  1162. widget.msg.soundListened == 0;
  1163. return Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  1164. Container(
  1165. margin: const EdgeInsets.only(right: 8.0),
  1166. child: GestureDetector(
  1167. child: ClipRRect(
  1168. borderRadius: BorderRadius.circular(8),
  1169. child: hasHeadImg
  1170. ? CachedNetworkImage(
  1171. imageUrl: memberModel.avtar,
  1172. placeholder: (context, url) => Image.asset(
  1173. Constants.DefaultHeadImgUrl,
  1174. width: 40,
  1175. height: 40,
  1176. ),
  1177. width: 40,
  1178. height: 40,
  1179. )
  1180. : SizedBox(
  1181. width: 40,
  1182. height: 40,
  1183. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  1184. onTap: () {
  1185. AppNavigator.pushProfileInfoPage(context, memberModel.memberId,
  1186. fromWhere: 2,
  1187. addMode:
  1188. FriendListMgr().isMyFriend(memberModel.memberId) ? 1 : 0);
  1189. },
  1190. onLongPress: () {
  1191. print('long press user');
  1192. MessageMgr().emit('Alter User Message', memberModel);
  1193. },
  1194. )),
  1195. infoModel.isShowName > 0
  1196. ? Column(
  1197. crossAxisAlignment: CrossAxisAlignment.start,
  1198. children: <Widget>[
  1199. Container(
  1200. padding: EdgeInsets.only(left: 9),
  1201. child: fixedText(refName, fontSize: 12, color: Colors.grey),
  1202. ),
  1203. SizedBox(height: 2),
  1204. _reveiveMsg(context),
  1205. ],
  1206. )
  1207. : _reveiveMsg(context),
  1208. isShowSoundSate
  1209. ? Container(
  1210. margin: EdgeInsets.only(
  1211. left: 8, top: infoModel.isShowName > 0 ? 38 : 20),
  1212. child: CircleAvatar(
  1213. radius: 3.5,
  1214. backgroundColor: Colors.red,
  1215. ))
  1216. : Container()
  1217. ]);
  1218. }
  1219. }