calculateFlexChildSizes method

Size calculateFlexChildSizes(
  1. ChildLayouter layoutChild, {
  2. required double freeSpace,
})

Implementation

Size calculateFlexChildSizes(ChildLayouter layoutChild,
    {required double freeSpace}) {
  var allocatedWidth = 0.0;
  var allocatedHeight = 0.0;

  // calculate total flex
  int totalFlex = 0;
  RenderBox? child = firstChild;
  RenderBox? lastFlexChild;
  while (child != null) {
    final BoxData data = child.parentData! as BoxData;
    final int flex = _getFlex(child);
    if (flex > 0) {
      totalFlex += flex;
      lastFlexChild = child;
    }
    child = data.nextSibling;
  }

  // layout flexible children
  if (totalFlex > 0) {
    // Distribute free space to flexible children.
    double flexSpaceUsed = 0.0;

    // layout flexible children
    final double spacePerFlex = freeSpace / totalFlex;
    child = firstChild;
    while (child != null) {
      // perform layout
      if (child.parentData is BoxData &&
          (child.parentData as BoxData).model != null) {
        var childModel = (child.parentData as BoxData).model!;

        //var idChild = childModel.id;

        final int flex = _getFlex(child);
        if (flex > 0) {
          var fit = _getFit(child);

          // get the childs max extent
          double maxExtent = (child == lastFlexChild)
              ? (freeSpace - flexSpaceUsed)
              : spacePerFlex * flex;

          // get the childs min extent
          // switch (fit)
          // {
          //   case FlexFit.tight:
          //     minExtent = maxExtent;
          //     break;
          //   case FlexFit.loose:
          //     minExtent = 0.0;
          //     break;
          // }

          // how much of the free space can I consume?
          var maxSpace = (flex / totalFlex) * freeSpace;
          if (maxExtent > maxSpace) {
            maxExtent = maxSpace;
          }

          // get child layout constraints
          var childConstraints =
              _getChildLayoutConstraints(child, childModel, maxExtent);

          // calculate the child's size by performing
          // a dry layout. We use LocalBoxConstraints in order to
          // override isTight, which is used in Layout() to determine if a
          // child size change forces a parent to resize.
          doLayout(child, childConstraints, layoutChild);

          // set used flex space
          switch (fit) {
            case FlexFit.tight:
              flexSpaceUsed = flexSpaceUsed + maxExtent;
              break;
            case FlexFit.loose:
              flexSpaceUsed = flexSpaceUsed +
                  min(
                      maxExtent,
                      (_direction == Axis.horizontal
                          ? child.size.width
                          : child.size.height));
              break;
          }

          // set width
          allocatedWidth = _direction == Axis.horizontal
              ? (allocatedWidth + child.size.width)
              : max(allocatedWidth, child.size.width);

          // set height
          allocatedHeight = _direction == Axis.horizontal
              ? max(allocatedHeight, child.size.height)
              : allocatedHeight + child.size.height;
        }
      }

      // get next child
      child = childAfter(child);
    }
  }

  return Size(allocatedWidth, allocatedHeight);
}