Created
April 21, 2020 03:11
-
-
Save zaprogrammer/73c62075720c4ed5499a65200270a04d to your computer and use it in GitHub Desktop.
[Flutter snippets] Flutter useful snippets #flutter #dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Get screen width & height | |
MediaQueryData queryData = MediaQuery.of(context); | |
int width = queryData.size.width; | |
int height = queryData.size.height; | |
//Device pixel ratio | |
int ratio = queryData.devicePixelRatio; | |
//Get text scale factor | |
int textScale = queryData.textScaleFactor; | |
//Take screen width and height and calculate a grid of 100*100 out of it to position and scale things and save it as static variables that can be reused | |
AppConfig.width = MediaQuery.of(context).size.width; | |
AppConfig.height = MediaQuery.of(context).size.height; | |
AppConfig.blockSize = AppConfig.width / 100; | |
AppConfig.blockSizeVertical = AppConfig.height / 100; | |
//Then scale everything according to above numbers | |
double elementWidth = AppConfig.blockSize * 10.0; // 10% of the screen width | |
double fontSize = AppConfig.blockSize * 1.2; | |
//Fix for safearea (like notch, etc) | |
AppConfig.safeAreaHorizontal = MediaQuery.of(context).padding.left + | |
MediaQuery.of(context).padding.right; | |
double screenWidthWithoutSafeArea = AppConfig.width - AppConfig.safeAreaHorizontal; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment