@@ -4,6 +4,7 @@ import 'package:codelessly_json_annotation/codelessly_json_annotation.dart';
44import 'package:equatable/equatable.dart' ;
55
66import '../../../codelessly_api.dart' ;
7+ import '../models/grid_delegate_type.dart' ;
78
89part 'grid_view_node.g.dart' ;
910
@@ -25,58 +26,6 @@ class GridViewNode extends SinglePlaceholderNode
2526 /// The properties of the [GridView] .
2627 GridViewProperties properties;
2728
28- @override
29- BoxConstraintsModel ? relegatedConstraintsToChildren (BaseNode child) {
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- }
78- }
79-
8029 /// Creates a [GridViewNode] .
8130 GridViewNode ({
8231 required super .id,
@@ -134,6 +83,55 @@ class GridViewNode extends SinglePlaceholderNode
13483 );
13584 }
13685
86+ @override
87+ BoxConstraintsModel ? relegatedConstraintsToChildren (BaseNode child) {
88+ // Total cross axis space available for the grid view.
89+ final totalCrossAxisSize = scrollDirection.isVertical
90+ ? innerBoxLocal.size.width
91+ : innerBoxLocal.size.height;
92+
93+ double childMainAxisExtent, childCrossAxisExtent;
94+ switch (properties.gridDelegate) {
95+ case FixedCrossAxisCountGridDelegateProperties prop:
96+ final double usableCrossAxisExtent = max (
97+ 0.0 ,
98+ totalCrossAxisSize -
99+ prop.crossAxisSpacing * (prop.crossAxisCount - 1 ),
100+ );
101+ childCrossAxisExtent = usableCrossAxisExtent / prop.crossAxisCount;
102+ childMainAxisExtent =
103+ prop.mainAxisExtent ?? childCrossAxisExtent / prop.childAspectRatio;
104+ case MaxCrossAxisExtentGridDelegateProperties prop:
105+ int crossAxisCount = (totalCrossAxisSize /
106+ (prop.maxCrossAxisExtent + prop.crossAxisSpacing))
107+ .ceil ();
108+ crossAxisCount = max (1 , crossAxisCount);
109+
110+ final double usableCrossAxisExtent = max (
111+ 0.0 ,
112+ totalCrossAxisSize - prop.crossAxisSpacing * (crossAxisCount - 1 ),
113+ );
114+ childCrossAxisExtent = usableCrossAxisExtent / crossAxisCount;
115+ childMainAxisExtent =
116+ prop.mainAxisExtent ?? childCrossAxisExtent / prop.childAspectRatio;
117+ }
118+
119+ return switch (scrollDirection) {
120+ AxisC .horizontal => BoxConstraintsModel (
121+ minWidth: childMainAxisExtent,
122+ maxWidth: childMainAxisExtent,
123+ minHeight: childCrossAxisExtent,
124+ maxHeight: childCrossAxisExtent,
125+ ),
126+ AxisC .vertical => BoxConstraintsModel (
127+ minWidth: childCrossAxisExtent,
128+ maxWidth: childCrossAxisExtent,
129+ minHeight: childMainAxisExtent,
130+ maxHeight: childMainAxisExtent,
131+ ),
132+ };
133+ }
134+
137135 /// Creates a [GridViewNode] from a JSON object.
138136 factory GridViewNode .fromJson (Map json) => _$GridViewNodeFromJson (json);
139137
@@ -207,31 +205,43 @@ class GridViewProperties with SerializableMixin, EquatableMixin {
207205 ];
208206}
209207
210- /// Defines the type of grid delegate to use for a [GridViewNode] .
211- enum GridDelegateType {
212- /// Represents a delegate that makes grid layouts with a fixed number of tiles
213- /// in the cross axis.
214- fixedCrossAxisCount,
215-
216- /// Represents a delegate that makes grid layouts with tiles that each have a
217- /// maximum cross-axis extent.
218- maxCrossAxisExtent;
219-
220- /// Pretty name of the enum value.
221- String get label => switch (this ) {
222- fixedCrossAxisCount => 'Fixed Cross Axis Count' ,
223- maxCrossAxisExtent => 'Max Cross Axis Extent' ,
224- };
225- }
226-
227208/// The properties of a grid delegate that controls the layout of a
228209/// [GridViewNode] .
229210sealed class GridDelegateProperties with EquatableMixin , SerializableMixin {
230211 /// Represents the type of grid delegate.
231212 final GridDelegateType type;
232213
214+ /// The number of logical pixels between each child along the main axis.
215+ final double mainAxisSpacing;
216+
217+ /// The number of logical pixels between each child along the cross axis.
218+ final double crossAxisSpacing;
219+
220+ /// The ratio of the cross-axis to the main-axis extent of each child. This
221+ /// is ignored if [mainAxisExtent] is not null.
222+ final double childAspectRatio;
223+
224+ /// The main axis extent of each child in the grid. If this is null, then
225+ /// [childAspectRatio] controls the main axis extent of each child.
226+ final double ? mainAxisExtent;
227+
233228 /// Creates a new [GridDelegateProperties] instance.
234- const GridDelegateProperties (this .type);
229+ const GridDelegateProperties (this .type, {
230+ this .mainAxisSpacing = 0 ,
231+ this .crossAxisSpacing = 0 ,
232+ this .childAspectRatio = 1 ,
233+ this .mainAxisExtent,
234+ });
235+
236+ /// Creates a copy of this [GridDelegateProperties] instance with the given
237+ /// value overrides.
238+ GridDelegateProperties copyWith ({
239+ double ? mainAxisSpacing,
240+ double ? crossAxisSpacing,
241+ double ? childAspectRatio,
242+ double ? mainAxisExtent,
243+ bool forceMainAxisExtent = false ,
244+ });
235245
236246 factory GridDelegateProperties .fromJson (Map json) {
237247 final type = GridDelegateType .values.byName (json['type' ]);
@@ -254,27 +264,13 @@ class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
254264 /// The number of children to fit in the cross axis for a grid view.
255265 final int crossAxisCount;
256266
257- /// The number of logical pixels between each child along the main axis.
258- final double mainAxisSpacing;
259-
260- /// The number of logical pixels between each child along the cross axis.
261- 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.
265- 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.
269- final double ? mainAxisExtent;
270-
271267 /// Creates a new [FixedCrossAxisCountGridDelegateProperties] instance.
272268 const FixedCrossAxisCountGridDelegateProperties ({
273269 required this .crossAxisCount,
274- this .mainAxisSpacing = 0 ,
275- this .crossAxisSpacing = 0 ,
276- this .childAspectRatio = 1 ,
277- this .mainAxisExtent,
270+ super .mainAxisSpacing = 0 ,
271+ super .crossAxisSpacing = 0 ,
272+ super .childAspectRatio = 1 ,
273+ super .mainAxisExtent,
278274 }) : super (GridDelegateType .fixedCrossAxisCount);
279275
280276 @override
@@ -287,6 +283,7 @@ class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
287283
288284 /// Creates a copy of this [FixedCrossAxisCountGridDelegateProperties]
289285 /// instance with the given value overrides.
286+ @override
290287 FixedCrossAxisCountGridDelegateProperties copyWith ({
291288 int ? crossAxisCount,
292289 double ? mainAxisSpacing,
@@ -297,22 +294,19 @@ class FixedCrossAxisCountGridDelegateProperties extends GridDelegateProperties {
297294 }) {
298295 return FixedCrossAxisCountGridDelegateProperties (
299296 crossAxisCount: crossAxisCount ?? this .crossAxisCount,
300- mainAxisSpacing: mainAxisSpacing ?? this .mainAxisSpacing,
301- crossAxisSpacing: crossAxisSpacing ?? this .crossAxisSpacing,
302- childAspectRatio: childAspectRatio ?? this .childAspectRatio,
297+ mainAxisSpacing: mainAxisSpacing ?? super .mainAxisSpacing,
298+ crossAxisSpacing: crossAxisSpacing ?? super .crossAxisSpacing,
299+ childAspectRatio: childAspectRatio ?? super .childAspectRatio,
303300 mainAxisExtent: forceMainAxisExtent
304301 ? mainAxisExtent
305- : mainAxisExtent ?? this .mainAxisExtent,
302+ : mainAxisExtent ?? super .mainAxisExtent,
306303 );
307304 }
308305
309306 @override
310307 List <Object ?> get props => [
311308 ...super .props,
312309 crossAxisCount,
313- mainAxisSpacing,
314- crossAxisSpacing,
315- childAspectRatio,
316310 ];
317311}
318312
@@ -323,27 +317,13 @@ class MaxCrossAxisExtentGridDelegateProperties extends GridDelegateProperties {
323317 /// The maximum extent of a child/item in the cross axis.
324318 final double maxCrossAxisExtent;
325319
326- /// The number of logical pixels between each child along the main axis.
327- final double mainAxisSpacing;
328-
329- /// The number of logical pixels between each child along the cross axis.
330- 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.
334- 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.
338- final double ? mainAxisExtent;
339-
340320 /// Creates a new [MaxCrossAxisExtentGridDelegateProperties] instance.
341321 const MaxCrossAxisExtentGridDelegateProperties ({
342322 required this .maxCrossAxisExtent,
343- this .mainAxisSpacing = 0 ,
344- this .crossAxisSpacing = 0 ,
345- this .childAspectRatio = 1 ,
346- this .mainAxisExtent,
323+ super .mainAxisSpacing = 0 ,
324+ super .crossAxisSpacing = 0 ,
325+ super .childAspectRatio = 1 ,
326+ super .mainAxisExtent,
347327 }) : super (GridDelegateType .maxCrossAxisExtent);
348328
349329 @override
@@ -356,6 +336,7 @@ class MaxCrossAxisExtentGridDelegateProperties extends GridDelegateProperties {
356336
357337 /// Creates a copy of this [MaxCrossAxisExtentGridDelegateProperties]
358338 /// instance with the given value overrides.
339+ @override
359340 MaxCrossAxisExtentGridDelegateProperties copyWith ({
360341 double ? maxCrossAxisExtent,
361342 double ? mainAxisSpacing,
@@ -366,21 +347,18 @@ class MaxCrossAxisExtentGridDelegateProperties extends GridDelegateProperties {
366347 }) {
367348 return MaxCrossAxisExtentGridDelegateProperties (
368349 maxCrossAxisExtent: maxCrossAxisExtent ?? this .maxCrossAxisExtent,
369- mainAxisSpacing: mainAxisSpacing ?? this .mainAxisSpacing,
370- crossAxisSpacing: crossAxisSpacing ?? this .crossAxisSpacing,
371- childAspectRatio: childAspectRatio ?? this .childAspectRatio,
350+ mainAxisSpacing: mainAxisSpacing ?? super .mainAxisSpacing,
351+ crossAxisSpacing: crossAxisSpacing ?? super .crossAxisSpacing,
352+ childAspectRatio: childAspectRatio ?? super .childAspectRatio,
372353 mainAxisExtent: forceMainAxisExtent
373354 ? mainAxisExtent
374- : mainAxisExtent ?? this .mainAxisExtent,
355+ : mainAxisExtent ?? super .mainAxisExtent,
375356 );
376357 }
377358
378359 @override
379360 List <Object ?> get props => [
380361 ...super .props,
381362 maxCrossAxisExtent,
382- mainAxisSpacing,
383- crossAxisSpacing,
384- childAspectRatio,
385363 ];
386364}
0 commit comments