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

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