12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import 'package:flutter/rendering.dart';
- class FixedHeightGridDelegate extends SliverGridDelegate {
- /// Creates a delegate that makes grid layouts with a fixed number of tiles in
- /// the cross axis.
- ///
- /// All of the arguments must not be null. The `mainAxisSpacing` and
- /// `crossAxisSpacing` arguments must not be negative. The `crossAxisCount`
- /// and `childAspectRatio` arguments must be greater than zero.
- const FixedHeightGridDelegate({
- required this.crossAxisCount,
- this.mainAxisSpacing = 0.0,
- this.crossAxisSpacing = 0.0,
- this.height = 56.0,
- }) : assert(crossAxisCount > 0),
- assert(mainAxisSpacing >= 0),
- assert(crossAxisSpacing >= 0),
- assert(height > 0);
- /// The number of children in the cross axis.
- final int crossAxisCount;
- /// The number of logical pixels between each child along the main axis.
- final double mainAxisSpacing;
- /// The number of logical pixels between each child along the cross axis.
- final double crossAxisSpacing;
- /// The height of the crossAxis.
- 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(FixedHeightGridDelegate oldDelegate) {
- return oldDelegate.crossAxisCount != crossAxisCount ||
- oldDelegate.mainAxisSpacing != mainAxisSpacing ||
- oldDelegate.crossAxisSpacing != crossAxisSpacing ||
- oldDelegate.height != height;
- }
- }
|