简体   繁体   English

带有 package sn 进度对话框的文件下载器应用程序

[英]file downloader app with package sn progress dialog

How to change the progress_dialog package to the sn_progress_dialog package?如何将progress_dialog package 更改为sn_progress_dialog package? I'm trying to make a file downloader app with a progress dialog, but the progress_dialog package is not null safety.我正在尝试制作带有进度对话框的文件下载器应用程序,但 progress_dialog package 不是 null 安全的。

  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;
    });

  }

And this is my screenshot app with progress_dialog package.这是我的屏幕截图应用程序,带有progress_dialog package。

Just do like this:这样做:

  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;
    });

  }

and you can change color or message in show method like this:您可以在 show 方法中更改颜色或消息,如下所示:

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);

just need a little tweaking:只需要一点点调整:

  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;
    });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM