Hibok
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

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