3

Firstly, this, though similiar in Question, is not what I need as the Answer does not help me in my case.

I have my AppConfig decide the fontsize, based on device, so that very big devices, such as Tablets, get bigger Texts (by a bit). My config gets called before any Views, so I have no BuildContext while this is running. So how can I do this?

class AppConfig {
  static AppConfig _instance;

  static AppConfig instance() {
    if (_instance == null) {
      _instance = AppConfig();
    }
    return _instance;
  }

// config stuff for colours and themes

double width = MediaQuery.of(context).size.width;

if (width > 500) { //arbitrary number I haven't decided yet
    initWithBigFonts();
} else {
    initWithSmallFonts();
}
3
  • 2
    what about WidgetsBinding.instance.window?
    – pskink
    Commented Feb 19, 2021 at 12:02
  • @pskink that looks like what i'd need but - how would i use it here?
    – Maritn Ge
    Commented Feb 19, 2021 at 12:12
  • check Window class properties
    – pskink
    Commented Feb 19, 2021 at 12:34

6 Answers 6

13

For future reference the full solution to this:

By adding

Size size = WidgetsBinding.instance.window.physicalSize;
double width = size.width;
double height = size.height;

You can, from anywhere and without BuildContext, get the Size from the device screen.

1
  • The windows property has been marked as deprecated, should be used the platformDispatcher property now.
    – krrskl
    Commented Jul 17 at 16:41
6

in this case, you should think about the devicePixelRatio;

Size size = WidgetsBinding.instance.window.physicalSize;
double ratio = WidgetsBinding.instance.window.devicePixelRatio;
double width = size.width / ratio;
4

the "window" singleton has been deprecated as of v3.7.0-32.0.pre because "window" assumed that the app consists of a single view which is not the case anymore. You can use one of the following two options.

  • PlatformDispatcher.instance.views.first.physicalSize. or,
  • PlatformDispatcher.instance.implicitView?.physicalSize (nullable)

divide the physicalSize by the devicePixelRatio (PlatformDispatcher.instance.views.first.devicePixelRatio) to get the view size.

For more info, https://docs.flutter.dev/release/breaking-changes/window-singleton

1

Size size = MediaQueryData.fromWindow(WidgetsBinding.instance.window).size;

1

Update

window is deprecated and shouldn't be used. Instead use PlatformDispatcher.

final view = WidgetsBinding.instance.platformDispatcher.views.first;
final size = view.physicalSize;
final pixelRatio = view.devicePixelRatio;

// Size in physical pixels:
final width = size.width;
final height = size.height;

// Size in logical pixels:
final width = size.width / pixelRatio;
final height = size.height / pixelRatio;
0

Update for the answer marked as useful

From the flutter version v3.7.0-32.0.pre the window property has been marked as deprecated, should be used the PlatformDispatcher property now.

final view = WidgetsBinding.instance.platformDispatcher.views.first;
final size = view.physicalSize;
final width = size.width / view.devicePixelRatio;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.