如何在flutter中获取showModalBottomSheet的onDismiss回调?

时间:2020-03-22 14:46:07

标签: flutter dart flutter-layout

我有一个showModalBottomSheet,并且isDismissible设置为true,当我在showModalBottomSheet之外单击时,我想接收它的回调。

在showModalBottomSheet中,我有隐藏按钮,单击隐藏按钮后,我正在Navigator.pop(context)来隐藏对话框,

尝试了whenComplete()then(),但每次隐藏按钮时我都会得到回调。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

今天,

弹出路径时可以传递参数,以查看如何关闭模式表:

                showModalBottomSheet<bool>(
                  context: context,
                  isDismissible: true,
                  builder: (BuildContext context) {
                    return Center(
                      child: RaisedButton(
                        child: const Text("hide"),
                        onPressed: () => Navigator.of(context).pop(true), // pass true indicating that it was hidden via button
                      ),
                    );
                  },
                ).then(
                  (isManuallyHidden) {
                    if (isManuallyHidden ?? false) {
                      print("hidden via button");
                    } else {
                      print("dismissed");
                    }
                  },
                );