store flutter MediaQuery values in another class - flutter

is it possible to store flutter mediaquery values inside another .dart file with a variable and retrieve it anytime needed?
something like below?
class MediaQueryValues{
function widthandheight(){
double mediawidth = MediaQuery.of(context).size.width;
double mediaheight = MediaQuery.of(context).size.height;
}
}

You could make an extension on the BuildContext class:
import 'package:flutter/material.dart';
extension MediaQueryValues on BuildContext {
double get width => MediaQuery.of(this).size.width;
double get height => MediaQuery.of(this).size.height.
}
Now import the class in the file you want to use it in, and you can write: context.width and context.height on your BuildContext and it will return the screen height and width.
I believe that that is the shortest syntax.

Related

Get.context in Flutter using GetX not working in release apk

I'm getting height and width of the screen size using Get.context!.height and Get.context!.width. This works perfect in Debug mode. But in the release APK, it's not working at all due to which all the elements based on these are disappearing.
I also tried using MediaQuery but as the height and width are being used inside a class that doesn't have any BuildContext, MediaQuery is not the solution. So I went with Get.context! which works great in debug version. Once I switch to release version, bam, it no longer works.
Here is the code which is not working in release mode but working in debug mode :
class Dimensions {
static double screenHeight = Get.context!.height;
static double screenWidth = Get.context!.width;
static double pageView = screenHeight / 3.08;
static double pageViewContainer = screenHeight / 4.00;
static double pageDetailsContainer = screenHeight / 7.40;
static double imageButtonSectionHeight = screenHeight * 0.195;
}
Try using Get.overlayContext instead of Get.context`.
I'm using Getx also, please if this also didn't work, consider passing the context as a parameter from the constructor of that class or from your method and use the MediaQuery.of(context) instead.
the BuildContext topic is essential, and it can also be problematic if used incorrectly.

Are logical pixels the units returned by the dx property of the Offset class?

Does the dx property of the Offset class return the double value of the logical pixels on the x-axis? I need to know if I can measure say a percentage of the dx against MediaQuery.of(context).size.width?
Short answer: The dx property of an Offset object and the Width size of a Media Query are both doubles (pixel, yes) thus you can divide, multiply or do any math operation with them (separately or with each other). So, you can do your OffsetObject.dx/MediaQuery(context).Size.Width to get the percentage. You can run the code block below on Dart pad.
Loooong Answer: Not sure it adds more info.
Yes, of course you can measure the dx property of an Offset object against the width property of a MediaQuery because they are both of type double.
The mere fact that they are both of type doubles should allow you to carry out any math/arithmetic calculation you need. See the sample app below. the text returns the numbers (you can run it on Dartpad). I explained the variables/objects defined below as well.
ScreenWidthSize: the MediaQuery width, note the type is double. MediaQuery.of(context).size.width returns an object of type double.
offsetMagnitude: This defines the Offset object (I am not sure how you will get your Offset object so this for illustration. Then I need to get the dx.
horizontalOffset: This defines/extracts the dx property of the offsetMagnitude, the offset object. Note it is a double too.
horizontalOffsetToScreenWidthRatio: Since both ScreenWidthSize and horizontalOffset are doubles, I can carry out any mathematical operation, as long as they are applicable to the type double. I have therefore measured the percentage (we can format the result better but you get it).
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
double screenWidthSize = MediaQuery.of(context).size.width;
Offset offsetMagnitude = Offset(12,25);
double horizontalOffset = offsetMagnitude.dx;
double horizontalOffsetToScreenWidthRatio = offsetMagnitude.dx/screenWidthSize;
return Text(
'Hello, my screen is $screenWidthSize wide and my horizontal offset is $horizontalOffset, so the horizontal offset to screen width ratio is $horizontalOffsetToScreenWidthRatio',
style: Theme.of(context).textTheme.headline4,
);
}
}
/* Not sure what you are trying to achieve but it seems like some form of adaptive or responsive operation. I think if you check the docs, you will find a close widget needing only little manipulation to achieve what you want. Check the Align widget, might be what you need*/

Flutter responsive layout by enclosing everything in a root widget / How to change all double values of descendant widgets using one root widget

I was writing my own responsive layouts like this:
Instead of writing Normal code
Container(padding: EdgeInsets.all(10)),
Text("abc", style: TextStyle(fontSize: 20)),
I used a function responsiveSize()
Container(padding: EdgeInsets.all(responsiveSize(10, context))),
Text("abc", style: TextStyle(fontSize: responsiveSize(20, context))),
and define the function responsiveSize() as
double responsiveSize(double number, BuildContext context){
if (MediaQuery.of(context).size.width < 450) {
return number;
} else {
return number * 1.5;
}
}
or something like this which I can easily change.
But the problem with this is that I have to wrap every double value in my code with the function responsiveSize(), which is very tedious. I am looking for a way to wrap all my widgets in one root widget, like wrapping my MaterialApp widget inside a ResponsiveSize widget:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ResponsiveSize(
child: MaterialApp(
home: HomePage(),
),
);
}
}
and writing Normal code (as mentioned above, use double values as it is without wrapping them in a responsiveSize() function) and define ResponsiveSize widget in some way which I don't know. This way I can use responsive layout by just one widget and can easily remove the root ResponsiveSize widget if I don't want it later.
However this involves changing all double values in all descendant widgets of the ResponsiveSize widget, and this is something which I don't know how to do.
So, I would like to know how I can change all double values of all descendant widgets of a root widget? How should I define the root widget?
The LayoutBuilder widget might be what you're looking for.

Flutter web get screen size

I first time try flutter for web and build a project on top off existing project according to this documentation https://flutter.dev/web. Project run fine but in chrome half off my application is out off screen. I tried to get screen size with MediaQuery.of(context) and SafeArea() but nothing happens. What is right way to get screen size?
In Flutter, I've been attempting to determine the size of the entire context view. You may get width or height by using MediaQuery with the current context of your widget
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
Around your widget, you'll need a MaterialApp or a WidgetsApp. They are the ones who give the MediaQuery. Flutter will always look up the widget tree to find the widget when you call.of(context).
You can Improve responsiveness by adding Functions like
static bool isSmallScreen(BuildContext context) {
return MediaQuery.of(context).size.width < 800;
}
static bool isLargeScreen(BuildContext context) {
return MediaQuery.of(context).size.width > 1200;
}
static bool isMediumScreen(BuildContext context) {
return MediaQuery.of(context).size.width >= 800 &&
MediaQuery.of(context).size.width <= 1200;
}
In my opinion, don't use a garbage library that doesn't support multiplatform and don't have above 100 pub points instead of create your own functions to do stuff and also you can use expand which expand the screen according to the screen size
don't give size like
height:100
weight:100
it may lead to screen crashing when we use it on multiple screen

How do you access mediaquery.of(context).width.height outside of a build method?

I don't specifically need to access the mediaquery method, just anything that will return a the native devices height and width. What I'm trying to achieve is a reactive theme that will scale text sizes based on a device's size.
This is exactly what I do in my apps, I don't want to use mediacontext too(as sometimes I don't have the context), take a look at the following example:
https://github.com/lhcdims/statemanagement02
Try to look at the class 'sv' inside lib/ScreenVariables.dart
And call sv.init() inside main.dart, then you can use sv.screenwidth, sv.defaultfontsize ...etc anywhere in any dart files.
It is simple to do that. This is how I do it in a StatefulWidget:
class MyPage extends StatefulWidget {
#override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
var screenHeight = window.physicalSize.height / window.devicePixelRatio;
var screenWidth = window.physicalSize.width / window.devicePixelRatio;
. . . .
. . . .
. . . .
You can't access the Context object outside the build method directly. You can either pass the Context object through from inside the build method to the separate build function.
Widget build(BuildContext context){
return Container(
children: buildChildren(context)
);
}
Widget buildChildren(BuildContext context){
...
}