Flutter – How to get screen width and screen height

In many cases, when you want to build responsive UIs, you will need to know the screen size (width and/or height). In this post, let’s create simple functions to retrieve those values:

1. Get screen width

double getScreenWidth(BuildContext context) {
  return MediaQuery.of(context).size.width;
}

2. Get screen height

double getScreenWidth(BuildContext context) {
  return MediaQuery.of(context).size.height;
}

3. Get screen height and exclude SafeArea

double getScreenHeightExcludeSafeArea(BuildContext context) {
  final double height =  MediaQuery.of(context).size.height;
  final EdgeInsets padding = MediaQuery.of(context).padding;
  return height - padding.top - padding.bottom;
}
Tagged : / / / /
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x