【问题标题】:Run a function AFTER that the alertbox has been dismissed在警报框被解除后运行一个函数
【发布时间】:2020-05-12 03:30:45
【问题描述】:

我已经阅读了无数链接,例如this one,但这并没有帮助。

用例很简单,我想在警报框被关闭后运行一个函数。

void dummyFunc()  {

            sleep(Duration(seconds:3));
            print("done");

}

Future<bool> displayDialog() async {
            return showDialog<bool>(

                context: context,
                barrierDismissible: false, // user must tap button!
                builder: (BuildContext context) {
                    return AlertDialog(

                        title: Text('AlertDialog Title'),
                        content: SingleChildScrollView(
                            child: ListBody(
                                children: <Widget>[
                                    Text('This is a demo alert dialog.'),
                                    Text('Would you like to approve of this message?'),
                                ],
                            ),
                        ),
                        actions: <Widget>[
                            FlatButton(
                                child: Text('Decline'),
                                onPressed: () {
                                    Navigator.of(context).pop(false);
                                },
                            ),
                            FlatButton(
                                child: Text('Approve'),
                                onPressed: () {
                                    Navigator.of(context).pop(true);
                                },
                            ),
                        ],
                        elevation: 24.0,
                        shape:RoundedRectangleBorder(),
                    );
                },
            );
        }

var button = AnimatedOpacity(
            opacity: 1.0,
            duration: Duration(milliseconds: 500),
            child: FloatingActionButton(
                tooltip: 'Start',
                child: Icon(iconShape),
                onPressed: () async {
                    bool shouldUpdate = await displayDialog();
                    print(shouldUpdate);
                    if (shouldUpdate)
                          dummyFunc();

                })
        );

但警报对话框在 3sd 后被关闭。

请告诉我我做错了什么,谢谢~

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    我认为这是因为您正在使用睡眠。而不是使用 Future 延迟。

    void dummyFunc() {
        print("done");
      }
    

    如果不想延迟,也可以去掉这个future,这个函数会在对话框关闭后执行。

    Sleep 将保持该过程,这就是您面临此错误的原因。

    【讨论】:

    • 虚拟函数只是一个更复杂函数的替代品,但我确实在动画之间使用了一些睡眠事件。根据您的消息,我尝试使用 Future.delayed 和 Timer,但它仍然无法按预期工作。
    • 只是删除未来,它会如你所料。如果您的进程不是异步的,那么您为什么要睡觉?您打印单个语句或编写任意数量的行长代码都没有关系。如果它不是在这种情况下异步,它将执行相同的操作。 @AntoninGAVREL
    • 问题是我睡了,但你的回答帮助我弄清楚
    • 这不是确切的答案,我赞成你的答案,因为我在正确的方向上轻推我;)如果有一个非常棒的答案来解释为什么睡眠会阻塞进程,我会接受其他人的答案警报框级别
    • 事实上我在我的代码中使用了一个循环,所以你的答案是最好的,谢谢 Viren!
    【解决方案2】:

    感谢 Viren 给了我很好的直觉,解决了这个问题,如果您不使用循环,Timer 也可以很好地工作:

    void dummyFunc()  {
                Timer(Duration(seconds: 3), () {
    
                        print("done");
                    });
    }
    

    编辑:实际上,Viren 的答案与loops! 一起使用效果更好,这也可以。只是避免睡觉。花了 3 个小时,现在我讨厌 sleep()。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2023-03-19
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多