Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout Update #312

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading