performLayout method

  1. @override
void performLayout()
override

Do the work of computing the layout for this render object.

Do not call this function directly: call layout instead. This function is called by layout when there is actually work to be done by this render object during layout. The layout constraints provided by your parent are available via the constraints getter.

If sizedByParent is true, then this function should not actually change the dimensions of this render object. Instead, that work should be done by performResize. If sizedByParent is false, then this function should both change the dimensions of this render object and instruct its children to layout.

In implementing this function, you must call layout on each of your children, passing true for parentUsesSize if your layout information is dependent on your child's layout information. Passing true for parentUsesSize ensures that this render object will undergo layout if the child undergoes layout. Otherwise, the child can change its layout information without informing this render object.

Some special RenderObject subclasses (such as the one used by OverlayPortal.overlayChildLayoutBuilder) call applyPaintTransform in their performLayout implementation. To ensure such RenderObjects get the up-to-date paint transform, RenderObject subclasses should typically update the paint transform (as reported by applyPaintTransform) in this method instead of paint.

Implementation

@override
void performLayout() {
  size = constraints.constrain(Size(globeSize, globeSize));

  final int count = _childCount;
  _ensurePoints(count);

  final BoxConstraints childConstraints = constraints.loosen();
  final double radius =
      globeSize / 2.0; // use half-size so items sit within bounds
  final double rotationXRad = _degToRad(rotationXDegrees);
  final double rotationYRad = _degToRad(rotationYDegrees);
  final Offset center = Offset(size.width / 2, size.height / 2);

  int index = 0;
  RenderBox? child = firstChild;
  while (child != null) {
    final SphereParentData pd = child.parentData! as SphereParentData;
    child.layout(childConstraints, parentUsesSize: true);

    final _SpherePoint sp = _points[index];

    // initial spherical to Cartesian coordinates
    double x = radius * math.cos(sp.phi) * math.cos(sp.theta);
    double y = radius * math.sin(sp.phi);
    double z = radius * math.cos(sp.phi) * math.sin(sp.theta);

    // --- Apply vertical rotation (X-axis rotation) ---
    double cosY = math.cos(rotationYRad);
    double sinY = math.sin(rotationYRad);
    double y1 = y * cosY - z * sinY;
    double z1 = y * sinY + z * cosY;

    // --- Apply horizontal rotation (Y-axis rotation) ---
    double cosX = math.cos(rotationXRad);
    double sinX = math.sin(rotationXRad);
    double x1 = x * cosX + z1 * sinX;
    double z2 = -x * sinX + z1 * cosX;

    // perspective scale (depth)
    final double scale = 0.3 + 0.7 * ((z2 / radius + 1.0) / 2.0);
    final double depth = math.cos(scale);
    final double finalScale = (scale * 1.95) - depth;
    final double alpha = alphaEnabled
        ? ((scale * 2.0) - 0.98).clamp(0.0, 1.0)
        : 1.0;

    final Offset childCenter = center + Offset(x1, y1);
    final Size scaledChildSize = Size(
      child.size.width * finalScale,
      child.size.height * finalScale,
    );
    final Offset temp =
        childCenter -
        Offset(scaledChildSize.width / 2, scaledChildSize.height / 2);
    final topLeft = Offset(
      temp.dx < 0 ? 0 : temp.dx,
      temp.dy < 0 ? 0 : temp.dy,
    );
    // assert(topLeft.dx >= 0 && topLeft.dy >= 0, '$child is violating');
    pd.topLeft = topLeft;
    pd.scale = finalScale;
    pd.alpha = alpha;
    pd.z = z2;

    child = pd.nextSibling;
    index++;
  }

  // no extra size changes
}