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

114 行
3.0 KiB

  1. import 'package:chat/models/ChatMsg.dart';
  2. import 'package:chat/proto/chat.pbserver.dart';
  3. import 'package:chat/utils/MessageMgr.dart';
  4. import 'package:chat/utils/msgHandler.dart';
  5. import 'package:chat/utils/upload_util.dart';
  6. import 'package:flutter/cupertino.dart';
  7. import 'package:flutter/material.dart';
  8. class MsgStateWidget extends StatefulWidget {
  9. final MsgModel msg;
  10. MsgStateWidget(this.msg);
  11. @override
  12. _MsgStateWidgetState createState() => _MsgStateWidgetState();
  13. }
  14. class _MsgStateWidgetState extends State<MsgStateWidget> {
  15. int sendState = MsgState.None;
  16. @override
  17. void initState() {
  18. super.initState();
  19. sendState = widget.msg.state;
  20. MessageMgr().on('Update Chat Message State', updateSendState);
  21. }
  22. @override
  23. void dispose() {
  24. MessageMgr().off('Update Chat Message State', updateSendState);
  25. super.dispose();
  26. }
  27. updateSendState(msg) {
  28. if (widget.msg.time == msg['time'] &&
  29. widget.msg.sessionId == msg['sessionId']) {
  30. widget.msg.state = msg['state'];
  31. if (mounted) {
  32. setState(() {
  33. sendState = widget.msg.state;
  34. });
  35. }
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. if (sendState == MsgState.UploadFailed) {
  41. return Container(
  42. child: IconButton(
  43. iconSize: 22,
  44. icon: Icon(
  45. Icons.error_outline,
  46. color: Colors.red,
  47. ),
  48. onPressed: () {
  49. setState(() {
  50. sendState = MsgState.Uploading;
  51. });
  52. MessageMgr().emit('ReUpload Msg', widget.msg);
  53. },
  54. ),
  55. );
  56. }
  57. if (sendState == MsgState.SendingFailed) {
  58. return Container(
  59. child: IconButton(
  60. iconSize: 22,
  61. icon: Icon(
  62. Icons.error_outline,
  63. color: Colors.red,
  64. ),
  65. onPressed: () {
  66. print('重新发送');
  67. setState(() {
  68. sendState = MsgState.Sending;
  69. });
  70. MsgHandler.sendChatMsg(widget.msg);
  71. },
  72. ),
  73. );
  74. } else if (sendState == MsgState.SendingSuccess) {
  75. return Container(width: 0, height: 0);
  76. } else {
  77. if (widget.msg.state == MsgState.Uploading) {
  78. return Container(
  79. child: IconButton(
  80. iconSize: 22,
  81. icon: Icon(
  82. Icons.pause_circle_outline,
  83. color: Colors.red,
  84. ),
  85. onPressed: () {
  86. print('暂停发送');
  87. UploadUtil().cancelSendMsg(widget.msg);
  88. setState(() {
  89. sendState = MsgState.UploadFailed;
  90. });
  91. MsgHandler.sendChatMsg(widget.msg);
  92. },
  93. ),
  94. );
  95. } else {
  96. return Align(
  97. alignment: Alignment.center,
  98. child: Padding(
  99. padding: EdgeInsets.only(top: 5),
  100. child: CupertinoActivityIndicator()));
  101. }
  102. }
  103. }
  104. }