How to get flutter windows desktop users screen width at runtime
473
The window_size
plugin will let you get the current screen size, or a list of all screens and their sizes.
Author by
exceptionsAreBad
Updated on January 02, 2023Comments
-
exceptionsAreBad over 1 year
So, normally I can get screen size by using
MediaQuery.of(context).size
, however I don't have access to context at runtime. Is there any package or API I can tap into to accomplish this ?EDIT: Accepted @smorgan answer. Adding the pubspec.yaml entry with small use case for others, so they don't have to go digging.
**Noting this will eventually be removed and added into flutter core functionality (hopefully).
Also the
ref
field is the commit hash of current branch as of 12/15/21pubspec.yaml entry:
window_size: git: url: git://github.com/google/flutter-desktop-embedding.git path: plugins/window_size ref: 03d957e8b5c99fc83cd4a781031b154ab3de8753
Getting Screen Size
var screenSize = await getCurrentScreen(); print(screenSize?.frame.size);
-
exceptionsAreBad over 2 yearsIts returning my applications window size, not the users screen size.
-
Stijn2210 over 2 yearsWell just maximize the screen using the plugin and then measure the size
-
exceptionsAreBad over 2 yearsYea there must be some kind of bug, sometimes it comes back as size 0 and sometimes it comes back as 1280X720, which my current screen resolution is 1920X1080. I don't know what's going on.
-
Stijn2210 over 2 yearsWeird, I guess desktop apps still have some hiccups to be worked out
-
exceptionsAreBad over 2 yearsThanks. Probably a broad question but, is there a timeframe when you think it may be in the flutter api? Trying to gauge when this may break.
-
exceptionsAreBad over 2 yearsTested and working. Marking as correct answer. No other library is working for me.
-
smorgan over 2 yearsWhy would it break? Adding window support to the framework wouldn't have any effect on method channels.