Search code examples
flutter

Get screen size in flutter without having to pass context and without layout builder in flutter after deprecation of MediaQueryData.fromWindow()?


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.


Solution

  • 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;