size property

Size size

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);
      if (RenderObject.debugActiveLayout != null &&
          !RenderObject.debugActiveLayout!.debugDoingThisLayoutWithCallback) {
        assert(
          debugDoingThisResize || debugDoingThisLayout || _computingThisDryLayout ||
            (RenderObject.debugActiveLayout == parent && size._canBeUsedByParent),
          '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().",
        );
      }
      assert(size == _size);
    }
    return true;
  }());
  return _size ?? (throw StateError('RenderBox was not laid out: $runtimeType#${shortHash(this)}'));
}
  1. @protected
void size=(Size value)

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

@protected
set size(Size value) {
  assert(!(debugDoingThisResize && debugDoingThisLayout));
  assert(sizedByParent || !debugDoingThisResize);
  assert(() {
    if ((sizedByParent && debugDoingThisResize) ||
        (!sizedByParent && debugDoingThisLayout)) {
      return true;
    }
    assert(!debugDoingThisResize);
    final List<DiagnosticsNode> information = <DiagnosticsNode>[
      ErrorSummary('RenderBox size setter called incorrectly.'),
    ];
    if (debugDoingThisLayout) {
      assert(sizedByParent);
      information.add(ErrorDescription('It appears that the size setter was called from performLayout().'));
    } else {
      information.add(ErrorDescription(
        'The size setter was called from outside layout (neither performResize() nor performLayout() were being run for this object).',
      ));
      if (owner != null && owner!.debugDoingLayout) {
        information.add(ErrorDescription('Only the object itself can set its size. It is a contract violation for other objects to set it.'));
      }
    }
    if (sizedByParent) {
      information.add(ErrorDescription('Because this RenderBox has sizedByParent set to true, it must set its size in performResize().'));
    } else {
      information.add(ErrorDescription('Because this RenderBox has sizedByParent set to false, it must set its size in performLayout().'));
    }
    throw FlutterError.fromParts(information);
  }());
  assert(() {
    value = debugAdoptSize(value);
    return true;
  }());
  _size = value;
  assert(() {
    debugAssertDoesMeetConstraints();
    return true;
  }());
}