Created
June 14, 2022 11:34
-
-
Save samuchakraborty/019763b23fac107dc04ce527c020525c to your computer and use it in GitHub Desktop.
flutter gridview height handler
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
import 'package:flutter/rendering.dart'; | |
class SliverGridDelegateWithFixedCrossAxisCountAndFixedHeight | |
extends SliverGridDelegate { | |
const SliverGridDelegateWithFixedCrossAxisCountAndFixedHeight({ | |
required this.crossAxisCount, | |
this.mainAxisSpacing = 0.0, | |
this.crossAxisSpacing = 0.0, | |
this.height = 56.0, | |
}) : assert(crossAxisCount != null && crossAxisCount > 0), | |
assert(mainAxisSpacing != null && mainAxisSpacing >= 0), | |
assert(crossAxisSpacing != null && crossAxisSpacing >= 0), | |
assert(height != null && height > 0); | |
final int crossAxisCount; | |
final double mainAxisSpacing; | |
final double crossAxisSpacing; | |
final double height; | |
bool _debugAssertIsValid() { | |
assert(crossAxisCount > 0); | |
assert(mainAxisSpacing >= 0.0); | |
assert(crossAxisSpacing >= 0.0); | |
assert(height > 0.0); | |
return true; | |
} | |
@override | |
SliverGridLayout getLayout(SliverConstraints constraints) { | |
assert(_debugAssertIsValid()); | |
final double usableCrossAxisExtent = | |
constraints.crossAxisExtent - crossAxisSpacing * (crossAxisCount - 1); | |
final double childCrossAxisExtent = usableCrossAxisExtent / crossAxisCount; | |
final double childMainAxisExtent = height; | |
return SliverGridRegularTileLayout( | |
crossAxisCount: crossAxisCount, | |
mainAxisStride: childMainAxisExtent + mainAxisSpacing, | |
crossAxisStride: childCrossAxisExtent + crossAxisSpacing, | |
childMainAxisExtent: childMainAxisExtent, | |
childCrossAxisExtent: childCrossAxisExtent, | |
reverseCrossAxis: axisDirectionIsReversed(constraints.crossAxisDirection), | |
); | |
} | |
@override | |
bool shouldRelayout( | |
SliverGridDelegateWithFixedCrossAxisCountAndFixedHeight oldDelegate) { | |
return oldDelegate.crossAxisCount != crossAxisCount || | |
oldDelegate.mainAxisSpacing != mainAxisSpacing || | |
oldDelegate.crossAxisSpacing != crossAxisSpacing || | |
oldDelegate.height != height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment