Hibok
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

145 satır
4.4 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 Icon(Icons.play_circle_outline, color: Colors.white);
  71. }
  72. } else {
  73. if (widget.msg.state == MsgState.DownloadFailed) {
  74. return Container(
  75. padding: EdgeInsets.all(5),
  76. decoration: BoxDecoration(
  77. color: Colors.grey.withAlpha(150),
  78. borderRadius: BorderRadius.circular(8)),
  79. child: Icon(Icons.refresh, color: Colors.white70),
  80. );
  81. } else if (widget.msg.state == MsgState.Downloading &&
  82. widget.isShowProgress) {
  83. return StreamBuilder(
  84. stream: UploadUtil().getStream(widget.msg.extraFile).stream,
  85. initialData: UploadUtil().streamLastPercentMap[widget.msg.extraFile],
  86. builder: (BuildContext context, AsyncSnapshot snapshot) {
  87. if (snapshot.data != null) {
  88. if (snapshot.data >= 1) {
  89. return SizedBox(width: 0, height: 0);
  90. }
  91. return CircularPercentIndicator(
  92. radius: 30.0,
  93. lineWidth: 2.0,
  94. percent: snapshot.data,
  95. center: Padding(
  96. padding: EdgeInsets.all(2),
  97. child: fixedText(
  98. "${(snapshot.data * 100).toStringAsFixed(0)}%",
  99. fontSize: 9,
  100. color: Colors.green)),
  101. progressColor: Colors.green,
  102. );
  103. } else {
  104. return SizedBox(width: 0, height: 0);
  105. }
  106. },
  107. );
  108. } else if (widget.msg.state < MsgState.Downloading) {
  109. if (widget.msg.msgType == ChatType.ShortVideoChatType.value) {
  110. return Icon(Icons.play_circle_outline, color: Colors.white);
  111. }
  112. }
  113. }
  114. return Container(width: 0, height: 0);
  115. }
  116. @override
  117. Widget build(BuildContext context) {
  118. bool isNeedDown = widget.msg.localFile == null &&
  119. (widget.msg.state != MsgState.Downloading &&
  120. widget.msg.state != MsgState.DownloadSuccess);
  121. return InkWell(
  122. onTap: isNeedDown ? downloadRes : widget.onFinishTap,
  123. child: Stack(
  124. alignment: Alignment.center,
  125. children: <Widget>[
  126. widget.child,
  127. _downloadWidget(),
  128. ],
  129. ));
  130. }
  131. }