1

I am developing app in flutter. I want to store width and height of a screen in a variable using media query and want to use it globally in whole app. How to do this?

  • You can create a singleton check [this](https://stackoverflow.com/questions/12649573/how-do-you-build-a-singleton-in-dart) – fayeed Jan 12 '20 at 19:11
  • 1
    I use the belt and braces approach, I have a 'global_configs.dart' code file that contains all the variables I need to reference across the app (those not handled by scoped_model). I just import it in to all other .dart files that need it. Singletons and inherited widgets are something I will look at later...using the 'kiss' approach to get things working – GrahamD Jan 12 '20 at 19:27
  • 1
    That information is already available globally. Simply import `dart:ui` and read `window` – Rémi Rousselet Jan 12 '20 at 21:06

1 Answers1

2

Create a class Common.dart

class Common{
  static  int WIDTH;
  static  int HEIGHT;
}

In your page class, set WIDTH and HEIGHT and access from all classes.

  @override
  Widget build(BuildContext context) {
  Common.WIDTH = MediaQuery.of(context).size.width; //set width to static variable
  Common.HEIGHT = MediaQuery.of(context).size.height;//set height to static variable
  }

Getting WIDTH and HEIGHT

 var tempWidth =  Common.WIDTH;  
 var tempHeight =  Common.HEIGHT;
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46