2

i want to make a download progress message inside the alert dialog but state not updating inside the the modal the is the download function using dio

downloadBook() async {
    Directory tempDir = await getTemporaryDirectory();
    var response = await dio.download(widget.bookModel.acf.bookLink,
        tempDir.path + '/books/' + widget.bookModel.title.rendered + '.epub',
        options: Options(
          responseType: ResponseType.bytes,
        ), onReceiveProgress: (actualbytes, totalbytes) {
      var percenatge = actualbytes / totalbytes * 100;
      _percentageBar = percenatge / 100;
      setState(() {
        downloadMessage = 'Downloading... ${percenatge.floor()} %';
      });
    });
  }

and this is the alert dialogue function

void _showDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: new Text("Alert!!"),
          content: new Text(downloadMessage),
          actions: <Widget>[
            new FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

and i made call like this inside the text button but the message stuck on 0% and not updated;

onPressed: () {
        // _showDialog(context);
        downloadBook();
        showDialog(
          context: context,
          builder: (context) {
            String contentText = "Content of Dialog";
            return StatefulBuilder(
              builder: (context, setState) {
                return AlertDialog(
                  title: new Text("Alert!!"),
                  content: new Text(downloadMessage),
                  actions: <Widget>[
                    new FlatButton(
                      child: new Text("OK"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                );
              },
            );
          },
        );
      },

FLASH TUBE
  • 33
  • 4

2 Answers2

-1

You can use

StatefullBuilder

widget top of your alert dialog and pass

setState

to it where you call it.

In this way you can access to State from outside.

alireza daryani
  • 787
  • 5
  • 16
-1

As I understood from your question, you are trying to have a different state for your alert dialog, so I will just update your code regarding the alertDialog and you can do what you want after that.

showDialog(
  context: context,
  builder: (context) {
    String contentText = "Content of Dialog";
    return StatefulBuilder(
      builder: (context, setState) {
        return AlertDialog(
          title: new Text("Alert!!"),
          content: new Text(downloadMessage),
          actions: <Widget>[
            new FlatButton(
              child: new Text("OK"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  },
);

So you can use setState for any progress you want inside.

Hassan Serhan
  • 392
  • 3
  • 13