Flutter : 关于 HitTestBehavior

/// How to behave during hit tests.
enum HitTestBehavior {
  /// Targets that defer to their children receive events within their bounds
  /// only if one of their children is hit by the hit test.
  deferToChild,

  /// Opaque targets can be hit by hit tests, causing them to both receive
  /// events within their bounds and prevent targets visually behind them from
  /// also receiving events.
  opaque,

  /// Translucent targets both receive events within their bounds and permit
  /// targets visually behind them to also receive events.
  translucent,
}

HitTestBehavior.deferToChild是默认的配置

例子

当给 Container设置颜色的时候,点击有响应。而去掉颜色,则点击无响应。

 GestureDetector(
                  onTap: () {
                    print("onTap");
                  },
                  child: Container(
                    color: Colors.blue,
                    width: 40,
                    height: 40,
                  ),
                ),

Container的源码里可以看到,如果 color != null,那么会给一个 ColoredBox

// Container.dart
 if (color != null)
      current = ColoredBox(color: color!, child: current);

ColoredBox里的 createRenderObject_RenderColoredBox所创建的:

// ColoredBox
  @override
  _RenderColoredBox createRenderObject(BuildContext context) {
    return _RenderColoredBox(color: color);
  }

由于 RenderColoredBox继承自 RenderProxyBoxWithHitTestBehavior, 在 RenderColoredBox构造方法里,会默认传入 HitTestBehavior.opaque,使其可以让自己命中点击。

class _RenderColoredBox extends RenderProxyBoxWithHitTestBehavior {
  _RenderColoredBox({ required Color color })
    : _color = color,
      super(behavior: HitTestBehavior.opaque);
  ...
}
abstract class RenderProxyBoxWithHitTestBehavior extends RenderProxyBox {
  RenderProxyBoxWithHitTestBehavior({
    this.behavior = HitTestBehavior.deferToChild,
    RenderBox? child,
  }) : super(child);

  /// How to behave during hit testing.
  HitTestBehavior behavior;
  ...

  @override
  bool hitTestSelf(Offset position) => behavior == HitTestBehavior.opaque;

  ...
}

而如果没有给颜色的话,会给的是 ConstrainedBox,而它里面没有对点击事件做判断的地方,故点击无响应。

 if (constraints != null)
      current = ConstrainedBox(constraints: constraints!, child: current);

因此我们针对这种情况就直接对GestureDetector的behavior赋予HitTestBehavior.opaque,就能使其点击事件有响应,因为GestureDetector的源码一路跟下去,它的实现里也有RenderProxyBoxWithHitTestBehavior

如果我们给Container去掉color属性,赋予一个空的BoxDecoration

 GestureDetector(
                  onTap: () {
                    print("onTap");
                  },
                  child: Container(
                   decoration:BoxDecoration(),
                    width: 40,
                    height: 40,
                  ),
                ),

它是可以响应点击事件的,因为会给一个DecoratedBox

if (decoration != null)
      current = DecoratedBox(decoration: decoration!, child: current);

DecoratedBox的内部实现里有 RenderDecoratedBox,这里有对hitTest的处理:

class RenderDecoratedBox extends RenderProxyBox {
   ...
  @override
  bool hitTestSelf(Offset position) {
    return _decoration.hitTest(size, position, textDirection: configuration.textDirection);
  }
  ...
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值