Hibok
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1303 linhas
41 KiB

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