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

file downloader app with package sn progress dialog

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

  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 app with progress_dialog 包裹。

就这样:

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

  }

您可以像这样在显示方法中更改颜色或消息:

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

只需要一点点调整:

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

  }