Skip to content

Commit

Permalink
🐛 Fix placeholders count
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Jan 7, 2025
1 parent 643af2f commit a02aa2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class AssetGridDragSelectionCoordinator {
final int placeholderCount = delegate.assetsGridItemPlaceholderCount(
context: context,
pathWrapper: provider.currentPath,
onlyOneScreen: onlyOneScreen,
);

int columnIndex = getDragAxisIndex(globalPosition.dx, itemSize);
Expand Down
34 changes: 23 additions & 11 deletions lib/src/delegates/asset_picker_builder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ abstract class AssetPickerBuilderDelegate<Asset, Path> {
int assetsGridItemPlaceholderCount({
required BuildContext context,
required PathWrapper<Path>? pathWrapper,
required bool onlyOneScreen,
}) {
if (onlyOneScreen) {
return 0;
}
final bool gridRevert = effectiveShouldRevertGrid(context);
int totalCount = pathWrapper?.assetCount ?? 0;
// If user chose a special item's position, add 1 count.
Expand Down Expand Up @@ -1288,13 +1292,6 @@ class DefaultAssetPickerBuilderDelegate
if (totalCount == 0 && specialItem == null) {
return loadingIndicator(context);
}
// Then we use the [totalCount] to calculate placeholders we need.
final placeholderCount = assetsGridItemPlaceholderCount(
context: context,
pathWrapper: wrapper,
);
// Calculate rows count.
final int row = (totalCount + placeholderCount) ~/ gridCount;
// 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].
Expand All @@ -1306,11 +1303,21 @@ class DefaultAssetPickerBuilderDelegate
// the grid item before it gets manipulated by the grid revert.
final textDirectionCorrection = Directionality.of(context);

Widget sliverGrid(BuildContext context, List<AssetEntity> assets) {
Widget sliverGrid(
BuildContext context,
List<AssetEntity> assets,
bool onlyOneScreen,
) {
// Then we use the [totalCount] to calculate placeholders we need.
final placeholderCount = assetsGridItemPlaceholderCount(
context: context,
pathWrapper: wrapper,
onlyOneScreen: onlyOneScreen,
);
return SliverGrid(
delegate: SliverChildBuilderDelegate(
(context, int index) {
if (gridRevert) {
if (placeholderCount > 0) {
if (index < placeholderCount) {
return const SizedBox.shrink();
}
Expand Down Expand Up @@ -1424,6 +1431,8 @@ class DefaultAssetPickerBuilderDelegate

return LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// 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 onlyOneScreen = row * itemSize <=
Expand Down Expand Up @@ -1453,8 +1462,11 @@ class DefaultAssetPickerBuilderDelegate
);
}

final reverted = gridRevert && !onlyOneScreen;
return Directionality(
textDirection: effectiveGridDirection(context),
textDirection: reverted
? effectiveGridDirection(context)
: Directionality.of(context),
child: ColoredBox(
color: theme.canvasColor,
child: Selector<DefaultAssetPickerProvider, List<AssetEntity>>(
Expand All @@ -1476,7 +1488,7 @@ class DefaultAssetPickerBuilderDelegate
SliverGap.v(
context.topPadding + appBarPreferredSize!.height,
),
sliverGrid(context, assets),
sliverGrid(context, assets, onlyOneScreen),
// Append the extra bottom padding for Apple OS.
if (anchor == 1 && isAppleOS(context)) bottomGap,
if (gridRevert && !onlyOneScreen)
Expand Down

0 comments on commit a02aa2c

Please sign in to comment.