import 'package:flutter/material.dart';
class Home extends StatelessWidget {
const Home({super.key});
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.sizeOf(context).width;
double screenHeight = MediaQuery.sizeOf(context).height;
return SafeArea(
child: Scaffold(),
);
}
}
What is the best way to declare MediaQuery objects? That should not effect the performance of the Flutter applciation.
4
Answers
I got a solution. Let me know is it correct or not
I think you can measure the size values inside the build() method of root page, because root page in most cases won’t rebuild so it won’t gain the performance
Then store these values in global variables, which can be accessed anywhere in the app
Or even write down values to local storage and then read it next time without recalculation
Use an extension to get it everywhere using context as a reference.
You can create shortcut with extension class like
And there is how to use it