1+ import 'dart:math' ;
2+
13import 'package:codelessly_json_annotation/codelessly_json_annotation.dart' ;
24import 'package:equatable/equatable.dart' ;
35
@@ -25,12 +27,54 @@ class GridViewNode extends SinglePlaceholderNode
2527
2628 @override
2729 BoxConstraintsModel ? relegatedConstraintsToChildren (BaseNode child) {
28- // TODO: implement relegatedConstraintsToChildren.
29- // if (scrollDirection == AxisC.vertical) {
30- // return BoxConstraintsModel(maxHeight: properties.itemExtent);
31- // } else {
32- // return BoxConstraintsModel(maxWidth: properties.itemExtent);
33- // }
30+ // TODO: maybe I am not using it correctly? Saad, please fix this if that's the case.
31+
32+ // Total cross axis space available for the grid view.
33+ final totalCrossAxisSize = scrollDirection.isVertical
34+ ? innerBoxLocal.size.width
35+ : innerBoxLocal.size.height;
36+
37+ double childMainAxisExtent, childCrossAxisExtent;
38+ switch (properties.gridDelegate) {
39+ case FixedCrossAxisCountGridDelegateProperties prop:
40+ final double usableCrossAxisExtent = max (
41+ 0.0 ,
42+ totalCrossAxisSize -
43+ prop.crossAxisSpacing * (prop.crossAxisCount - 1 ),
44+ );
45+ childCrossAxisExtent = usableCrossAxisExtent / prop.crossAxisCount;
46+ childMainAxisExtent =
47+ prop.mainAxisExtent ?? childCrossAxisExtent / prop.childAspectRatio;
48+ case MaxCrossAxisExtentGridDelegateProperties prop:
49+ int crossAxisCount = (totalCrossAxisSize /
50+ (prop.maxCrossAxisExtent + prop.crossAxisSpacing))
51+ .ceil ();
52+ crossAxisCount = max (1 , crossAxisCount);
53+
54+ final double usableCrossAxisExtent = max (
55+ 0.0 ,
56+ totalCrossAxisSize - prop.crossAxisSpacing * (crossAxisCount - 1 ),
57+ );
58+ childCrossAxisExtent = usableCrossAxisExtent / crossAxisCount;
59+ childMainAxisExtent =
60+ prop.mainAxisExtent ?? childCrossAxisExtent / prop.childAspectRatio;
61+ }
62+
63+ if (scrollDirection.isHorizontal) {
64+ return BoxConstraintsModel (
65+ minWidth: childMainAxisExtent,
66+ maxWidth: childMainAxisExtent,
67+ minHeight: childCrossAxisExtent,
68+ maxHeight: childCrossAxisExtent,
69+ );
70+ } else {
71+ return BoxConstraintsModel (
72+ minWidth: childCrossAxisExtent,
73+ maxWidth: childCrossAxisExtent,
74+ minHeight: childMainAxisExtent,
75+ maxHeight: childMainAxisExtent,
76+ );
77+ }
3478 }
3579
3680 /// Creates a [GridViewNode] .
@@ -101,9 +145,9 @@ class GridViewNode extends SinglePlaceholderNode
101145 AlignmentModel childAlignment () {
102146 switch (scrollDirection) {
103147 case AxisC .horizontal:
104- return reverse ? AlignmentModel .centerRight : AlignmentModel .centerLeft ;
148+ return reverse ? AlignmentModel .topRight : AlignmentModel .topLeft ;
105149 case AxisC .vertical:
106- return reverse ? AlignmentModel .bottomCenter : AlignmentModel .topCenter ;
150+ return reverse ? AlignmentModel .bottomLeft : AlignmentModel .topLeft ;
107151 }
108152 }
109153}
@@ -180,7 +224,10 @@ enum GridDelegateType {
180224 };
181225}
182226
227+ /// The properties of a grid delegate that controls the layout of a
228+ /// [GridViewNode] .
183229sealed class GridDelegateProperties with EquatableMixin , SerializableMixin {
230+ /// Represents the type of grid delegate.
184231 final GridDelegateType type;
185232
186233 /// Creates a new [GridDelegateProperties] instance.
@@ -204,10 +251,21 @@ sealed class GridDelegateProperties with EquatableMixin, SerializableMixin {
204251/// in the cross axis.
205252@JsonSerializable ()
206253class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
254+ /// The number of children to fit in the cross axis for a grid view.
207255 final int crossAxisCount;
256+
257+ /// The number of logical pixels between each child along the main axis.
208258 final double mainAxisSpacing;
259+
260+ /// The number of logical pixels between each child along the cross axis.
209261 final double crossAxisSpacing;
262+
263+ /// The ratio of the cross-axis to the main-axis extent of each child. This
264+ /// is ignored if [mainAxisExtent] is not null.
210265 final double childAspectRatio;
266+
267+ /// The main axis extent of each child in the grid. If this is null, then
268+ /// [childAspectRatio] controls the main axis extent of each child.
211269 final double ? mainAxisExtent;
212270
213271 /// Creates a new [FixedCrossAxisCountGridDelegateProperties] instance.
@@ -223,6 +281,7 @@ class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
223281 Map toJson () => _$FixedCrossAxisCountGridDelegatePropertiesToJson (this )
224282 ..['type' ] = type.name;
225283
284+ /// Creates a [FixedCrossAxisCountGridDelegateProperties] from a JSON object.
226285 factory FixedCrossAxisCountGridDelegateProperties .fromJson (Map json) =>
227286 _$FixedCrossAxisCountGridDelegatePropertiesFromJson (json);
228287
@@ -261,10 +320,21 @@ class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
261320/// maximum cross-axis extent.
262321@JsonSerializable ()
263322class MaxCrossAxisExtentGridDelegateProperties extends GridDelegateProperties {
323+ /// The maximum extent of a child/item in the cross axis.
264324 final double maxCrossAxisExtent;
325+
326+ /// The number of logical pixels between each child along the main axis.
265327 final double mainAxisSpacing;
328+
329+ /// The number of logical pixels between each child along the cross axis.
266330 final double crossAxisSpacing;
331+
332+ /// The ratio of the cross-axis to the main-axis extent of each child. This
333+ /// is ignored if [mainAxisExtent] is not null.
267334 final double childAspectRatio;
335+
336+ /// The main axis extent of each child in the grid. If this is null, then
337+ /// [childAspectRatio] controls the main axis extent of each child.
268338 final double ? mainAxisExtent;
269339
270340 /// Creates a new [MaxCrossAxisExtentGridDelegateProperties] instance.
@@ -280,6 +350,7 @@ class MaxCrossAxisExtentGridDelegateProperties extends GridDelegateProperties {
280350 Map toJson () => _$MaxCrossAxisExtentGridDelegatePropertiesToJson (this )
281351 ..['type' ] = type.name;
282352
353+ /// Creates a [MaxCrossAxisExtentGridDelegateProperties] from a JSON object.
283354 factory MaxCrossAxisExtentGridDelegateProperties .fromJson (Map json) =>
284355 _$MaxCrossAxisExtentGridDelegatePropertiesFromJson (json);
285356
0 commit comments