Skip to content

Commit

Permalink
Layout Update (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdlukaa authored Jan 5, 2025
2 parents 7cc7a4c + 6c8cae6 commit 7a035ee
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 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 All @@ -171,7 +184,8 @@ class StaticGridState extends State<StaticGrid> {
key: ValueKey(index),
width: childWidth,
child: AspectRatio(
aspectRatio: widget.childAspectRatio,
aspectRatio:
widget.childAspectRatio.clamp(0.1, double.infinity),
child: realChildren[index],
),
);
Expand Down

0 comments on commit 7a035ee

Please sign in to comment.