对话框在颤动中同时打开多个时间

时间:2019-09-05 05:42:55

标签: flutter dart dialog flutter-layout box

每当我在滚轮上单击多次时,将同时打开多个对话框。 我只是想,在解散之前应该打开它。

enter image description here

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

代码:

对话框

alertDialogBox(BuildContext context) {
    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>[
             ....
          ],
        ),
      );
    });
  }

方向盘:

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


                  }

                )

在尝试旋转车轮24小时后

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

2 个答案:

答案 0 :(得分:1)

这也许是因为,您试图在不必要的位置显示对话框异步。只需删除async,在显示简单对话框时就没有必要了。

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

答案 1 :(得分:0)

回答这个问题为时已晚,我遇到了相同的情况并已解决。

这是因为每次状态更改时,build方法都会调用alertDialogBox函数。您需要通过向“ isAlertboxOpened”之类的类中添加变量来限制它,并以有条件的方式打开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);
    }
  }
}