performLayout method

  1. @override
void performLayout()

Implementation

@override
void performLayout() {
  BoxConstraints constraints = this.constraints;
  double maxWidth = constraints.maxWidth;
  double maxHeight = constraints.maxHeight;
  contentSize = Size(maxWidth, maxHeight);

  RenderBox? actions = childForSlot(AppBarItem.actions);
  RenderBox? title = childForSlot(AppBarItem.title);
  RenderBox? backButton = childForSlot(AppBarItem.backButton);

  BoxConstraints remaingConstraints = constraints.copyWith(minHeight: 0);

  ///
  /// Back Button
  ///
  Size backButtonSize = Size.zero;
  BoxParentData? backButtonParentData;

  if (backButton != null) {
    backButton.layout(remaingConstraints, parentUsesSize: true);
    backButtonSize = backButton.size;

    final backButtonOffset =
        Offset(0, centerVertically(maxHeight, backButtonSize));

    backButtonParentData = backButton.parentData! as BoxParentData;
    backButtonParentData.offset = backButtonOffset;
    remaingConstraints = remaingConstraints.copyWith(
      maxWidth: remaingConstraints.maxWidth - backButtonSize.width,
    );
  }

  ///
  /// Actions
  ///
  Size actionSize = Size.zero;
  BoxParentData? actionParentData;
  if (actions != null) {
    actions.layout(remaingConstraints, parentUsesSize: true);
    actionSize = actions.size;
    final offset_action = Offset(
      maxWidth - actionSize.width,
      centerVertically(maxHeight, actionSize),
    );
    actionParentData = actions.parentData! as BoxParentData;
    actionParentData.offset = offset_action;
    remaingConstraints = remaingConstraints.copyWith(
      maxWidth: remaingConstraints.maxWidth - actionSize.width,
    );
  }

  ///
  /// Title
  ///
  final biggestIndent = backButtonSize.width >= actionSize.width
      ? backButtonSize.width
      : actionSize.width;

  final titleConstraints = BoxConstraints(
    maxWidth: maxWidth - biggestIndent * 2,
    maxHeight: remaingConstraints.maxHeight,
  );
  Size titleSize = Size.zero;
  BoxParentData? titleParentData;
  if (title != null) {
    title.layout(titleConstraints, parentUsesSize: true);
    titleSize = title.size;
    final offset_title = Offset(
      ((titleConstraints.maxWidth - titleSize.width) / 2) + biggestIndent,
      centerVertically(maxHeight, titleSize),
    );
    titleParentData = title.parentData! as BoxParentData;
    titleParentData.offset = offset_title;
  }

  /// Find the biggest height
  final biggestHeight =
      max(backButtonSize.height, max(titleSize.height, actionSize.height));

  if (maxHeight == double.infinity) {
    maxHeight = biggestHeight;
    contentSize = Size(maxWidth, biggestHeight);

    if (backButtonParentData != null) {
      backButtonParentData.offset = Offset(
        backButtonParentData.offset.dx,
        centerVertically(maxHeight, backButtonSize),
      );
    }
    if (actionParentData != null) {
      actionParentData.offset = Offset(
        actionParentData.offset.dx,
        centerVertically(maxHeight, actionSize),
      );
    }
    if (titleParentData != null) {
      titleParentData.offset = Offset(
        titleParentData.offset.dx,
        centerVertically(maxHeight, titleSize),
      );
    }
  }

  size = contentSize;
}