Home > OS >  How to stop the loader after 5 seconds
How to stop the loader after 5 seconds

Time:06-26

I'm trying to display the loader on whole screen on button click and after 5 seconds displaying a dialogue box an want to stop the loader. here is my code

          setState(() {
              _isLoading = true;
            });
            _isLoading
                ? showDialog(
                    context: context,
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return WillPopScope(
                        onWillPop: () async {
                          return false;
                        },
                        child: Center(
                          child: CircularProgressIndicator(
                            color: Apptheme.primaryColor,
                          ),
                        ),
                      );
                    })
                : null;
            await Future.delayed(const Duration(seconds: 5), () {
              AppDialogs()
                  .showInfoDialogue(context, "Sorry all drivers are busy!", () {
                Navigator.pop(context);
              });
            });
            setState(() {
              _isLoading = false;
            });

calling this code on button click, it shows the dialogue box after 5 seconds but not stop the loader. kindly help where i'm doing wrong.

CodePudding user response:

You can define thai cde in instate event

Future.delqyed(duration:Duration(seconds:5),(){showdialog((BuildContext context,Widget child)=>Dialog()); });

You can try this

CodePudding user response:

Once you call showDialog the loader is mounted on the screen and it will not disappear, if you have a simple setup calling .pop() on the navigator will remove your loader before you show the dialog. Modification required:

await Future.delayed(const Duration(seconds: 5), () {
    Navigator.of(context).pop();
    AppDialogs().showInfoDialogue(context, "Sorry all drivers are busy!", () {
      Navigator.pop(context);
    });
});
  • Related