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.

Implementation

@override
void performLayout() {
  final constraints = this.constraints;
  final maxWidth = constraints.maxWidth;

  final text = childForSlot(TextLayoutItem.text)!;
  final tralling = childForSlot(TextLayoutItem.trailling);
  final leading = childForSlot(TextLayoutItem.leading);
  final placeHolder = childForSlot(TextLayoutItem.placeHolder);

  final intrinsicSizes = {
    TextLayoutItem.leading: findIntrinsicSize(leading, maxWidth),
    TextLayoutItem.trailling: findIntrinsicSize(tralling, maxWidth),
    TextLayoutItem.placeHolder: findIntrinsicSize(placeHolder, maxWidth),
  };
  intrinsicSizes[TextLayoutItem.text] = findIntrinsicSize(
    text,
    maxWidth -
        intrinsicSizes[TextLayoutItem.trailling]!.width -
        intrinsicSizes[TextLayoutItem.leading]!.width,
  );

  var maxHeight = constraints.maxHeight == double.infinity
      ? findMaxHeight(intrinsicSizes)
      : constraints.maxHeight;
  final textMaxHeight = maxHeight;
  final textInset = placeHolderTitleHeight?.height ?? 0;
  if (placeHolderTitleHeight != null &&
      constraints.maxHeight == double.infinity) {
    maxHeight += placeHolderTitleHeight!.height;
  }

  ///
  /// Leading
  ///
  final leadingSize = intrinsicSizes[TextLayoutItem.leading] ?? Size.zero;
  if (leading != null) {
    final backButtonOffset =
        Offset(0, centerVertically(maxHeight, leadingSize));
    (leading.parentData! as BoxParentData).offset = backButtonOffset;
  }

  ///
  /// Trailling
  ///
  final traillingSize = intrinsicSizes[TextLayoutItem.trailling] ?? Size.zero;
  if (tralling != null) {
    final backButtonOffset = Offset(
      maxWidth - traillingSize.width,
      centerVertically(maxHeight, traillingSize),
    );
    (tralling.parentData! as BoxParentData).offset = backButtonOffset;
  }

  ///
  /// Text
  ///
  var textSize = intrinsicSizes[TextLayoutItem.text]!;
  text.layout(
    BoxConstraints(
      maxWidth: maxWidth - leadingSize.width - traillingSize.width,
      maxHeight: textMaxHeight,
    ),
    parentUsesSize: true,
  );
  textSize = text.size;
  final textOffset = Offset(
    leadingSize.width,
    textInset + centerVertically(textMaxHeight, textSize),
  );
  (text.parentData! as BoxParentData).offset = textOffset;

  ///
  /// Placeholder
  ///
  if (placeHolder != null) {
    final placeHolderSize =
        intrinsicSizes[TextLayoutItem.placeHolder] ?? Size.zero;
    final topInset = (placeHolderTitleHeight != null ? animation : 1) *
        centerVertically(maxHeight, placeHolderSize);
    placeHolder.layout(constraints);

    final dx = switch (textAlign) {
      TextAlign.start ||
      TextAlign.left ||
      TextAlign.justify =>
        leadingSize.width,
      TextAlign.end ||
      TextAlign.right =>
        maxWidth - traillingSize.width - placeHolderSize.width,
      TextAlign.center => (maxWidth - placeHolderSize.width) / 2,
    };

    final placeHolderOffset = Offset(dx, topInset);
    (placeHolder.parentData! as BoxParentData).offset = placeHolderOffset;
  }

  size = Size(maxWidth, maxHeight);
  contentSize = size;
}