Hibok
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

1466 рядки
46 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. blueDot(bool isShow) {
  688. return Container(
  689. margin: EdgeInsets.only(right: 5),
  690. decoration: BoxDecoration(
  691. shape: BoxShape.circle,
  692. color: isShow ? Color(0xFFFF7E00) : Color(0xFFCFCFCF),
  693. ),
  694. width: 6,
  695. height: 6,
  696. );
  697. }
  698. _receiveText(MsgModel msg) {
  699. List<Widget> showMsg = [];
  700. if (textList.length > 0) {
  701. bool isUrl =false;
  702. if(textList[curTextType].contains('http') ){
  703. isUrl =true;
  704. }
  705. showMsg.add(InkWell(
  706. onTap: () {
  707. if (msg.transTag == 1) {
  708. return;
  709. }
  710. if (msg.transTag == 2 || msg.transTag == 3) {
  711. setState(() {
  712. curTextType += 1;
  713. curTextType %= textList.length;
  714. });
  715. return;
  716. }
  717. },
  718. child: Container(
  719. constraints:
  720. BoxConstraints(maxWidth: Screen.width - 140, minHeight: 22),
  721. alignment: Alignment.centerLeft,
  722. child: extendedText(
  723. textList[curTextType],
  724. color: isUrl ? Colors.blue : Constants.BlackTextColor,
  725. hideKeyboard: widget.hideKeyboard,
  726. fontSize: FontSize,
  727. ))));
  728. }
  729. var width = _getTextWidth(textList[curTextType]);
  730. var minWidth = width + 20;
  731. if (msg.transTag != 0) {
  732. minWidth = 200;
  733. showMsg.add(Padding(
  734. padding: EdgeInsets.symmetric(vertical: 5),
  735. child: Divider(color: Color(0xFFECECEC), height: 1)));
  736. Widget tranWidget = _transProcessWidget(msg.transTag);
  737. showMsg.add(tranWidget);
  738. }
  739. Widget text = Stack(children: <Widget>[
  740. Container(
  741. width: width + 20,
  742. constraints:
  743. BoxConstraints(maxWidth: Screen.width - 120, minWidth: minWidth),
  744. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  745. child: Column(
  746. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  747. decoration: BoxDecoration(
  748. border: Border.all(color: ReciveBorderColor, width: 0.5),
  749. color: isLongPressed ? Colors.grey[300] : Colors.white,
  750. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  751. ),
  752. msg.transTag != 1 && msg.transTag !=0
  753. ? Positioned(
  754. right: 5,
  755. top: 5,
  756. child: Container(
  757. child: Row(children: <Widget>[
  758. blueDot(curTextType == 0),
  759. blueDot(curTextType == 1),
  760. blueDot(curTextType == 2),
  761. //blueDot(true),
  762. ])))
  763. : Container()
  764. ]);
  765. if (msg.refMsgContent != null && msg.refMsgContent.length > 0) {
  766. QuoteMsg quoteMsg = QuoteMsg.fromBuffer(msg.refMsgContent);
  767. return Column(
  768. mainAxisSize: MainAxisSize.min,
  769. crossAxisAlignment: CrossAxisAlignment.start,
  770. children: <Widget>[
  771. text,
  772. SizedBox(height: 2),
  773. Container(
  774. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  775. padding: EdgeInsets.symmetric(vertical: 1, horizontal: 3),
  776. child: Text(
  777. quoteMsg.content,
  778. maxLines: 1,
  779. textScaleFactor: 1.0,
  780. overflow: TextOverflow.ellipsis,
  781. style: TextStyle(fontSize: 12),
  782. ),
  783. decoration: BoxDecoration(
  784. color: Colors.grey[300],
  785. borderRadius: BorderRadius.circular(5)),
  786. )
  787. ],
  788. );
  789. } else {
  790. return text;
  791. }
  792. }
  793. //用户评价人工翻译,差评
  794. rateTranslateResult() async {
  795. return await HttpUtil().rateTranslateResult(widget.msg, () {
  796. if (mounted) {
  797. setState(() {});
  798. }
  799. });
  800. }
  801. _translateItemWidget(int code, String title, Function onTap) {
  802. Color color = onTap == null ? Constants.GreyTextColor : Color(0xFF087FF3);
  803. return InkWell(
  804. onTap: onTap,
  805. splashColor: Colors.red,
  806. child: Container(
  807. width: 80,
  808. child: Row(
  809. children: <Widget>[
  810. Icon(
  811. IconData(code, fontFamily: Constants.IconFontFamily),
  812. color: color,
  813. size: 20,
  814. ),
  815. SizedBox(width: 5),
  816. Expanded(
  817. child: SizedBox(
  818. child: Text(
  819. title,
  820. style: TextStyle(color: color, fontSize: 10),
  821. textScaleFactor: 1.0,
  822. maxLines: 1,
  823. overflow: TextOverflow.ellipsis,
  824. ),
  825. ))
  826. ],
  827. )));
  828. }
  829. bool isTranslating = false;
  830. _transProcessWidget(int transTag) {
  831. double width = 160;
  832. Widget userTranslateWidget;
  833. Widget machineTranslateWidget;
  834. if (transTag == 1) {
  835. //机器翻译中
  836. userTranslateWidget =
  837. _translateItemWidget(0xe670, I18n.of(context).man_retranslate, null);
  838. machineTranslateWidget =
  839. _translateItemWidget(0xe671, I18n.of(context).robotTranslate, null);
  840. } else if (transTag == 2) {
  841. //人工翻译中
  842. userTranslateWidget =
  843. _translateItemWidget(0xe670, I18n.of(context).ManTranslate, null);
  844. machineTranslateWidget = _translateItemWidget(
  845. 0xe671,
  846. I18n.of(context).robot_retranslate,
  847. textList.length <= 1
  848. ? null
  849. : () {
  850. setState(() {
  851. curTextType += 1;
  852. curTextType %= textList.length;
  853. });
  854. });
  855. } else if (transTag == 3) {
  856. //机器翻译完成
  857. userTranslateWidget = _translateItemWidget(
  858. 0xe670,
  859. I18n.of(context).man_retranslate,
  860. isTranslating
  861. ? null
  862. : () async {
  863. isTranslating = true;
  864. int money =
  865. Provider.of<MoneyChangeProvider>(context, listen: false)
  866. .money;
  867. int voucher =
  868. Provider.of<VoucherChangeProvider>(context, listen: false)
  869. .voucher;
  870. int needMoney = widget.msg.getNeedMoney();
  871. if (needMoney > voucher + money) {
  872. showToast('翻译券和H币不足');
  873. return;
  874. }
  875. var res = await HttpUtil().getPersonalTranslate(widget.msg);
  876. if (res) {
  877. print('请求人工翻译成功,进行扣费');
  878. setState(() {
  879. widget.msg.transTag = 2;
  880. //优先扣券
  881. if (voucher > 0) {
  882. int costQuan = min(voucher, needMoney);
  883. Provider.of<VoucherChangeProvider>(context,
  884. listen: false)
  885. .subVoucher(costQuan);
  886. }
  887. //不足的话再扣H币
  888. if (needMoney > voucher) {
  889. Provider.of<MoneyChangeProvider>(context, listen: false)
  890. .subMoney(needMoney - voucher);
  891. }
  892. });
  893. }
  894. });
  895. machineTranslateWidget = _translateItemWidget(
  896. 0xe671,
  897. I18n.of(context).robot_retranslate,
  898. textList.length <= 1
  899. ? null
  900. : () {
  901. setState(() {
  902. curTextType += 1;
  903. curTextType %= textList.length;
  904. });
  905. });
  906. } else if (transTag == 4 || transTag == 10) {
  907. //4人工翻译完成,未评论 10人工翻译完成已评论
  908. userTranslateWidget = InkWell(
  909. onTap: () {
  910. setState(() {
  911. curTextType = 0;
  912. });
  913. },
  914. child: Container(
  915. width: width / 2,
  916. child: Row(
  917. children: <Widget>[
  918. InkWell(
  919. child: Icon(
  920. IconData(0xe641, fontFamily: Constants.IconFontFamily),
  921. size: 18,
  922. color:
  923. transTag == 10 ? Colors.grey : Color(0xFF087FF3)),
  924. onTap: transTag == 10
  925. ? null
  926. : () {
  927. CustomUI.showIosDialog(
  928. context, I18n.of(context).bad_ev, () async {
  929. bool isSuccess = await rateTranslateResult();
  930. if (isSuccess) {
  931. Navigator.of(context).pop(true);
  932. showToast(I18n.of(context).success);
  933. } else {
  934. showToast(I18n.of(context).fail);
  935. Navigator.of(context).pop();
  936. }
  937. }, () {
  938. Navigator.of(context).pop();
  939. });
  940. },
  941. ),
  942. SizedBox(width: 5),
  943. Expanded(
  944. child: SizedBox(
  945. child: Text(
  946. I18n.of(context).over,
  947. style: TextStyle(color: Color(0xFF087FF3), fontSize: 10),
  948. textScaleFactor: 1.0,
  949. maxLines: 1,
  950. overflow: TextOverflow.ellipsis,
  951. ),
  952. ))
  953. ],
  954. )));
  955. machineTranslateWidget =
  956. _translateItemWidget(0xe675, I18n.of(context).see_original, () {
  957. setState(() {
  958. curTextType = textList.length - 1;
  959. });
  960. });
  961. }
  962. return Container(
  963. height: 26,
  964. alignment: Alignment.center,
  965. child: Row(
  966. children: <Widget>[
  967. userTranslateWidget,
  968. Expanded(
  969. child: SizedBox(child: VerticalDivider(), height: 20, width: 2)),
  970. machineTranslateWidget
  971. ],
  972. ),
  973. );
  974. }
  975. _receiveImg(BuildContext context, List<int> imgData, {String downloadData}) {
  976. // ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  977. ImageProvider provider = MemoryImage(widget.msg.localFile==null?Uint8List.fromList(imgData):File(widget.msg.localFile).readAsBytesSync());
  978. var imgSize = _getImgSize();
  979. return DownloadItem(
  980. isAutoDown: false,
  981. msg: widget.msg,
  982. onFinishTap: (){
  983. widget.hideKeyboard();
  984. showFullImg(context, widget.msg);
  985. },
  986. child: Container(
  987. alignment: Alignment.centerLeft,
  988. width: imgSize.width,
  989. height: imgSize.height,
  990. child: ClipRRect(
  991. child: Image(
  992. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  993. ),
  994. borderRadius: BorderRadius.circular(5),
  995. ),
  996. ),
  997. );
  998. }
  999. _receiveVideo(BuildContext context, List<int> imgData,
  1000. {String downloadData}) {
  1001. ImageProvider provider = MemoryImage(Uint8List.fromList(imgData));
  1002. var imgSize = _getImgSize();
  1003. return InkWell(
  1004. onTap: () {
  1005. if (widget.msg.localFile != null) {
  1006. showVideoPage(context, widget.msg.localFile);
  1007. }
  1008. },
  1009. child: DownloadItem(
  1010. isAutoDown: false,
  1011. msg: widget.msg,
  1012. child: Container(
  1013. width: imgSize.width,
  1014. height: imgSize.height,
  1015. child: ClipRRect(
  1016. child: Image(
  1017. image: provider ?? AssetImage(R.assetsImagesIcAlbum),
  1018. ),
  1019. borderRadius: BorderRadius.circular(5),
  1020. ),
  1021. ),
  1022. ));
  1023. }
  1024. showVideoPage(BuildContext context, String filePath) {
  1025. widget.hideKeyboard();
  1026. Navigator.push(context,
  1027. MaterialPageRoute<void>(builder: (BuildContext context) {
  1028. return VideoPage(videoPath: filePath);
  1029. }));
  1030. }
  1031. _receiveSound(BuildContext context, List<int> soundData) {
  1032. print(' build 收到的语音消息');
  1033. MsgModel msg = widget.msg;
  1034. var time = widget.msg.extraInfo / 1000;
  1035. if (time > 60) {
  1036. time = 60.0;
  1037. }
  1038. bool isPlaying = false;
  1039. if (curSoundUrl != null) {
  1040. isPlaying = SoundUtils().isPlaying(curSoundUrl);
  1041. }
  1042. var soundWidget = InkWell(
  1043. onTap: () async {
  1044. bool isLocal = true;
  1045. if (msg.localFile != null) {
  1046. isLocal = true;
  1047. curSoundUrl = msg.localFile;
  1048. } else {
  1049. isLocal = false;
  1050. var sessionId = msg.sessionId;
  1051. curSoundUrl = UploadUtil()
  1052. .getFullUrl(msg.extraFile, sessionId, msg.channelType);
  1053. }
  1054. print('当前文件$curSoundUrl 本地?$isLocal');
  1055. if (isPlaying) {
  1056. await SoundUtils().pause();
  1057. } else {
  1058. print('开始播放');
  1059. if (widget.msg.soundListened == 0) {
  1060. widget.msg.updateSoundListened();
  1061. }
  1062. await SoundUtils().play(curSoundUrl, isLocal: isLocal, onPlayed: () {
  1063. if (mounted) {
  1064. this.setState(() {});
  1065. }
  1066. }, complete: () {
  1067. if (mounted) {
  1068. this.setState(() {});
  1069. }
  1070. });
  1071. }
  1072. },
  1073. child: Container(
  1074. width: 130,
  1075. child: Row(children: <Widget>[
  1076. Container(
  1077. alignment: Alignment.center,
  1078. padding: EdgeInsets.only(bottom: 2),
  1079. margin: EdgeInsets.only(right: 10),
  1080. width: 25.5,
  1081. height: 25.5,
  1082. decoration: BoxDecoration(
  1083. border:
  1084. Border.all(color: const Color(0xFF1B92C7), width: 0.5),
  1085. color: const Color(0xFF04A4FE),
  1086. shape: BoxShape.circle),
  1087. child: Icon(
  1088. IconData(isPlaying ? 0xe652 : 0xe653,
  1089. fontFamily: Constants.IconFontFamily),
  1090. size: isPlaying ? 15 : 18,
  1091. color: Colors.white,
  1092. ),
  1093. ),
  1094. isPlaying
  1095. ? Stack(
  1096. children: <Widget>[
  1097. Container(
  1098. height: 18,
  1099. width: 19,
  1100. ),
  1101. Positioned(
  1102. bottom: 0,
  1103. child: VideoAnim(
  1104. begin: 18,
  1105. start: 0.444,
  1106. end: 4.5,
  1107. )),
  1108. Positioned(
  1109. left: 7,
  1110. bottom: 0,
  1111. child: VideoAnim(
  1112. begin: 4.5,
  1113. end: 18,
  1114. )),
  1115. Positioned(
  1116. left: 14,
  1117. bottom: 0,
  1118. child: VideoAnim(
  1119. begin: 18,
  1120. end: 4.5,
  1121. ))
  1122. ],
  1123. )
  1124. : Stack(
  1125. children: <Widget>[
  1126. Container(
  1127. height: 18,
  1128. width: 19,
  1129. ),
  1130. Positioned(
  1131. bottom: 0, child: CustomUI.buildAudioContaniner(12)),
  1132. Positioned(
  1133. left: 7,
  1134. bottom: 0,
  1135. child: CustomUI.buildAudioContaniner(4.5)),
  1136. Positioned(
  1137. left: 14,
  1138. bottom: 0,
  1139. child: CustomUI.buildAudioContaniner(18))
  1140. ],
  1141. ),
  1142. Expanded(child: SizedBox()),
  1143. fixedText(time.toStringAsFixed(0),
  1144. color: Constants.BlackTextColor, fontSize: 16)
  1145. ])),
  1146. );
  1147. List<Widget> showMsg = [];
  1148. showMsg.add(DownloadItem(
  1149. msg: widget.msg,
  1150. child: soundWidget,
  1151. isShowProgress: false,
  1152. ));
  1153. double width = 130;
  1154. double minWidth = 0;
  1155. if (textList.length > 0) {
  1156. width = _getTextWidth(textList[curTextType]) + 20;
  1157. width = min(width, Screen.width - 120);
  1158. minWidth = max(width, 150);
  1159. showMsg.add(Padding(
  1160. padding: EdgeInsets.symmetric(vertical: 5),
  1161. child: Divider(
  1162. height: 1,
  1163. )));
  1164. showMsg.add(Container(
  1165. child: extendedText(
  1166. textList[curTextType],
  1167. color: Constants.BlackTextColor,
  1168. hideKeyboard: widget.hideKeyboard,
  1169. fontSize: FontSize,
  1170. ),
  1171. alignment: Alignment.centerLeft,
  1172. constraints:
  1173. BoxConstraints(maxWidth: Screen.width - 120, minHeight: 22),
  1174. ));
  1175. }
  1176. if (msg.transTag != 0) {
  1177. minWidth = 200;
  1178. showMsg.add(Divider(color: Color(0xFFECECEC), height: 3));
  1179. Widget tranWidget = _transProcessWidget(msg.transTag);
  1180. showMsg.add(tranWidget);
  1181. }
  1182. return Container(
  1183. width: width + 20,
  1184. constraints:
  1185. msg.transTag != 0 ? BoxConstraints(minWidth: minWidth) : null,
  1186. child: Column(
  1187. crossAxisAlignment: CrossAxisAlignment.start, children: showMsg),
  1188. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1189. decoration: BoxDecoration(
  1190. color: Colors.white,
  1191. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1192. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1193. );
  1194. }
  1195. void showFullImg(BuildContext context, MsgModel msg) {
  1196. print('显示图片');
  1197. Navigator.push(context,
  1198. MaterialPageRoute<void>(builder: (BuildContext context) {
  1199. return PhotoPage(msg: msg);
  1200. }));
  1201. }
  1202. _reveiveMsg(BuildContext context) {
  1203. Widget item;
  1204. switch (ChatType.valueOf(widget.msg.msgType)) {
  1205. case ChatType.TextChatType:
  1206. item = _receiveText(widget.msg);
  1207. break;
  1208. case ChatType.EmoticonType:
  1209. item = _receiveJIF(widget.msg);
  1210. break;
  1211. case ChatType.ImageChatType:
  1212. if (widget.msg.extraFile != null) {
  1213. item = _receiveImg(context, widget.msg.msgContent,
  1214. downloadData: widget.msg.extraFile);
  1215. } else {
  1216. item = _receiveImg(context, widget.msg.msgContent);
  1217. }
  1218. break;
  1219. case ChatType.ShortVideoChatType:
  1220. item = _receiveVideo(context, widget.msg.msgContent,
  1221. downloadData: widget.msg.extraFile);
  1222. break;
  1223. case ChatType.ShortVoiceChatType:
  1224. item = _receiveSound(context, widget.msg.msgContent);
  1225. break;
  1226. case ChatType.PlaceChatType:
  1227. item = PlaceItem(isMe: false, placeContent: widget.msg.msgContent);
  1228. break;
  1229. case ChatType.FileChatType:
  1230. item = _receiveFileMsgItem();
  1231. break;
  1232. default:
  1233. }
  1234. return wrapItemWithMenu(item);
  1235. }
  1236. Widget _receiveFileMsgItem() {
  1237. return DownloadItem(
  1238. isAutoDown: false,
  1239. msg: widget.msg,
  1240. onComplete: () {
  1241. if (mounted) {
  1242. setState(() {});
  1243. }
  1244. },
  1245. child: Container(
  1246. height: 100,
  1247. constraints: BoxConstraints(maxWidth: Screen.width - 120),
  1248. padding: EdgeInsets.symmetric(horizontal: 9, vertical: 10.5),
  1249. decoration: BoxDecoration(
  1250. color: Colors.white,
  1251. border: Border.all(color: ReciveBorderColor, width: 0.5),
  1252. borderRadius: BorderRadius.all(Radius.circular(ChatRadius))),
  1253. child: FileMsgItem(widget.msg)));
  1254. }
  1255. Widget _getReceivedMessageLayout(BuildContext context) {
  1256. bool hasHeadImg = true;
  1257. var memberModel = widget.memberModel;
  1258. if (memberModel == null) {
  1259. return Container();
  1260. }
  1261. if (memberModel.avtar == null || memberModel.avtar.length == 0) {
  1262. hasHeadImg = false;
  1263. }
  1264. GroupInfoModel infoModel = Provider.of<GroupInfoModel>(context);
  1265. var refName = Provider.of<RefNameProvider>(context)
  1266. .getGroupRefName(infoModel.sessionId, memberModel.memberId);
  1267. bool isShowSoundSate =
  1268. widget.msg.msgType == ChatType.ShortVoiceChatType.value &&
  1269. widget.msg.soundListened == 0;
  1270. return Row(crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
  1271. Container(
  1272. margin: const EdgeInsets.only(right: 8.0),
  1273. child: GestureDetector(
  1274. child: ClipRRect(
  1275. borderRadius: BorderRadius.circular(8),
  1276. child: hasHeadImg
  1277. ? CachedNetworkImage(
  1278. imageUrl: memberModel.avtar,
  1279. placeholder: (context, url) => Image.asset(
  1280. Constants.DefaultHeadImgUrl,
  1281. width: 40,
  1282. height: 40,
  1283. ),
  1284. width: 40,
  1285. height: 40,
  1286. )
  1287. : SizedBox(
  1288. width: 40,
  1289. height: 40,
  1290. child: Image.asset(R.assetsImagesDefaultNorAvatar))),
  1291. onTap: () {
  1292. AppNavigator.pushProfileInfoPage(context, memberModel.memberId,
  1293. fromWhere: 2,
  1294. addMode: !FriendListMgr().isMyFriend(memberModel.memberId)
  1295. ? 1
  1296. : 0);
  1297. },
  1298. onLongPress: () {
  1299. print('long press user');
  1300. MessageMgr().emit('Alter User Message', memberModel);
  1301. },
  1302. )),
  1303. infoModel.isShowName > 0
  1304. ? Column(
  1305. crossAxisAlignment: CrossAxisAlignment.start,
  1306. children: <Widget>[
  1307. Container(
  1308. padding: EdgeInsets.only(left: 9),
  1309. child: fixedText(refName, fontSize: 12, color: Colors.grey),
  1310. ),
  1311. SizedBox(height: 2),
  1312. _reveiveMsg(context),
  1313. ],
  1314. )
  1315. : _reveiveMsg(context),
  1316. isShowSoundSate
  1317. ? Container(
  1318. margin: EdgeInsets.only(
  1319. left: 8, top: infoModel.isShowName > 0 ? 38 : 20),
  1320. child: CircleAvatar(
  1321. radius: 3.5,
  1322. backgroundColor: Colors.red,
  1323. ))
  1324. : Container()
  1325. ]);
  1326. }
  1327. }