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.

Share:
473
exceptionsAreBad
Author by

exceptionsAreBad

Updated on January 02, 2023

Comments

  • exceptionsAreBad
    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/21

    pubspec.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
    exceptionsAreBad over 2 years
    Its returning my applications window size, not the users screen size.
  • Stijn2210
    Stijn2210 over 2 years
    Well just maximize the screen using the plugin and then measure the size
  • exceptionsAreBad
    exceptionsAreBad over 2 years
    Yea 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
    Stijn2210 over 2 years
    Weird, I guess desktop apps still have some hiccups to be worked out
  • exceptionsAreBad
    exceptionsAreBad over 2 years
    Thanks. 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
    exceptionsAreBad over 2 years
    Tested and working. Marking as correct answer. No other library is working for me.
  • smorgan
    smorgan over 2 years
    Why would it break? Adding window support to the framework wouldn't have any effect on method channels.