flutter判断是否超出可视范围
作者:yangyxd 来源:www.zhihu.com 发布时间:2021-06-29 13:26
方法1:
visibility_detector | Flutter Package (pub.dev)
方法2:
获得目标RenderOject
context.findRenderObject();
获取 renderObject 跟Viewport的距离:
RenderAbstractViewport viewport = RenderAbstractViewport.of(renderObject);
然后使用 viewport 的 getOffsetToReveal API,可以判断出Widget与父Widget可视区域的相对位置。
然后通下面的方法,可以判断出是否可视:
Matrix4 getTransformTo(RenderObject ancestor) {
assert(attached);
if (ancestor == null) {
final AbstractNode rootNode = owner.rootNode;
if (rootNode is RenderObject)
ancestor = rootNode;
}
final List<RenderObject> renderers = <RenderObject>[];
for (RenderObject renderer = this; renderer != ancestor; renderer = renderer.parent) {
assert(renderer != null); // Failed to find ancestor in parent chain.
renderers.add(renderer);
}
final Matrix4 transform = Matrix4.identity();
for (int index = renderers.length - 1; index > 0; index -= 1)
renderers[index].applyPaintTransform(renderers[index - 1], transform);
return transform;
}
Offset localToGlobal(Offset point, { RenderObject ancestor }) {
return MatrixUtils.transformPoint(getTransformTo(ancestor), point);
}
final Offset position = child.localToGlobal(Offset.zero, ancestor: parent);
bool childIsActivedInViewport = ((position.dx - size.dx).abs() < 1 &&
(position.dy - size.dy).abs() < 1);
更新时间:2024-12-18 20:26