Home > Net >  file downloader app with package sn progress dialog
file downloader app with package sn progress dialog

Time:11-24

How to change the progress_dialog package to the 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.

  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.

CodePudding user response:

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 :

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

CodePudding user response:

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

  }
  • Related