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.
 
 
 
 
 
 

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