在颤振中以编程方式关闭模态底板 [英] Close modal bottom sheet programmatically in flutter

查看:40
本文介绍了在颤振中以编程方式关闭模态底板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am displaying a BottomSheet via showModalBottomSheet<Null>() and inside several widgets with a GestureDetector. I would like to see the BottomSheet closed not only by touching outside it but also after an onTap event of a GestureDetector inside. However, it seems the GestureDetector is not forwarding the touch event.

所以我想知道,是否有一种方法可以通过编程方式触发ModalBottomSheet的关闭或一种告诉

So I am wondering, is there a way to trigger the closing of the ModalBottomSheet programmatically or a way to tell the GestureDetector to forward the touch event?

更新(2018-04-12):

按照代码段进行更好的理解。问题是,点击项目1或项目2时,ModalBottomSheet无法关闭。

Following a code snippet for better understanding. The problem is that the ModalBottomSheet isn't closing when tapping on "Item 1" or "Item 2".

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")
      ),
    ]),
  );
});


推荐答案

我不知道如何传递事件被GestureDetector捕获。但是,可以通过

I could not find out how to pass on an event caught by a GestureDetector. However, closing a ModalBottomSheet programmatically is done via

Navigator.pop(context);

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

So I just call that pop function inside the onTap callback function of the GestureDetector.

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

这篇关于在颤振中以编程方式关闭模态底板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆