-
-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃悰 Fix refresh issue on switching folders #179
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AlexV525
requested changes
Jul 19, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to replace
flutter_wechat_assets_picker/lib/src/delegates/asset_picker_builder_delegate.dart
Lines 805 to 808 in 07e22d6
@override | |
Widget assetsGridBuilder(BuildContext context) { | |
// First, we need the count of the assets. | |
int totalCount = provider.currentPathEntity!.assetCount; |
with below snippet (wrap it with a
Selector
, along with some format changes), instead of changing this flag:
@override
Widget assetsGridBuilder(BuildContext context) {
return Selector<DefaultAssetPickerProvider, AssetPathEntity?>(
selector: (_, DefaultAssetPickerProvider p) => p.currentPathEntity,
builder: (_, AssetPathEntity? path, __) {
// First, we need the count of the assets.
int totalCount = path!.assetCount;
// If user chose a special item's position, add 1 count.
if (specialItemPosition != SpecialItemPosition.none) {
totalCount += 1;
}
// Then use the [totalCount] to calculate how many placeholders we need.
final int placeholderCount;
if (isAppleOS && totalCount % gridCount != 0) {
// When there are left items that not filled into one row,
// filled the row with placeholders.
placeholderCount = gridCount - totalCount % gridCount;
} else {
// Otherwise, we don't need placeholders.
placeholderCount = 0;
}
// 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].
final double dividedSpacing = itemSpacing / gridCount;
final double topPadding = context.topPadding + kToolbarHeight;
Widget _sliverGrid(BuildContext ctx, List<AssetEntity> assets) {
return SliverGrid(
delegate: SliverChildBuilderDelegate(
(_, int index) => Builder(
builder: (BuildContext c) {
if (isAppleOS) {
if (index < placeholderCount) {
return const SizedBox.shrink();
}
index -= placeholderCount;
}
return Directionality(
textDirection: Directionality.of(context),
child: assetGridItemBuilder(c, index, assets),
);
},
),
childCount: assetsGridItemCount(
context: ctx,
assets: assets,
placeholderCount: placeholderCount,
),
findChildIndexCallback: (Key? key) {
if (key is ValueKey<String>) {
return findChildIndexBuilder(
id: key.value,
assets: assets,
placeholderCount: placeholderCount,
);
}
return null;
},
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: gridCount,
mainAxisSpacing: itemSpacing,
crossAxisSpacing: itemSpacing,
),
);
}
return LayoutBuilder(
builder: (BuildContext c, BoxConstraints constraints) {
final double itemSize = constraints.maxWidth / gridCount;
// Check whether all rows can be placed at the same time.
final bool onlyOneScreen = row * itemSize <=
constraints.maxHeight -
context.bottomPadding -
topPadding -
permissionLimitedBarHeight;
final double height;
if (onlyOneScreen) {
height = constraints.maxHeight;
} else {
// Reduce [permissionLimitedBarHeight] for the final height.
height = constraints.maxHeight - permissionLimitedBarHeight;
}
// Use [ScrollView.anchor] to determine where is the first place of
// the [SliverGrid]. Each row needs [dividedSpacing] to calculate,
// then minus one times of [itemSpacing] because spacing's count
// in the cross axis is always less than the rows.
final double anchor = math.min(
(row * (itemSize + dividedSpacing) + topPadding - itemSpacing) /
height,
1,
);
return Directionality(
textDirection: effectiveGridDirection(context),
child: ColoredBox(
color: theme.canvasColor,
child: Selector<DefaultAssetPickerProvider, List<AssetEntity>>(
selector: (_, DefaultAssetPickerProvider provider) =>
provider.currentAssets,
builder: (_, List<AssetEntity> assets, __) =>
CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: gridScrollController,
anchor: isAppleOS ? anchor : 0,
center: isAppleOS ? gridRevertKey : null,
slivers: <Widget>[
if (isAppleOS)
SliverGap.v(context.topPadding + kToolbarHeight),
_sliverGrid(_, assets),
// Ignore the gap when the [anchor] is not equal to 1.
if (isAppleOS && anchor == 1)
SliverGap.v(
context.bottomPadding + bottomSectionHeight,
),
if (isAppleOS)
SliverToBoxAdapter(
key: gridRevertKey,
child: const SizedBox.shrink(),
),
],
),
),
),
);
},
);
},
);
}
AlexV525
approved these changes
Jul 19, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for taking my suggestion. 馃憤
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@AlexV525
This fixes the following issue on iOS. It is possible this is an overkill, and issue can be solved in a softer manner.