|
- import 'package:chat/chat/video_view.dart';
- import 'package:chat/models/ChatMsg.dart';
- import 'package:chat/proto/all.pbserver.dart';
- import 'package:chat/utils/screen.dart';
- import 'package:chat/utils/upload_util.dart';
- import 'package:flutter/material.dart';
- import 'package:percent_indicator/circular_percent_indicator.dart';
-
- class DownloadItem extends StatefulWidget {
- final MsgModel msg;
- final Widget child;
- final bool isShowProgress;
- final bool isAutoDown;
- final Function onComplete;
- final Function onFinishTap;
- DownloadItem({
- this.msg,
- this.child,
- this.isShowProgress = true,
- this.isAutoDown = true,
- this.onComplete,
- this.onFinishTap,
- });
- @override
- _DownloadItemState createState() => _DownloadItemState();
- }
-
- class _DownloadItemState extends State<DownloadItem> {
- int downloadState = 0; //0,默认,1进行中,2成功,3失败
-
- @override
- void initState() {
- super.initState();
-
- if (widget.msg.localFile == null) {
- if (widget.isAutoDown &&
- (widget.msg.state != MsgState.Downloading &&
- widget.msg.state != MsgState.DownloadSuccess)) {
- downloadRes();
- }
- }
- }
-
- downloadRes() async {
- print('开始下载文件${widget.msg.extraFile}');
- widget.msg.state = MsgState.Downloading;
-
- if (mounted) {
- setState(() {});
- }
- var filePath = await UploadUtil().downloadFile(widget.msg);
-
- if (filePath != null) {
- print('下载完成');
- if (widget.onComplete != null) {
- widget.onComplete();
- }
- if (mounted) {
- setState(() {});
- }
- } else {
- print('下载文件失败');
-
- if (mounted) {
- setState(() {});
- }
- }
- }
-
- showVideoPage(BuildContext context, String filePath) {
- Navigator.push(context,
- MaterialPageRoute<void>(builder: (BuildContext context) {
- return VideoPage(videoPath: filePath);
- }));
- }
-
- _downloadWidget() {
- if (widget.msg.localFile != null) {
- if (widget.msg.msgType == ChatType.ShortVideoChatType.value) {
- return Icon(Icons.play_circle_outline, color: Colors.white);
- }
- } else {
- if (widget.msg.state == MsgState.DownloadFailed) {
- return Container(
- padding: EdgeInsets.all(5),
- decoration: BoxDecoration(
- color: Colors.grey.withAlpha(150),
- borderRadius: BorderRadius.circular(8)),
- child: Icon(Icons.refresh, color: Colors.white70),
- );
- } else if (widget.msg.state == MsgState.Downloading &&
- widget.isShowProgress) {
- return StreamBuilder(
- stream: UploadUtil().getStream(widget.msg.extraFile).stream,
- initialData: UploadUtil().streamLastPercentMap[widget.msg.extraFile],
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- if (snapshot.data != null) {
- if (snapshot.data >= 1) {
- return SizedBox(width: 0, height: 0);
- }
-
- return CircularPercentIndicator(
- radius: 30.0,
- lineWidth: 2.0,
- percent: snapshot.data,
- center: Padding(
- padding: EdgeInsets.all(2),
- child: fixedText(
- "${(snapshot.data * 100).toStringAsFixed(0)}%",
- fontSize: 9,
- color: Colors.green)),
- progressColor: Colors.green,
- );
- } else {
- return SizedBox(width: 0, height: 0);
- }
- },
- );
- } else if (widget.msg.state < MsgState.Downloading) {
- if (widget.msg.msgType == ChatType.ShortVideoChatType.value) {
- return Icon(Icons.play_circle_outline, color: Colors.white);
- }
- }
- }
-
- return Container(width: 0, height: 0);
- }
-
- @override
- Widget build(BuildContext context) {
- bool isNeedDown = widget.msg.localFile == null &&
- (widget.msg.state != MsgState.Downloading &&
- widget.msg.state != MsgState.DownloadSuccess);
- return InkWell(
- onTap: isNeedDown ? downloadRes : widget.onFinishTap,
- child: Stack(
- alignment: Alignment.center,
- children: <Widget>[
- widget.child,
- _downloadWidget(),
- ],
- ));
- }
- }
|