我想在小部件的特定部分进行触摸,以使下面的小部件能够处理手势。 IgnorePointer在整个窗口小部件上执行该操作。是否可以部分进行?
在iOS中,有UIView.point(inside:with:)
我创建了自己的小部件。hitTest方法可以决定是否允许触摸。如果要特定的小部件允许点,请使用GlobalKey对象作为可触摸小部件的键,并使用该全局键来获取上下文和渲染对象。使用渲染对象并将类型设置为RenderBox。使用我编写的扩展名并调用rectFromAncestor以获取rect和调用包含的内容以检查是否指向窗口小部件内。
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
class RenderIgnorePosition extends RenderProxyBox {
/// Creates a render object that is invisible to custom hit testing.
RenderIgnorePosition({
RenderBox child,
@required bool Function(BoxHitTestResult, {Offset position}) hitTest,
}) : _hitTest = hitTest,
super(child);
final bool Function(BoxHitTestResult, {Offset position}) _hitTest;
@override
bool hitTest(BoxHitTestResult result, {Offset position}) {
return _hitTest(result, position: position) &&
super.hitTest(result, position: position);
}
}
class IgnorePosition extends SingleChildRenderObjectWidget {
/// Creates a widget that is invisible to hit testing.
const IgnorePosition({
Key key,
@required this.hitTest,
@required Widget child,
}) : assert(hitTest != null),
super(key: key, child: child);
final bool Function(BoxHitTestResult, {Offset position}) hitTest;
@override
RenderIgnorePosition createRenderObject(BuildContext context) {
return RenderIgnorePosition(
hitTest: hitTest,
);
}
}
extension RenderBoxExtension on RenderBox {
Rect rectFromAncestor(RenderObject ancestor) {
final offset = localToGlobal(Offset.zero, ancestor: ancestor);
return RRect.fromLTWH(
offset.dx,
offset.dy,
size.width,
size.height,
);
}
}