performLayout method

  1. @override
void performLayout(
  1. Size size
)
override

Override this method to lay out and position all children given this widget's size.

This method must call layoutChild for each child. It should also specify the final position of each child with positionChild.

Implementation

@override
void performLayout(Size size) {
  var cursor = Offset.zero;
  var tagSizes = <Size>[];
  //* Layout all the tags here
  for (final index in Iterable<int>.generate(length).toList()) {
    final tagId = getTagId(index);
    if (hasChild(getTagId(index))) {
      final childSize = layoutChild(
        tagId,
        BoxConstraints.loose(
          //* Let child specify it's own heigh so use infinity here
          Size(size.width, double.infinity),
        ),
      );

      //* Check if overflowing
      if (_isOverflow(
        childWidth: childSize.width,
        parentWidth: size.width,
        tagSizes: tagSizes,
        spacing: spacing,
      )) {
        //* Push the cursor down and back to the left
        cursor = Offset(0, (length > 0 ? cursor.dy : 0) + childSize.height);

        //* Reset the tagSizes for this roll
        tagSizes = <Size>[];
      }

      positionChild(tagId, cursor);
      // * Update cursor to the next position
      cursor = Offset(cursor.dx + childSize.width + spacing, cursor.dy);
      // * Push the size to tagSizes
      tagSizes.add(childSize);
    }
  }

  var textFieldSize = Size.zero;

  //* Layout the textbox
  if (hasChild(textFieldId)) {
    final currentRowWidth = tagSizes.fold<double>(0, (result, tag) {
      return result + tag.width;
    });
    final spacingWidth = spacing * max(tagSizes.length - 1, 0);
    final leftOverWidth = size.width - currentRowWidth - spacingWidth;
    final textWidth = max(leftOverWidth, minTextFieldWidth);
    //* Check if Textbox is overflowing
    //* Check if overflowing
    if (_isOverflow(
      childWidth: textWidth,
      parentWidth: size.width,
      tagSizes: tagSizes,
      spacing: spacing,
    )) {
      textFieldSize = layoutChild(
        textFieldId,
        BoxConstraints.loose(Size.fromWidth(size.width)),
      );
      //* Push the cursor down and back to the left
      cursor =
          Offset(0, (length > 0 ? (cursor.dy + textFieldSize.height) : 0));

      //* Reset the tagSizes for this roll
      tagSizes = <Size>[];
    } else {
      textFieldSize = layoutChild(
        textFieldId,
        BoxConstraints.loose(Size.fromWidth(textWidth)),
      );
    }
    positionChild(textFieldId, cursor);
  }

  //* Set parent height so that [TagsRenderLayoutBox] can use it to set the parentHeight
  parentSize =
      Size(size.width, (length > 0 ? cursor.dy : 0) + textFieldSize.height);
}