如何在颤振中从模态传回数据?

dar*_*tar 5 dart flutter

所以基本上我使用showModalBottomSheet小部件来显示一个全屏容器,该容器有一个GestureDetector运行此 onTap 的容器:

onTap: () {
  final String testText = "Sup";
  Navigator.of(context).pop(testText);
}
Run Code Online (Sandbox Code Playgroud)

await当我调用结果时,这显然会返回文本showModalBottomSheet,但是,我还想进行设置enableDrag: true,以便我们可以将模式滑开。

我的问题是:

当滑动关闭时,如何传递参数/结果?使用函数,我可以简单地做到这一点Navigator.of(context).pop(...),但是当我们滑动时,没有函数,因此当我们滑动关闭时我无法找到传递参数的方法。

谢谢你!

Mr.*_*t88 0

当您滑动时, pop() 方法被调用,无论如何都无法覆盖它,但我找到了一种处理这种情况的方法:

您可以在 showModalBottomSheet() 上使用 then() 方法,如下所示:

showModalBottomSheet(context: context, builder: (context) => SecondPage()).then((value) {
        str = "done";
        print("data: $str");
      });
Run Code Online (Sandbox Code Playgroud)

请记住,future 返回的值是 pop() 方法返回的值,否则为 null。