updateMiniCardsGrid function

Future<void> updateMiniCardsGrid(
  1. UpdateMiniCardsGridOptions options
)

Updates the mini cards grid layout based on the specified configuration.

This function calculates the dimensions of each grid cell and updates either the main grid or an alternative grid based on defal. The layout calculation considers pagination adjustments, event-specific spacing, and component dimensions.

  • rows - The number of rows in the grid.
  • cols - The number of columns in the grid.
  • defal - If true, updates the main grid; if false, updates an alternative grid.
  • actualRows - The effective row count used for layout calculations.

Example usage:

final params = UpdateMiniCardsGridParameters(
  updateGridRows: (rows) => print('Updated grid rows: $rows'),
  updateGridCols: (cols) => print('Updated grid cols: $cols'),
  updateAltGridRows: (rows) => print('Updated alt grid rows: $rows'),
  updateAltGridCols: (cols) => print('Updated alt grid cols: $cols'),
  updateGridSizes: (gridSizes) => print('Updated grid sizes: $gridSizes'),
  gridSizes: GridSizes(gridWidth: 100, gridHeight: 100, altGridWidth: 80, altGridHeight: 80),
  paginationDirection: 'horizontal',
  paginationHeightWidth: 30.0,
  doPaginate: true,
  componentSizes: ComponentSizes(otherWidth: 500, otherHeight: 300),
  eventType: EventType.chat,
  getUpdatedAllParams: () => params,
);

final options = UpdateMiniCardsGridOptions(
  rows: 3,
  cols: 4,
  defal: true,
  actualRows: 3,
  parameters: params,
);

await updateMiniCardsGrid(options);

Implementation

Future<void> updateMiniCardsGrid(UpdateMiniCardsGridOptions options) async {
  // Retrieve updated parameters
  var parameters = options.parameters.getUpdatedAllParams();

  // Destructure parameters
  final updateGridRows = parameters.updateGridRows;
  final updateGridCols = parameters.updateGridCols;
  final updateAltGridRows = parameters.updateAltGridRows;
  final updateAltGridCols = parameters.updateAltGridCols;
  final updateGridSizes = parameters.updateGridSizes;

  // Grid configuration
  var gridSizes = parameters.gridSizes;
  final paginationDirection = parameters.paginationDirection;
  final paginationHeightWidth = parameters.paginationHeightWidth;
  final doPaginate = parameters.doPaginate;
  final componentSizes = parameters.componentSizes;
  final eventType = parameters.eventType;

  double containerWidth = componentSizes.otherWidth;
  double containerHeight = componentSizes.otherHeight;

  // Adjust container size for pagination if enabled
  if (doPaginate) {
    if (paginationDirection == 'horizontal') {
      containerHeight -= paginationHeightWidth;
    } else {
      containerWidth -= paginationHeightWidth;
    }
  }

  int cardSpacing = eventType == EventType.chat ? 0 : 3;
  final totalSpacingHorizontal = (options.cols - 1) * cardSpacing;
  final totalSpacingVertical = (options.actualRows - 1) * cardSpacing;

  // Calculate individual card dimensions
  final cardWidth = options.cols == 0 || options.actualRows == 0
      ? 0
      : ((containerWidth - totalSpacingHorizontal) / options.cols).floor();
  final cardHeight = options.cols == 0 || options.actualRows == 0
      ? 0
      : ((containerHeight - totalSpacingVertical) / options.actualRows).floor();

  // Update grid or alternative grid based on `defal` flag
  if (options.defal) {
    updateGridRows(options.rows);
    updateGridCols(options.cols);
    gridSizes = GridSizes(
      gridWidth: cardWidth,
      gridHeight: cardHeight,
      altGridWidth: gridSizes.altGridWidth,
      altGridHeight: gridSizes.altGridHeight,
    );
    updateGridSizes(gridSizes);
  } else {
    updateAltGridRows(options.rows);
    updateAltGridCols(options.cols);
    gridSizes = GridSizes(
      gridWidth: gridSizes.gridWidth,
      gridHeight: gridSizes.gridHeight,
      altGridWidth: cardWidth,
      altGridHeight: cardHeight,
    );
    updateGridSizes(gridSizes);
  }
}