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 getScreenHeight(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

1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
nico
nico
1 year ago

Hello. Thanks for this.
(your second function’s name should be getScreenHeight)

1
0
Would love your thoughts, please comment.x
()
x