size property

Size size
inherited

The size of this render box computed during layout.

This value is stale whenever this object is marked as needing layout. During performLayout, do not read the size of a child unless you pass true for parentUsesSize when calling the child's layout function.

The size of a box should be set only during the box's performLayout or performResize functions. If you wish to change the size of a box outside of those functions, call markNeedsLayout instead to schedule a layout of the box.

Implementation

Size get size {
  assert(hasSize, 'RenderBox was not laid out: $this');
  assert(() {
    final Size? size = _size;
    if (size is _DebugSize) {
      assert(size._owner == this);
      final RenderObject? parent = this.parent;
      // Whether the size getter is accessed during layout (but not in a
      // layout callback).
      final bool doingRegularLayout = !(RenderObject.debugActiveLayout?.debugDoingThisLayoutWithCallback ?? true);
      final bool sizeAccessAllowed = !doingRegularLayout
        || debugDoingThisResize
        || debugDoingThisLayout
        || _computingThisDryLayout
        || RenderObject.debugActiveLayout == parent && size._canBeUsedByParent;
      assert(sizeAccessAllowed,
        'RenderBox.size accessed beyond the scope of resize, layout, or '
        'permitted parent access. RenderBox can always access its own size, '
        'otherwise, the only object that is allowed to read RenderBox.size '
        'is its parent, if they have said they will. It you hit this assert '
        'trying to access a child\'s size, pass "parentUsesSize: true" to '
        "that child's layout() in ${objectRuntimeType(this, 'RenderBox')}.performLayout.",
      );
      final RenderBox? renderBoxDoingDryBaseline = _computingThisDryBaseline
        ? this
        : (parent is RenderBox && parent._computingThisDryBaseline ? parent : null);
      assert(renderBoxDoingDryBaseline == null,
        'RenderBox.size accessed in '
        '${objectRuntimeType(renderBoxDoingDryBaseline, 'RenderBox')}.computeDryBaseline.'
        'The computeDryBaseline method must not access '
        '${renderBoxDoingDryBaseline == this ? "the RenderBox's own size" : "the size of its child"},'
        "because it's established in peformLayout or peformResize using different BoxConstraints."
      );
      assert(size == _size);
    }
    return true;
  }());
  return _size ?? (throw StateError('RenderBox was not laid out: $runtimeType#${shortHash(this)}'));
}
  1. @override
void size=(Size value)
override

Setting the size, in debug mode, triggers some analysis of the render box, as implemented by debugAssertDoesMeetConstraints, including calling the intrinsic sizing methods and checking that they meet certain invariants.

Implementation

@override
set size(Size value) {
  _boxSize = value;
  super.size = value;
}