Size size

The size of the RenderBox returned by findRenderObject.

This getter will only return a valid result after the layout phase is complete. It is therefore not valid to call this from the build function itself. It should only be called from paint callbacks or interaction event handlers (e.g. gesture callbacks).

For details on the different phases of a frame, see WidgetsBinding.beginFrame.

This getter will only return a valid result if findRenderObject actually returns a RenderBox. If findRenderObject returns a render object that is not a subtype of RenderBox (e.g., RenderView), this getter will throw an exception in checked mode and will return null in release mode.

Calling this getter is theoretically relatively expensive (O(N) in the depth of the tree), but in practice is usually cheap because the tree usually has many render objects and therefore the distance to the nearest render object is usually short.

Source

@override
Size get size {
  assert(() {
    if (_debugLifecycleState != _ElementLifecycle.active) {
      throw new FlutterError(
        'Cannot get size of inactive element.\n'
        'In order for an element to have a valid size, the element must be '
        'active, which means it is part of the tree. Instead, this element '
        'is in the $_debugLifecycleState state.\n'
        'The size getter was called for the following element:\n'
        '  $this\n'
      );
    }
    if (owner._debugBuilding) {
      throw new FlutterError(
        'Cannot get size during build.\n'
        'The size of this render object has not yet been determined because '
        'the framework is still in the process of building widgets, which '
        'means the render tree for this frame has not youet been determined. '
        'The size getter should only be called from paint callbacks or '
        'interaction event handlers (e.g. gesture callbacks).\n'
        '\n'
        'If you need some sizing information during build to decide which '
        'widgets to build, consider using a LayoutBuilder widget, which can '
        'tell you the layout constraints at a given location in the tree. See '
        '<https://docs.flutter.io/flutter/widgets/LayoutBuilder-class.html> '
        'for more details.\n'
        '\n'
        'The size getter was called for the following element:\n'
        '  $this\n'
      );
    }
    return true;
  });
  final RenderObject renderObject = findRenderObject();
  assert(() {
    if (renderObject == null) {
      throw new FlutterError(
        'Cannot get size without a render object.\n'
        'In order for an element to have a valid size, the element must have '
        'an assoicated render object. This element does not have an assoicated '
        'render object, which typically means that the size getter was called '
        'too early in the pipeline (e.g., during the build phase) before the '
        'framework has created the render tree.\n'
        'The size getter was called for the following element:\n'
        '  $this\n'
      );
    }
    if (renderObject is! RenderBox) {
      throw new FlutterError(
        'Cannot get size from a render object that is not a RenderBox.\n'
        'Instead of being a subtype of RenderBox, the render object associated '
        'with this element is a ${renderObject.runtimeType}. If this type of '
        'render object does have a size, consider calling findRenderObject '
        'and extracting its size manually.\n'
        'The size getter was called for the following element:\n'
        '  $this\n'
      );
    }
    final RenderBox box = renderObject;
    if (!box.hasSize || box.needsLayout) {
      throw new FlutterError(
        'Cannot get size from a render object that has not been through layout.\n'
        'The size of this render object has not yet been determined because '
        'this render object has not yet been through layout, which typically '
        'means that the size getter was called too early in the pipeline '
        '(e.g., during the build pahse) before the framework has determined '
        'the size and position of the render objects during layout.\n'
        'The size getter was called for the following element:\n'
        '  $this\n'
      );
    }
    return true;
  });
  if (renderObject is RenderBox)
    return renderObject.size;
  return null;
}