Skip to content

Instantly share code, notes, and snippets.

@xamantra
Created January 18, 2020 07:09
Show Gist options
  • Save xamantra/53c2bf8902b23dfbfde6cd128c5b2959 to your computer and use it in GitHub Desktop.
Save xamantra/53c2bf8902b23dfbfde6cd128c5b2959 to your computer and use it in GitHub Desktop.
RelativeScaler is a custom sizing system for flutter widgets to achieve the same physical sizes across different devices.
mixin RelativeScaler {
double _screenHeight;
double _screenWidth;
/// `RelativeScaler.screenHeight` -> the height of the screen.
double get screenHeight => _screenHeight;
/// `RelativeScaler.screenWidth` -> the width of the screen.
double get screenWidth => _screenWidth;
/// Initialize `RelativeScaler`.
initRelativeScaler(BuildContext context) {
var size = MediaQuery.of(context).size;
_screenHeight = size.height;
_screenWidth = size.width;
}
/// Size relative to the `height` of the screen.
double sy(double value) {
return (_screenHeight * _calculate(value)).roundToDouble();
}
/// Size relative to the `width` of the screen.
double sx(double value) {
return (_screenWidth * _calculate(value)).roundToDouble();
}
double _calculate(double value) {
return (value / 100) / 5.333333333333333;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment