Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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