Widget size measurement

Measure a widget after layout with MeasureSize.

Widget size measurement

Use MeasureSize when you need a widget’s rendered size after layout (for example, to position an overlay or report sizes to another system).

Use MeasureSize

SuperDeck exports MeasureSize from package:superdeck/superdeck.dart.

import 'package:flutter/widgets.dart';
import 'package:superdeck/superdeck.dart';

class ResizablePanel extends StatefulWidget {
  const ResizablePanel({super.key});

  @override
  State<ResizablePanel> createState() => _ResizablePanelState();
}

class _ResizablePanelState extends State<ResizablePanel> {
  Size _size = Size.zero;
  BoxConstraints _constraints = const BoxConstraints();

  @override
  Widget build(BuildContext context) {
    return MeasureSize(
      onChange: (size, parentConstraints) {
        setState(() {
          _size = size;
          _constraints = parentConstraints;
        });
      },
      child: Text('size=$_size constraints=$_constraints'),
    );
  }
}

Notes:

  • onChange runs in a post-frame callback, so it is safe to call setState.
  • parentConstraints are the constraints applied to MeasureSize by its parent.

Prefer built-in layout tools

Before you measure, consider:

  • LayoutBuilder (constraints during build)
  • SizeChangedLayoutNotifier (change notifications)
  • CompositedTransformTarget + CompositedTransformFollower (anchored overlays)
  • AnimatedSize (animate size changes)
  • CustomSingleChildLayout / CustomMultiChildLayout (custom layout)