Hibok
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1401 líneas
44 KiB

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