useWidgetBounding function

  1. @Deprecated('Use KaeruWidget instead. This will be removed in a future version.')
UseWidgetBoundingReturn useWidgetBounding()

A hook that provides the bounding box of a widget.

Note: This is part of the deprecated defineWidget API.

Implementation

@Deprecated('Use KaeruWidget instead. This will be removed in a future version.')
UseWidgetBoundingReturn useWidgetBounding() {
  final key = GlobalKey();
  final bounding = $ref<WidgetBounding?>(null);

  WidgetsBinding.instance.addPostFrameCallback((_) {
    final context = key.currentContext;
    if (context == null) return;
    final renderBox = context.findRenderObject() as RenderBox?;
    if (renderBox == null || !renderBox.hasSize) return;

    final size = renderBox.size;
    final offset = renderBox.localToGlobal(Offset.zero);

    bounding.value = WidgetBounding(
      x: offset.dx,
      y: offset.dy,
      width: size.width,
      height: size.height,
    );
  });

  return (
    key: key,
    x: $computed(() => bounding.value?.x),
    y: $computed(() => bounding.value?.y),
    top: $computed(() => bounding.value?.top),
    left: $computed(() => bounding.value?.left),
    right: $computed(() => bounding.value?.right),
    bottom: $computed(() => bounding.value?.bottom),
    width: $computed(() => bounding.value?.width),
    height: $computed(() => bounding.value?.height),
  );
}