RenderBox

351 阅读1分钟

size 方法

The size of this render box computed during layout. 在布局期间计算render box的size

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. 当object被标记为need layout,该值失效(过期)。在调用孩子的layout方法是,如果传递parentUsesSize为true的情况,可以读取size属性,否则不要读取。

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.

  Size get size {
    assert(hasSize, 'RenderBox was not laid out: ${toString()}');
    assert(() {
      final Size? _size = this._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 == this._size);
      }
      return true;
    }());
    return _size!;
  }