简体   繁体   中英

Use custom dialogs in Flutter FutureBuilder waiting

I have a request to the server, when I use FutureBuilder, the ConnectionState.waiting section, I want to use a custom dialog, but after ConnectionState.done, this dialog does not close, it does not even do Navigator.pop(context). enter image description here enter image description here

Try this

Before ConnectionState.waiting just call this onLoading(context);

then

if(ConnectionState. done){
  Navigator.pop();
}



void onLoading(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return Center(child:  CircularProgressIndicator(color: Colors.blue));
        });
  }

Unfortunately, it is not possible to disable Alert Dialog using object orientation, and I designed dialogs on a separate page, and Navigator.pop() does not work, but with this method, I achieved my desired result.

  FutureBuilder(
   future: _futureEditName,
   builder: (context,snapshot){
      if(snapshot.connectionState == ConnectionState.waiting){
          ShowWaitingDialog(context,true);

      }else if(snapshot.connectionState == ConnectionState.done && snapshot.hasData){
         ShowWaitingDialog(context,false)
      }
   }

);

show and dismiss Alert Dialog

ShowWaitingDialog(BuildContext context,bool show) {
showDialog(
  context: context,
  builder: (context) {
    return show == true ?
    AlertDialog(
      insetPadding: const EdgeInsets.all(20),
      alignment: Alignment.center,
      title: Container(
        width: double.maxFinite,
        child: Column(
          textDirection: TextDirection.rtl,
          children: [
            Row(
              textDirection: TextDirection.rtl,
              children: [
                const SpinKitCircle(
                  color: Colors.lightBlue,
                  size: 35,
                ),
                const SizedBox(width: 20,),
                Container(
                  alignment: Alignment.center,
                  child: const Text(
                    'please wait',
                    style: TextStyle(
                      color: kGraydark,
                      fontSize: 15,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    )
    : FutureBuilder(builder: (context, snapshot) {
      Navigator.of(context).pop();
      Navigator.of(context).pop();
      return Container();
    },);
},);

}

Navigator.of(context).pop() It must be called twice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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