From 7648ff13c0a73cd9b6dd22b01752c68f7d505850 Mon Sep 17 00:00:00 2001 From: Bruno D'Luka Date: Sun, 5 Jan 2025 17:08:57 -0300 Subject: [PATCH 1/2] fix: Minimum aspect ratio --- lib/widgets/reorderable_static_grid.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/widgets/reorderable_static_grid.dart b/lib/widgets/reorderable_static_grid.dart index 3e2b93e7..d3cb9b81 100644 --- a/lib/widgets/reorderable_static_grid.dart +++ b/lib/widgets/reorderable_static_grid.dart @@ -171,7 +171,8 @@ class StaticGridState extends State { key: ValueKey(index), width: childWidth, child: AspectRatio( - aspectRatio: widget.childAspectRatio, + aspectRatio: + widget.childAspectRatio.clamp(0.1, double.infinity), child: realChildren[index], ), ); From 6c8cae65d31d3877f8f2808f5e709dad66d9ab5e Mon Sep 17 00:00:00 2001 From: Bruno D'Luka Date: Sun, 5 Jan 2025 18:03:16 -0300 Subject: [PATCH 2/2] feat: On Layout, when there is only a single row and there is enough space for another row, try to accomodate it --- lib/widgets/reorderable_static_grid.dart | 77 ++++++++++++++---------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/lib/widgets/reorderable_static_grid.dart b/lib/widgets/reorderable_static_grid.dart index d3cb9b81..a8b961d2 100644 --- a/lib/widgets/reorderable_static_grid.dart +++ b/lib/widgets/reorderable_static_grid.dart @@ -68,8 +68,9 @@ class StaticGrid extends StatefulWidget { } class StaticGridState extends State { + late var crossAxisCount = widget.crossAxisCount; List realChildren = []; - int get gridRows => (realChildren.length / widget.crossAxisCount).ceil(); + int get gridRows => (realChildren.length / crossAxisCount).ceil(); void generateRealChildren() { realChildren = [...widget.children]; @@ -99,6 +100,7 @@ class StaticGridState extends State { void didUpdateWidget(covariant StaticGrid oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.children != widget.children) { + crossAxisCount = widget.crossAxisCount; generateRealChildren(); } } @@ -114,41 +116,52 @@ class StaticGridState extends State { vertical: widget.mainAxisSpacing, )), child: LayoutBuilder(builder: (context, constraints) { - final availableWidth = constraints.biggest.width - - widget.padding.horizontal - - (widget.crossAxisCount - 1) * widget.crossAxisSpacing; - - var childWidth = availableWidth / widget.crossAxisCount; - final childHeight = childWidth / widget.childAspectRatio; - double gridHeight = - childHeight * gridRows + widget.crossAxisSpacing * (gridRows - 1); - - if (gridRows == 1) { - // For a single row, ensure childHeight does not exceed the - // available height - if (childHeight > constraints.biggest.height) { - final maxHeight = constraints.biggest.height; - childWidth = maxHeight * widget.childAspectRatio; - gridHeight = maxHeight; - } - } else { - // For multiple rows, calculate gridHeight and adjust childWidth if - // necessary + late double gridHeight, childWidth; + void calculate() { + var availableWidth = constraints.biggest.width - + widget.padding.horizontal - + (widget.crossAxisCount - 1) * widget.crossAxisSpacing; + + childWidth = availableWidth / crossAxisCount; + final childHeight = childWidth / widget.childAspectRatio; gridHeight = childHeight * gridRows + widget.crossAxisSpacing * (gridRows - 1); - if (gridHeight > constraints.biggest.height) { - // Calculate the maximum height each child can have to fit - // within the available height - final maxHeight = - constraints.biggest.height / gridRows - widget.crossAxisSpacing; - - // Calculate the new width based on the maximum height and - // the aspect ratio - childWidth = maxHeight * widget.childAspectRatio; + if (gridRows == 1) { + // If there is enough space for another row, try to accomodate it + if (gridHeight * 2 < constraints.biggest.height) { + crossAxisCount -= 1; + calculate(); + } + + // For a single row, ensure childHeight does not exceed the + // available height + if (childHeight > constraints.biggest.height) { + final maxHeight = constraints.biggest.height; + childWidth = maxHeight * widget.childAspectRatio; + gridHeight = maxHeight; + } + } else { + // For multiple rows, calculate gridHeight and adjust childWidth if + // necessary + gridHeight = (childHeight * gridRows) + + (widget.crossAxisSpacing * (gridRows - 1)); + + if (gridHeight > constraints.biggest.height) { + // Calculate the maximum height each child can have to fit + // within the available height + final maxHeight = constraints.biggest.height / gridRows - + widget.crossAxisSpacing; + + // Calculate the new width based on the maximum height and + // the aspect ratio + childWidth = maxHeight * widget.childAspectRatio; + } } } + calculate(); + return SizedBox( height: gridHeight, child: ScrollConfiguration( @@ -158,8 +171,8 @@ class StaticGridState extends State { enableReorder: widget.reorderable, spacing: widget.mainAxisSpacing, runSpacing: widget.crossAxisSpacing, - minMainAxisCount: widget.crossAxisCount, - maxMainAxisCount: widget.crossAxisCount, + minMainAxisCount: crossAxisCount, + maxMainAxisCount: crossAxisCount, onReorder: widget.onReorder, needsLongPressDraggable: isMobile, alignment: WrapAlignment.center,