dart - 在 flutter 中以编程方式关闭模态底部表

我正在通过 showModalBottomSheet<Null>() 显示 BottomSheet并在几个带有 GestureDetector 的小部件内。 我希望看到 BottomSheet 不仅通过触摸它的外部而且在内部的 GestureDetector 的 onTap 事件之后关闭。但是,GestureDetector 似乎没有转发触摸事件。

所以我想知道,有没有办法以编程方式触发 ModalBottomSheet 的关闭或告诉 GestureDetector 转发触摸事件的方法?

更新(2018-04-12):

为了更好地理解代码片段。问题是当点击“Item 1”或“Item 2”时,ModalBottomSheet 没有关闭。

showModalBottomSheet<Null>(context: context, builder: (BuildContext context)
{
  return new SingleChildScrollView(child:
    new Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
      new GestureDetector(onTap: () { doSomething(); }, child:
        new Text("Item 1")
      ),
      new GestureDetector(onTap: () { doSomething(); }, child:
        new Text("Item 2")
      ),
    ]),
  );
});

最佳答案

以编程方式关闭 ModalBottomSheet 是通过

Navigator.pop(context);

所以我只是在 GestureDetector 的 onTap 回调函数中调用 pop 函数。

showModalBottomSheet(context: context, builder: (BuildContext context)
{
  return SingleChildScrollView(child:
    Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
      GestureDetector(onTap: () {
          Navigator.pop(context);
          doSomething();
        }, child:
        Text("Item 1")
      ),
      GestureDetector(onTap: () {
          Navigator.pop(context);
          doSomething();
        }, child:
        Text("Item 2")
      ),
    ]),
  );
});

https://stackoverflow.com/questions/49778866/

相关文章:

dart - Flutter - 在路由之间推送和获取值(value)

flutter - 如何在 flutter 中制作圆形 TextField?

android - 如何在单击推送通知时打开特定屏幕以进行 flutter

dart - 如何在 ListView 内 flutter 中创建一行可滚动的文本框或小部件?

dart - 有没有办法在 InitState 方法上加载异步数据?

firebase - Flutter 和 google_sign_in 插件 : PlatformE

json - 如何在 flutter 中反序列化来自json的对象列表

flutter - 如何检查滚动位置是在 ListView 的顶部还是底部?

firebase - flutter 和 Firebase : Compression before

dart - Flutter 图像预加载