Skip to content

Instantly share code, notes, and snippets.

@ltOgt
Created April 10, 2021 11:40
Show Gist options
  • Save ltOgt/2b0c65f3fb0cf422f461f7e11ed9c0a0 to your computer and use it in GitHub Desktop.
Save ltOgt/2b0c65f3fb0cf422f461f7e11ed9c0a0 to your computer and use it in GitHub Desktop.
Non-Scrollable GridView-ish Widget
class Grid extends StatelessWidget {
const Grid({
Key? key,
required this.children,
this.columns,
this.rows,
this.space = 10,
this.mainAxisAlignment = MainAxisAlignment.start,
this.crossAxisAlignment = CrossAxisAlignment.center,
}) : assert(
(columns != null && rows == null) ||
(rows != null && columns == null),
"Either columns XOR rows specifieds",
),
super(key: key);
final List<Widget> children;
final int? columns;
final int? rows;
final double space;
final MainAxisAlignment mainAxisAlignment;
final CrossAxisAlignment crossAxisAlignment;
@override
Widget build(BuildContext context) {
int _columns = columns ?? (children.length / rows!).ceil();
int _rows = rows ?? (children.length / columns!).ceil();
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: List<Widget>.generate(
_rows,
(r) => Row(
mainAxisAlignment: mainAxisAlignment,
crossAxisAlignment: crossAxisAlignment,
children: List<Widget>.generate(
_columns,
(c) => children[c + (r * _columns)],
).intertwineWith(
SizedBox(
width: space,
),
),
),
).intertwineWith(
SizedBox(
height: space,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment