assetGridAnchor method

double assetGridAnchor({
  1. required BuildContext context,
  2. required BoxConstraints constraints,
  3. required PathWrapper<Path>? pathWrapper,
})

Calculates the grid anchor when reverting items.

Implementation

double assetGridAnchor({
  required BuildContext context,
  required BoxConstraints constraints,
  required PathWrapper<Path>? pathWrapper,
}) {
  int totalCount = pathWrapper?.assetCount ?? 0;
  // If user chose a special item's position, add 1 count.
  if (specialItemPosition != SpecialItemPosition.none) {
    final specialItem = specialItemBuilder?.call(
      context,
      pathWrapper?.path,
      totalCount,
    );
    if (specialItem != null) {
      totalCount += 1;
    }
  }
  // Here we got a magic calculation. [itemSpacing] needs to be divided by
  // [gridCount] since every grid item is squeezed by the [itemSpacing],
  // and it's actual size is reduced with [itemSpacing / gridCount].
  final double dividedSpacing = itemSpacing / gridCount;
  final double topPadding = context.topPadding + appBarPreferredSize!.height;
  // Calculate rows count.
  final int row = (totalCount / gridCount).ceil();
  final double itemSize = constraints.maxWidth / gridCount;
  // Check whether all rows can be placed at the same time.
  final bool gridRevert = effectiveShouldRevertGrid(context);
  final bool onlyOneScreen =
      row * (itemSize + itemSpacing) <= constraints.maxHeight;
  final double anchor;
  if (!gridRevert || onlyOneScreen) {
    anchor = 0.0;
  } else {
    anchor = math.min(
      (row * (itemSize + dividedSpacing) + topPadding - itemSpacing) /
          constraints.maxHeight,
      1.0,
    );
  }
  return anchor;
}