Flutter - Set Custom Height for Widget in GridView
Last Updated :
24 Apr, 2025
To set the Custom Height and Width of Grid View we can use the property childAspectRatio of the grid view. childAspectRatio takes the ratio of width and height.
childAspectRatio: (itemWidth / itemHeight)
Then we can assign itemWidth and ItemHeight with values.
Dart
// size of the screen
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;
Here is the sample flutter project that implements the custom height and width of the grid view.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the material package
A material package gives us the essential functions and Parameters, Now call the runApp method that needs an Application in the main function.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home:RunMyApp()));
}
You also set debugShowCheckedModeBanner false and theme. In the above code, runApp method calls the class RunMyApp, Now we have to create it.
Step 3: Creating a Stateful Widget
Now we have to make a stateful widget because our application does go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more of the application.
Shortcut: For creating a stateless or Stateful widget, you can create a stateless or stateful widget by just typing three alphabets ‘stl’ and you can see a stateless widget and then hit enter.
class RunMyApp extends StatefulWidget {
@override
_RunMyAppState createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
List<String> widgetList = ['Geeks', 'for', 'Geeks'];
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;
return Scaffold(
appBar: AppBar(
title: Text("Custom Height and Width"),
),
body:
);
}
}
Step 4: In the body of the app, we can simply use the GridView, use the childAspectRatio property, and pass the height and width to it.
Container(
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: (itemWidth / itemHeight),
controller: ScrollController(keepScrollOffset: false),
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: widgetList.map((String value) {
return Container(
color: Colors.amberAccent,
margin: EdgeInsets.all(1.0),
child: Center(
child: Text(
value,
style: TextStyle(
fontSize: 50.0,
),
),
),
Code Example
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: RunMyApp(),
));
}
class RunMyApp extends StatefulWidget {
@override
_RunMyAppState createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
List<String> widgetList = ['Geeks', 'for', 'Geeks'];
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;
return Scaffold(
appBar: AppBar(
title: Text("Custom Height and Width"),
),
body: Container(
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: (itemWidth / itemHeight),
controller: ScrollController(keepScrollOffset: false),
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: widgetList.map((String value) {
return Container(
color: Colors.amberAccent,
margin: EdgeInsets.all(1.0),
child: Center(
child: Text(
value,
style: TextStyle(
fontSize: 50.0,
),
),
),
);
}).toList(),
),
),
);
}
}
Output
Â
Similar Reads
Custom Paint Widget in Flutter
Flutter gives developers the full freedom to use every pixel on the screen and draw custom shapes. This has been one of the key selling points of Flutter. To achieve this, we use the CustomPainter class. In this article, we'll try to create the GeeksforGeeks Logo using Flutter's CustomPaint widget.
5 min read
How to Get the Height of a Widget in Flutter?
To get the height of a widget in Flutter, you can use the LayoutBuilder widget to obtain the constraints of the parent container and then get the height from those constraints. How to use it? You can simply use this widget anywhere in your project as you needed that. LayoutBuilder( builder: (BuildCo
3 min read
Raised Button widget in Flutter
RaisedButton is the material design button based on a Material widget that elevates when pressed upon in flutter. It is one of the most widely used buttons in the flutter library. Let's understand this button with the help of an example. Disclaimer: As of May 2021 the RaisedButton class in flutter i
5 min read
Flutter - Custom Widgets
We create Custom Widgets when we want a custom look and feel to our app, and we know that there will be a repetition of a particular widget. We can create the custom widget in a new dart file with all the codes and defining the parameters that we need in the constructor. For more on how to split wid
8 min read
Flutter - Create Option Menu for ListView
ListView is the efficient widget to display a list of items. Sometimes we need some options on individual items in the ListView. To create an options menu for a ListView in Flutter, you can use the PopupMenuButton widget along with PopupMenuItem or PopupMenuItemBuilder. This allows you to display a
5 min read
Flutter - RichText Widget
The RichText widget is used to display text that uses various different styles. The displayed text is described using a tree of TextSpan objects, each of which has its own associated style that is used for that subtree. Depending on the layout constraints the text might break across multiple lines o
3 min read
Motion Toast Widget in Flutter
A Motion Toast widget in Flutter is a type of toast message that animates its appearance on the screen. It can be used to provide feedback or notifications to the user in a subtle, yet attention-grabbing way. One way to implement a Motion Toast in Flutter is to use the AnimatedContainer widget. You
3 min read
Flutter - Positioned Widget
Positioned is a widget that comes built-in with flutter SDK. Positioned does exactly what it sounds like, which is it arbitrarily positioned widgets on top of each other. It is usually used to position child widgets in Stack widget or similar. It only works for Stateless and Stateful widgets. Constr
3 min read
Flutter - Inherited Widget
If you are a flutter developer then you know how easy is to create Flutter UI. But when you have lots of widgets in your app and you want to pass data from one widget to another widget, this will be a pain for many developers,s especially for the newbie, this will be really hard for them to pass the
6 min read
Flutter - RadioListTile Widget
RadioListTile is a widget that combines a radio button with a list tile. It is often used in scenarios where you need to present a list of mutually exclusive options, and the user can select one option from the list. Each RadioListTile represents a single option in the list and consists of a title,
6 min read