Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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