How to Get the Height of a Widget in Flutter?
Last Updated :
24 Apr, 2025
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: (BuildContext context, BoxConstraints constraints) {
double height = constraints.maxHeight;
print("The height of the container is $height");
return Text("The height of the container is $height");
},
),
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(RunMyApp());
}
In the above code, runApp method calls the class RunMyApp, Now we have to create it.
Step 3: Creating Stateless Widget
Now we have to make a stateless widget because our application does not 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 StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp();
}
}
Step 4: Working with Scaffold Widget
Give the home property and there can be a scaffold widget with AppBar and body property. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(
title: Text('Appbar'),
),
body:
),
Step 5: In the body, you can use the LayoutBuilder to get the height of the widget.
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double height = constraints.maxHeight;
print("The height of the container is $height");
return Text("The height of the container is $height");
},
),
Code:
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Appbar'),
),
body: Container(
child: Center(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
double height = constraints.maxHeight;
return Text("The height of the container is $height");
},
),
),
),
),
);
}
}
Output:
Similar Reads
Flutter - Set Custom Height for Widget in GridView
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 = M
3 min read
Flutter - Determine the Height and Width of the Screen
Recently we created a Flutter Application and it's running well but when we installed the same application on another big-screen device it gives an error. When we switch the device to a new device the container is too long and throws an error. That means the application must be responsive to any scr
3 min read
How to Get Coordinates Widgets using rect_getter Package in Flutter?
Sometimes we need to get a widget's location or show some overlay widget or something similar over a widget whose location on the screen could be dynamic. In this article, we are going to discuss the way to get coordinates of a widget using the rect_getter package. Follow the steps below : 1. Create
9 min read
Flutter - Set the Height of the AppBar
In Flutter, the AppBar widget has a default height of 56 logical pixels. If you want to increase the height of the AppBar, you can use the toolbarHeight property. How to Use?Dart AppBar( toolbarHeight: 120, title: // tittle of the appbar ), Step By Step Implementation Step 1: Create a New Project in
3 min read
How to use Conditional Statement on Widget in Flutter?
Handling conditions is not too much difficult in Flutter. We can easily write our conditions based on the requirements which we are acquiring from our application. Let's work on conditions in flutter Apps. In this article, we'll talk about conditions and how we handle elements or widgets by using co
2 min read
OffStage Widget in Flutter
Flutter provides an inbuilt widget called âOffstageâ, which is been used to hide or show any widget programmatically depending on user action/event. Offstage Widget is a widget that lays the child out as if it was in the tree, but without painting anything, without making the child available for hit
4 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 - Splitting App into Widgets
Splitting an app into widgets refers to the breaking up of large widgets into smaller, more manageable widgets which can be inferred as smaller chunks of codes. The principal thing here is to understand when to split a widget into smaller ones. We are going to discuss when to split a widget and meth
5 min read
Flutter - GestureDetector Widget
A GestureDetector is a very useful Flutter widget that allows you to recognize and respond to various touch gestures, such as taps, swipe, double taps, drags, and more. It provides a way to make your Flutter app interactive and responsive to user input. In this article, we are going to implement and
4 min read
RotatedBox Widget in Flutter
The RotatedBox widget is used to rotate its child by an integral number of quarter turns. It is used to orient its child widgets into either horizontal or vertical orientation. Furthermore, it is very lightweight and can be used for designing various UI as it gives flexibility to the user over the D
2 min read