fragmentOf method

Creates a HeroContainerFragment from a widget key.

Captures the widget's image, global position, and size to create a fragment that can be used for transition animations.

Implementation

Future<HeroContainerFragment> fragmentOf(
  BuildContext context,
  GlobalKey key,
) async {
  final ui.Image? capturedImage = await captureWidget(context, key);

  final RenderBox renderBox =
      key.currentContext?.findRenderObject() as RenderBox;
  final Offset globalPosition = renderBox.localToGlobal(Offset.zero);
  final Size intrinsicSize = renderBox.size;

  return HeroContainerFragment(
    capturedImage: capturedImage!,
    offset: globalPosition,
    size: intrinsicSize,
  );
}