Skip to content

Commit

Permalink
feat: On Layout, when there is only a single row and there is enough …
Browse files Browse the repository at this point in the history
…space for another row, try to accomodate it
  • Loading branch information
bdlukaa committed Jan 5, 2025
1 parent 7648ff1 commit 6c8cae6
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions lib/widgets/reorderable_static_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class StaticGrid extends StatefulWidget {
}

class StaticGridState extends State<StaticGrid> {
late var crossAxisCount = widget.crossAxisCount;
List<Widget> realChildren = [];
int get gridRows => (realChildren.length / widget.crossAxisCount).ceil();
int get gridRows => (realChildren.length / crossAxisCount).ceil();
void generateRealChildren() {
realChildren = [...widget.children];

Expand Down Expand Up @@ -99,6 +100,7 @@ class StaticGridState extends State<StaticGrid> {
void didUpdateWidget(covariant StaticGrid oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.children != widget.children) {
crossAxisCount = widget.crossAxisCount;
generateRealChildren();
}
}
Expand All @@ -114,41 +116,52 @@ class StaticGridState extends State<StaticGrid> {
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(
Expand All @@ -158,8 +171,8 @@ class StaticGridState extends State<StaticGrid> {
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,
Expand Down

0 comments on commit 6c8cae6

Please sign in to comment.