skip to Main Content

How can I get screen size in Flutter without passing context after the deprecation of MediaQueryData.fromWindow(WidgetsBinding.instance.window). The new alternative method recommended by flutter is MediaQueryData.fromView(View.of(context)).size.width which involves the passing of context.

2

Answers


  1. You shouldn’t need the "screen size" in any location where context is not available.

    And you don’t need MediaQuery either. Use LayoutBuilder, which gives you the pixels remaining in your current layout, which is the only number you should be looking at when making layout breakpoint decisions.

    Login or Signup to reply.
  2. The ‘height’ here would return the same value as MediaQuery.of(context).size.height without context:

    final double physicalHeight = WidgetsBinding.instance.platformDispatcher.views.first.physicalSize.height;
    final double devicePixelRatio =
          WidgetsBinding.instance.platformDispatcher.views.first.devicePixelRatio;
    final double height = physicalHeight / devicePixelRatio;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search