usedColumns static method

int usedColumns({
  1. int? numberOfColumns,
  2. double? width,
  3. int? columnSpace,
  4. double? minColumnSize,
})

Return number of columns to use for a given width or breakpoint

Implementation

static int usedColumns({
  int? numberOfColumns,
  double? width,
  int? columnSpace,
  double? minColumnSize,
}) {
  // It is bad style to modify passed in parameters, so we make local
  // copies further below that we modify as needed for validity checks.
  // Make sure numberOfColumns is valid and force it valid if it is not.
  // If it had an invalid input we just use 1 column as columns available.
  int effectiveColumns = numberOfColumns ?? 1;
  if (effectiveColumns < 1 || effectiveColumns > 24) effectiveColumns = 1;

  // If the columnSpace input is null or 1
  // AND the minColumnSize is invalid, then we return numberOfColumns
  // as available columns, basically we did not specify any method
  // for reducing the available columns, so we use all available columns at
  // the given breakpoint.
  if ((columnSpace == null || columnSpace == 1) &&
      (minColumnSize == null ||
          minColumnSize <= 137 / 2 ||
          minColumnSize > 8192)) return effectiveColumns;

  // If columnSpace input was invalid, we use 1
  int? usedColumnSpace = columnSpace;
  if (usedColumnSpace != null &&
      (usedColumnSpace < 1 || usedColumnSpace > 24)) {
    usedColumnSpace = 1;
  }

  // Init both usable column count calculation methods to max allowed columns
  int columnsViaColumnInterval = 24;
  int columnsViaMinSize = 24;

  if (usedColumnSpace != null) {
    // Calculate number of columns to use based on how many columns of the
    // available ones should be used. 1 means it takes up 1 of the available
    // ones, 2 uses two of them, etc.
    columnsViaColumnInterval =
        (effectiveColumns / usedColumnSpace).floor() < 1
            ? 1
            : (effectiveColumns / usedColumnSpace).floor();
  }

  // This is a bit ugly, but if passed width was null or smaller
  // than 50dp, we use 50 as input width. We will not allow creation of
  // columns that are smaller than 50dp wide.
  double? effectiveWidth = width;
  if (effectiveWidth == null || effectiveWidth < 50) effectiveWidth = 50;

  if (minColumnSize != null) {
    // Calculate the number of columns to use based on provided
    // minimum column width. This feature can be used to make columns of
    // any min size that just increases proportionally in amount as the
    // width of the screen increases.
    columnsViaMinSize = (effectiveWidth / minColumnSize).floor() < 1
        ? 1
        : (effectiveWidth / minColumnSize).floor();
  }
  // Both ways of providing desired number of columns should not really be
  // used at the same time, but if they were, we use the smaller calculated
  // column amount result, as the usable column count that is returned.
  if (columnsViaColumnInterval <= columnsViaMinSize) {
    return columnsViaColumnInterval;
  } else {
    return columnsViaMinSize;
  }
}