Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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