Flutter 该对话框在“颤振”中同时打开多次

Flutter 该对话框在“颤振”中同时打开多次,flutter,dart,dialog,flutter-layout,box,Flutter,Dart,Dialog,Flutter Layout,Box,每当我在控制盘上多次单击时,都会同时打开多个对话框。 我只是想,它应该在前一个被解雇后开放 我拍摄了一张图片,在上面添加了动画,并用GestureDetector小部件将其包装起来。 onTap:我调用了为对话框定义的alertDialogBox方法的事件。观看上面的gif图像,并调用具有特定条件的动画方法 代码: 对话框 24小时试着转动方向盘之后 一天持续时间: 这可能是因为,您正在尝试异步显示对话框,而不必这样做。只需删除async,在显示简单对话框时不需要它 最好创建一个在if条件下运行

每当我在控制盘上多次单击时,都会同时打开多个对话框。 我只是想,它应该在前一个被解雇后开放

我拍摄了一张图片,在上面添加了动画,并用GestureDetector小部件将其包装起来。 onTap:我调用了为对话框定义的alertDialogBox方法的事件。观看上面的gif图像,并调用具有特定条件的动画方法

代码:

对话框

24小时试着转动方向盘之后

一天持续时间:


这可能是因为,您正在尝试异步显示对话框,而不必这样做。只需删除async,在显示简单对话框时不需要它


最好创建一个在if条件下运行async的方法,并在onTap中删除async。这将用异步分隔对话框代码。

回答这个问题太晚了,我遇到了相同的情况并解决了它

这是因为每次状态更改时,构建方法都会调用alertDialogBox函数。您需要通过向类添加一个变量(如“IsAlertBoxOpen”)来限制它,并使alertDialogBox的打开有条件,避免打开多个对话框

下面的代码应该可以工作

class _MyHomePageState extends State<MyHomePage> {

  bool isAlertboxOpened; // add this

@override
void initState(){
  isAlertboxOpened = false; // add this
}

alertDialogBox(BuildContext context) async {
    setState(() => isAlertboxOpened = true); // add this
    return showDialog(
       barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            backgroundColor: Colors.transparent,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(16.0))),
            contentPadding: EdgeInsets.only(top: 10.0),
            content: Stack(
            children: <Widget>[
             ....
            // when press ok button on pressed add this
             onPressed:(){
                 // your code
                 setState(() => isAlertboxOpened = false);
                  Navigator.of(context).pop();
              }

          ],
        ),
      );
    });
  }

void oneDayDuration()async{
int differenceTime;

await({
   ....here fetching last spin date time from firestore});
   ....//here getting difference hours between last spining time and current time

    if(differenceTime>=24){

         await({......//updating ispin=0 to firbase
        })    
        .then((result)  => {


          print("Now you're able to spin"),

        }).catchError((err) => print(err)); 
    }else{
      print("Please wait for 24 hours");
      isAlertboxOpened ? (){} : // add this to make this conditional 
      alertDialogBox(context);
    }
  }
}

有人怎么能在不看代码的情况下提供帮助?我们应该如何在不看代码的情况下解决问题?现在,你可以看到,我已经添加了代码。你是对的,但我正在使用Cloud Firestore,这就是为什么我需要异步。我并不是简单地展示。您在对话框中使用Firestore查询,是吗?不,不,我在onTap:事件中使用云Firestore查询,条件是if{…}else{alertDialogBobx;}。请重新检查我的更新代码。仍然是相同的问题。等等,向您展示我的全部代码,我到底在做什么。请再次检查,我已更新了我的代码onTap:。
GestureDetector(
              child: Container(
                      alignment: Alignment.center,
                      color: Colors.blue,
                      child: new AnimatedBuilder(
                        animation: _animationCtrl,
                        child:  Container(
                          height:MediaQuery.of(context).size.height/2.3,
                          width: MediaQuery.of(context).size.width/1.3,
                          decoration: BoxDecoration(
                            color: Colors.blue,
                            image: DecorationImage(
                              image: AssetImage('assets/wheel.png', ),
                              fit: BoxFit.contain,
                            ),
                              borderRadius: BorderRadius.all(Radius.circular(130.0)),
                          )
                        ),
                        builder: (BuildContext context, Widget _widget) {
                            .......
                        },
                      ),
                    ),
                  onTap: ()async{
                     await Firestore.instance.collection('Users').document(uid).get().then((DocumentSnapshot documnet){
                      getIsSpin=documnet['isSpin'];


                    });

                    if(getIsSpin=="0"){
                         if (!_animationCtrl.isAnimating) {

                         //applying animation
                        }

                       DateTime now = DateTime.now();
                     // String lastSpinTime =DateFormat("yyyy-MM-dd hh:mm:ss").format(now);

                      .....//here updating isSpin value=1 and also updating   spining Date time to firestore

                   }else {
                     oneDayDuration();
                   }


                  }

                )

void oneDayDuration()async{
int differenceTime;

await({
   ....here fetching last spin date time from firestore});
   ....//here getting difference hours between last spining time and current time

    if(differenceTime>=24){

         await({......//updating ispin=0 to firbase
        })    
        .then((result)  => {


          print("Now you're able to spin"),

        }).catchError((err) => print(err)); 
    }else{
      print("Please wait for 24 hours");

      alertDialogBox(context);
    }
  }
}
class _MyHomePageState extends State<MyHomePage> {

  bool isAlertboxOpened; // add this

@override
void initState(){
  isAlertboxOpened = false; // add this
}

alertDialogBox(BuildContext context) async {
    setState(() => isAlertboxOpened = true); // add this
    return showDialog(
       barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            backgroundColor: Colors.transparent,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(16.0))),
            contentPadding: EdgeInsets.only(top: 10.0),
            content: Stack(
            children: <Widget>[
             ....
            // when press ok button on pressed add this
             onPressed:(){
                 // your code
                 setState(() => isAlertboxOpened = false);
                  Navigator.of(context).pop();
              }

          ],
        ),
      );
    });
  }

void oneDayDuration()async{
int differenceTime;

await({
   ....here fetching last spin date time from firestore});
   ....//here getting difference hours between last spining time and current time

    if(differenceTime>=24){

         await({......//updating ispin=0 to firbase
        })    
        .then((result)  => {


          print("Now you're able to spin"),

        }).catchError((err) => print(err)); 
    }else{
      print("Please wait for 24 hours");
      isAlertboxOpened ? (){} : // add this to make this conditional 
      alertDialogBox(context);
    }
  }
}