首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有包序列号进度对话框的文件下载器应用程序

带有包序列号进度对话框的文件下载器应用程序
EN

Stack Overflow用户
提问于 2021-11-23 16:07:28
回答 2查看 55关注 0票数 0

如何将progress_dialog包修改为sn_progress_dialog包?我正在尝试制作一个带有进度对话框的文件下载应用程序,但progress_dialog包不是空安全的。

代码语言:javascript
运行
复制
  Future _downloadAndSaveFileToStorage(String urlPath) async {

    final name = urlPdf.split('/').last;

    ProgressDialog pr;
    pr = ProgressDialog(context, type: ProgressDialogType.Normal);
    pr.style(message: "Download file ...");

    try{
      await pr.show();
      final Directory _documentDir = Directory('/storage/emulated/0/MyDocuments/$name');
      await dio!.download(urlPath, _documentDir.path, onReceiveProgress: (rec, total){
        setState(() {
          _isLoading = true;
          progress = ((rec / total)*100).toStringAsFixed(0) + " %";
          print(progress);
          pr.update(message: "Please wait : $progress");
        });
      });
      pr.hide();
      _fileFullPath = _documentDir.path;
    } catch (e) {
      print(e);
    }

    setState(() {
      _isLoading = false;
    });

  }

这是我的screenshot应用程序和progress_dialog包。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-23 17:38:05

只需要稍微调整一下:

代码语言:javascript
运行
复制
  Future _downloadAndSaveFileToStorage(String urlPath) async {

    final name = urlPdf.split('/').last;

    ProgressDialog pd = ProgressDialog(context: context);

    try{

      pd.show(
        max: 100,
        msg: 'Preparing Download...',
        progressType: ProgressType.valuable,
        backgroundColor: Color(0xff212121),
        progressValueColor: Color(0xff3550B4),
        progressBgColor: Colors.white70,
        msgColor: Colors.white,
        valueColor: Colors.white
      );

      final Directory _documentDir = Directory('/storage/emulated/0/MYDocuments/$name');
      await dio!.download(urlPath, _documentDir.path, onReceiveProgress: (rec, total){
        setState(() {
          _isLoading = true;
          int progress = (((rec / total) * 100).toInt());
          print(progress);
          pd.update(value: progress, msg: 'File Downloading');
        });
      });
       pd.close();
      _fileFullPath = _documentDir.path;
    } catch (e) {
      pd.close();
      print(e);
    }

    setState(() {
      _isLoading = false;
    });

  }
票数 0
EN

Stack Overflow用户

发布于 2021-11-23 16:24:44

如下所示:

代码语言:javascript
运行
复制
  Future _downloadAndSaveFileToStorage(String urlPath) async {

    final name = urlPdf.split('/').last;

    ProgressDialog pd = ProgressDialog(context: context);

    try{
      pd.show(max: 100, msg: 'Download file ...');

      final Directory _documentDir = Directory('/storage/emulated/0/MyDocuments/$name');
      await dio!.download(urlPath, _documentDir.path, onReceiveProgress: (rec, total){
        setState(() {
          _isLoading = true;
          progress = ((rec / total)*100).toStringAsFixed(0) + " %";
          print(progress);
          pd.update(progress);
        });
      });
       pd.close();
      _fileFullPath = _documentDir.path;
    } catch (e) {
      pd.close();
      print(e);
    }

    setState(() {
      _isLoading = false;
    });

  }

您可以在show方法中更改颜色或消息,如下所示:

代码语言:javascript
运行
复制
pd.show(
    max: 100,
    msg: 'Preparing Download...',
    progressType: ProgressType.valuable,
    backgroundColor: Color(0xff212121),
    progressValueColor: Color(0xff3550B4),
    progressBgColor: Colors.white70,
    msgColor: Colors.white,
    valueColor: Colors.white);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70084117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档