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

157 рядки
4.8 KiB

  1. import 'package:chat/chat/video_view.dart';
  2. import 'package:chat/models/ChatMsg.dart';
  3. import 'package:chat/proto/all.pbserver.dart';
  4. import 'package:chat/utils/screen.dart';
  5. import 'package:chat/utils/upload_util.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:percent_indicator/circular_percent_indicator.dart';
  8. class DownloadItem extends StatefulWidget {
  9. final MsgModel msg;
  10. final Widget child;
  11. final bool isShowProgress;
  12. final bool isAutoDown;
  13. final Function onComplete;
  14. final Function onFinishTap;
  15. DownloadItem({
  16. this.msg,
  17. this.child,
  18. this.isShowProgress = true,
  19. this.isAutoDown = true,
  20. this.onComplete,
  21. this.onFinishTap,
  22. });
  23. @override
  24. _DownloadItemState createState() => _DownloadItemState();
  25. }
  26. class _DownloadItemState extends State<DownloadItem> {
  27. int downloadState = 0; //0,默认,1进行中,2成功,3失败
  28. @override
  29. void initState() {
  30. super.initState();
  31. if (widget.msg.localFile == null) {
  32. if (widget.isAutoDown &&
  33. (widget.msg.state != MsgState.Downloading &&
  34. widget.msg.state != MsgState.DownloadSuccess)) {
  35. downloadRes();
  36. }
  37. }
  38. }
  39. downloadRes() async {
  40. print('开始下载文件${widget.msg.extraFile}');
  41. widget.msg.state = MsgState.Downloading;
  42. if (mounted) {
  43. setState(() {});
  44. }
  45. var filePath = await UploadUtil().downloadFile(widget.msg);
  46. if (filePath != null) {
  47. print('下载完成');
  48. if (widget.onComplete != null) {
  49. widget.onComplete();
  50. }
  51. if (mounted) {
  52. setState(() {});
  53. }
  54. } else {
  55. print('下载文件失败');
  56. if (mounted) {
  57. setState(() {});
  58. }
  59. }
  60. }
  61. showVideoPage(BuildContext context, String filePath) {
  62. Navigator.push(context,
  63. MaterialPageRoute<void>(builder: (BuildContext context) {
  64. return VideoPage(videoPath: filePath);
  65. }));
  66. }
  67. _downloadWidget() {
  68. if (widget.msg.localFile != null) {
  69. if (widget.msg.msgType == ChatType.ShortVideoChatType.value) {
  70. return Container(
  71. padding: EdgeInsets.all(5),
  72. decoration: BoxDecoration(
  73. color: Colors.grey.withAlpha(150),
  74. borderRadius: BorderRadius.circular(8)),
  75. child: Icon(Icons.play_circle_outline, color: Colors.white70),
  76. );
  77. }
  78. } else {
  79. if (widget.msg.state == MsgState.DownloadFailed) {
  80. return Container(
  81. padding: EdgeInsets.all(5),
  82. decoration: BoxDecoration(
  83. color: Colors.grey.withAlpha(150),
  84. borderRadius: BorderRadius.circular(8)),
  85. child: Icon(Icons.refresh, color: Colors.white70),
  86. );
  87. } else if (widget.msg.state == MsgState.Downloading &&
  88. widget.isShowProgress) {
  89. return StreamBuilder(
  90. stream: UploadUtil().getStream(widget.msg.extraFile).stream,
  91. initialData: UploadUtil().streamLastPercentMap[widget.msg.extraFile],
  92. builder: (BuildContext context, AsyncSnapshot snapshot) {
  93. if (snapshot.data != null) {
  94. if (snapshot.data >= 1) {
  95. return SizedBox(width: 0, height: 0);
  96. }
  97. return CircularPercentIndicator(
  98. radius: 30.0,
  99. lineWidth: 2.0,
  100. percent: snapshot.data,
  101. center: Padding(
  102. padding: EdgeInsets.all(2),
  103. child: fixedText(
  104. "${(snapshot.data * 100).toStringAsFixed(0)}%",
  105. fontSize: 9,
  106. color: Colors.green)),
  107. progressColor: Colors.green,
  108. );
  109. } else {
  110. return SizedBox(width: 0, height: 0);
  111. }
  112. },
  113. );
  114. } else if (widget.msg.state < MsgState.Downloading) {
  115. if (widget.msg.msgType == ChatType.ShortVideoChatType.value) {
  116. return Container(
  117. padding: EdgeInsets.all(5),
  118. decoration: BoxDecoration(
  119. color: Colors.grey.withAlpha(150),
  120. borderRadius: BorderRadius.circular(8)),
  121. child: Icon(Icons.play_circle_outline, color: Colors.white70),
  122. );
  123. }
  124. }
  125. }
  126. return Container(width: 0, height: 0);
  127. }
  128. @override
  129. Widget build(BuildContext context) {
  130. bool isNeedDown = widget.msg.localFile == null &&
  131. (widget.msg.state != MsgState.Downloading &&
  132. widget.msg.state != MsgState.DownloadSuccess);
  133. return InkWell(
  134. onTap: isNeedDown ? downloadRes : widget.onFinishTap,
  135. child: Stack(
  136. alignment: Alignment.center,
  137. children: <Widget>[
  138. widget.child,
  139. _downloadWidget(),
  140. ],
  141. ));
  142. }
  143. }