class GrideMenu extends StatelessWidget {
GrideMenu({
Key key,
@required this.items,
@required this.onTap,
@required this.crossAxisCount
}):super(key:key);
final List items; / / the number of children
final Function onTap; // Child's click event
final int crossAxisCount; // The number of children per row
Widget build(context) {
// Mod the number of children
// If it is not equal to 0, there are extra children
// Then the number of lines must be increased by 1
final int more = items.length % crossAxisCount;
final int minCount = items.length ~/ crossAxisCount;
final int maxCount = more == 0 ? minCount : minCount + 1;
// Get the width
final screenWidth = MediaQuery.of(context).size.width;
// Find the width of a single child
final itemWidth = screenWidth /crossAxisCount;
return Container(
width: context.width,
padding: EdgeInsets.all(8),
height: maxCount * (itemWidth - 10.0),
child: GridView.builder(
// Children in the GridView will not be allowed to scroll
physics: NeverScrollableScrollPhysics(),
itemCount: items.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
// Number of elements on the horizontal axis
crossAxisCount: crossAxisCount,
// Vertical axis spacing
mainAxisSpacing: 5.0.// Horizontal axis spacing
crossAxisSpacing: 5.0.// Ratio of sub-component width to height to length
childAspectRatio: 1),
itemBuilder: (ctx, index) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
onTap: onTap,
child: Container(
color:Color(0xff0099ff(), [, (); })); }}Copy the code