getResponsiveGridListItems method Null safety

List<Widget> getResponsiveGridListItems(
  1. double maxWidth
)

Method to generate a list of ResponsiveGridRow's with spacing in between them.

maxWidth is the maximum width of the current layout.

Implementation

List<Widget> getResponsiveGridListItems(double maxWidth) {
  // Set the currentWidth to minItemWidth and itemsPerRow to one
  // since this is the minimum that is needed per row.
  var currentWidth = minItemWidth;
  var itemsPerRow = 1;

  // Calculate with outer margin (vertical) if set
  if (horizontalGridMargin != null) {
    currentWidth += 2 * horizontalGridMargin!;
  }

  // While another pair of spacing + minItemWidth fits the row, add it to
  // the variables
  while (currentWidth < maxWidth) {
    if (currentWidth + (minItemWidth + horizontalGridSpacing) <= maxWidth) {
      // If another spacing + item fits in the row, add one item to the row
      // and update the currentWidth
      currentWidth += (minItemWidth + horizontalGridSpacing);
      itemsPerRow++;
    } else {
      // If no other item + spacer fits into the row, break
      break;
    }
  }

  // Calculate the spacers per row (they are only in between the items, not
  // at the edges)
  var spacePerRow = itemsPerRow - 1;

  // Calculate the itemWidth that results from the maxWidth and number of
  // spacers and outer margin (horizontal)
  var itemWidth = (maxWidth -
          (spacePerRow * horizontalGridSpacing) -
          (2 * (horizontalGridMargin ?? 0))) /
      itemsPerRow;

  // Partition the items into groups of itemsPerRow length and map them
  // to ResponsiveGridRow's
  var items = partition(children, itemsPerRow)
      .map<Widget>(
        (e) => ResponsiveGridRow(
          rowItems: e,
          spacing: horizontalGridSpacing,
          horizontalGridMargin: horizontalGridMargin,
          itemWidth: itemWidth,
        ),
      )
      .toList();

  // Join the rows width spacing in between them (vertical)
  var responsiveGridListItems =
      genericJoin<Widget>(items, SizedBox(height: verticalGridSpacing));

  // Add outer margin (vertical) if set
  if (verticalGridMargin != null) {
    return [
      SizedBox(height: verticalGridMargin),
      ...responsiveGridListItems,
      SizedBox(height: verticalGridMargin)
    ];
  }

  return responsiveGridListItems;
}