【问题标题】:GestureDetector do not work when touch container empty space in Flutter在 Flutter 中触摸容器空白空间时,GestureDetector 不起作用
【发布时间】:2020-11-20 02:21:34
【问题描述】:

这是我在 Flutter 中定义的 GestureDetector:

return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onHorizontalDragStart: _onHorizontalDragStart,
          onHorizontalDragUpdate: _onHorizontalDragUpdate,
          onHorizontalDragEnd: _onHorizontalDragEnd,
          child:
          Container(
            color: Theme
                .of(context)
                .scaffoldBackgroundColor,
            child: Padding(
            padding: const EdgeInsets.all(
              16.0,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                InkWell(
                  onTap: () => launchUrl(item.link),
                  child: Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: Container(
                      child: Text(
                        item.title == "" ? "Comment" : item.title,
                        style: Theme
                            .of(context)
                            .textTheme
                            .headline5
                            .copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                ),
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8.0),
                    child: InkWell(
                        onTap: () async {
                          Channel channel = await Repo.fetchChannelItem(int.parse(item.subSourceId));
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) => ChannelPage(item:channel)),
                              //ProfilePage(username: item.author))
                        );
                      }, child: Text(
                      item.domain,
                      style: Theme
                          .of(context)
                          .textTheme
                          .caption,
                      )
                    ),
                  ),
                if (item.content != "")
                  
                if (item.parts.isNotEmpty)
                  
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    
                  ],
                ),
              ],
            ),
          ),
        ));
  }

此页面显示了一些文章内容,但有时文章很短,并在页面按钮中留出一些空间。在这种情况下,当拖动页面的空白区域时,GestureDetector 手势不起作用。我读过这个https://stackoverflow.com/questions/59062264/how-to-make-gesturedetector-also-work-when-touch-empty-space-in-flutter。并在 GestureDetector 中添加这一行:

behavior: HitTestBehavior.opaque,

还是不行,那么当拖动这个页面的空白处时,我应该怎么做才能使拖动事件起作用呢?现在我正在使用flutter Inspector 检查小部件,发现 GestureDetector 区域小于子子区域容器:

如何扩大GestureDetector效果区域?

【问题讨论】:

  • 如果你使用的是Android studio,你可以使用`flutter Inspector`来查看你的widget的所有位置。从那里你可以看到为什么它不工作。不确定其他 IDE

标签: flutter


【解决方案1】:

我想你可以试试另一个behavior: HitTestBehavior.translucent

【讨论】:

    【解决方案2】:

    我通过设置最小高度来扩展容器区域

     constraints: BoxConstraints(
         minHeight: MediaQuery.of(context).size.height*0.9,
     )
    

    【讨论】:

      猜你喜欢
      • 2020-03-22
      • 2016-04-20
      • 2023-01-25
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2016-04-22
      相关资源
      最近更新 更多