screen_util.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import 'package:flutter/material.dart';
  2. import 'dart:ui' as ui show window;
  3. ///默认设计稿尺寸(单位 dp or pt)
  4. double _designW = 360.0;
  5. double _designH = 640.0;
  6. double _designD = 3.0;
  7. /**
  8. * 配置设计稿尺寸(单位 dp or pt)
  9. * w 宽
  10. * h 高
  11. * density 像素密度
  12. */
  13. /// 配置设计稿尺寸 屏幕 宽,高,密度。
  14. /// Configuration design draft size screen width, height, density.
  15. void setDesignWHD(double? w, double? h, {double? density = 3.0}) {
  16. _designW = w ?? _designW;
  17. _designH = h ?? _designH;
  18. _designD = density ?? _designD;
  19. }
  20. /// Screen Util.
  21. class ScreenUtil {
  22. double _screenWidth = 0.0;
  23. double _screenHeight = 0.0;
  24. double _screenDensity = 0.0;
  25. double _statusBarHeight = 0.0;
  26. double _bottomBarHeight = 0.0;
  27. double _appBarHeight = 0.0;
  28. MediaQueryData? _mediaQueryData;
  29. static final ScreenUtil _singleton = ScreenUtil();
  30. static ScreenUtil getInstance() {
  31. _singleton._init();
  32. return _singleton;
  33. }
  34. _init() {
  35. MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
  36. if (_mediaQueryData != mediaQuery) {
  37. _mediaQueryData = mediaQuery;
  38. _screenWidth = mediaQuery.size.width;
  39. _screenHeight = mediaQuery.size.height;
  40. _screenDensity = mediaQuery.devicePixelRatio;
  41. _statusBarHeight = mediaQuery.padding.top;
  42. _bottomBarHeight = mediaQuery.padding.bottom;
  43. _appBarHeight = kToolbarHeight;
  44. }
  45. }
  46. /// screen width
  47. /// 屏幕 宽
  48. double get screenWidth => _screenWidth;
  49. /// screen height
  50. /// 屏幕 高
  51. double get screenHeight => _screenHeight;
  52. /// appBar height
  53. /// appBar 高
  54. double get appBarHeight => _appBarHeight;
  55. /// screen density
  56. /// 屏幕 像素密度
  57. double get screenDensity => _screenDensity;
  58. /// status bar Height
  59. /// 状态栏高度
  60. double get statusBarHeight => _statusBarHeight;
  61. /// bottom bar Height
  62. double get bottomBarHeight => _bottomBarHeight;
  63. /// media Query Data
  64. MediaQueryData? get mediaQueryData => _mediaQueryData;
  65. /// screen width
  66. /// 当前屏幕 宽
  67. static double getScreenW(BuildContext context) {
  68. MediaQueryData mediaQuery = MediaQuery.of(context);
  69. return mediaQuery.size.width;
  70. }
  71. /// screen height
  72. /// 当前屏幕 高
  73. static double getScreenH(BuildContext context) {
  74. MediaQueryData mediaQuery = MediaQuery.of(context);
  75. return mediaQuery.size.height;
  76. }
  77. /// screen density
  78. /// 当前屏幕 像素密度
  79. static double getScreenDensity(BuildContext context) {
  80. MediaQueryData mediaQuery = MediaQuery.of(context);
  81. return mediaQuery.devicePixelRatio;
  82. }
  83. /// status bar Height
  84. /// 当前状态栏高度
  85. static double getStatusBarH(BuildContext context) {
  86. MediaQueryData mediaQuery = MediaQuery.of(context);
  87. return mediaQuery.padding.top;
  88. }
  89. /// status bar Height
  90. /// 当前BottomBar高度
  91. static double getBottomBarH(BuildContext context) {
  92. MediaQueryData mediaQuery = MediaQuery.of(context);
  93. return mediaQuery.padding.bottom;
  94. }
  95. /// 当前MediaQueryData
  96. static MediaQueryData getMediaQueryData(BuildContext context) {
  97. MediaQueryData mediaQuery = MediaQuery.of(context);
  98. return mediaQuery;
  99. }
  100. /// 仅支持纵屏。
  101. /// returns the size after adaptation according to the screen width.(unit dp or pt)
  102. /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
  103. /// size 单位 dp or pt
  104. static double getScaleW(BuildContext context, double size) {
  105. if (getScreenW(context) == 0.0) return size;
  106. return size * getScreenW(context) / _designW;
  107. }
  108. /// 仅支持纵屏。
  109. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  110. /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
  111. /// size unit dp or pt
  112. static double getScaleH(BuildContext context, double size) {
  113. if (getScreenH(context) == 0.0) return size;
  114. return size * getScreenH(context) / _designH;
  115. }
  116. /// 仅支持纵屏。
  117. /// returns the font size after adaptation according to the screen density.
  118. /// 返回根据屏幕宽适配后字体尺寸
  119. /// fontSize 字体尺寸
  120. static double getScaleSp(BuildContext context, double fontSize) {
  121. if (getScreenW(context) == 0.0) return fontSize;
  122. return fontSize * getScreenW(context) / _designW;
  123. }
  124. /// Orientation
  125. /// 设备方向(portrait, landscape)
  126. static Orientation getOrientation(BuildContext context) {
  127. MediaQueryData mediaQuery = MediaQuery.of(context);
  128. return mediaQuery.orientation;
  129. }
  130. /// 仅支持纵屏。
  131. /// returns the size after adaptation according to the screen width.(unit dp or pt)
  132. /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
  133. /// size 单位 dp or pt
  134. double getWidth(double size) {
  135. return _screenWidth == 0.0 ? size : (size * _screenWidth / _designW);
  136. }
  137. /// 仅支持纵屏。
  138. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  139. /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
  140. /// size unit dp or pt
  141. double getHeight(double size) {
  142. return _screenHeight == 0.0 ? size : (size * _screenHeight / _designH);
  143. }
  144. /// 仅支持纵屏
  145. /// returns the size after adaptation according to the screen width.(unit dp or pt)
  146. /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
  147. /// sizePx unit px
  148. double getWidthPx(double sizePx) {
  149. return _screenWidth == 0.0
  150. ? (sizePx / _designD)
  151. : (sizePx * _screenWidth / (_designW * _designD));
  152. }
  153. /// 仅支持纵屏。
  154. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  155. /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
  156. /// sizePx unit px
  157. double getHeightPx(double sizePx) {
  158. return _screenHeight == 0.0
  159. ? (sizePx / _designD)
  160. : (sizePx * _screenHeight / (_designH * _designD));
  161. }
  162. /// 仅支持纵屏。
  163. /// returns the font size after adaptation according to the screen density.
  164. /// 返回根据屏幕宽适配后字体尺寸
  165. /// fontSize 字体尺寸
  166. double getSp(double fontSize) {
  167. if (_screenDensity == 0.0) return fontSize;
  168. return fontSize * _screenWidth / _designW;
  169. }
  170. /// 兼容横/纵屏。
  171. /// 获取适配后的尺寸,兼容横/纵屏切换,可用于宽,高,字体尺寸适配。
  172. /// Get the appropriate size, compatible with horizontal/vertical screen switching, can be used for wide, high, font size adaptation.
  173. double getAdapterSize(double dp) {
  174. if (_screenWidth == 0 || _screenHeight == 0) return dp;
  175. return getRatio() * dp;
  176. }
  177. /// 适配比率。
  178. /// Ratio.
  179. double getRatio() {
  180. return (_screenWidth > _screenHeight ? _screenHeight : _screenWidth) /
  181. _designW;
  182. }
  183. /// 兼容横/纵屏。
  184. /// 获取适配后的尺寸,兼容横/纵屏切换,适应宽,高,字体尺寸。
  185. /// Get the appropriate size, compatible with horizontal/vertical screen switching, can be used for wide, high, font size adaptation.
  186. static double getAdapterSizeCtx(BuildContext context, double dp) {
  187. Size size = MediaQuery.of(context).size;
  188. if (size == Size.zero) return dp;
  189. return getRatioCtx(context) * dp;
  190. }
  191. /// 适配比率。
  192. /// Ratio.
  193. static double getRatioCtx(BuildContext context) {
  194. Size size = MediaQuery.of(context).size;
  195. return (size.width > size.height ? size.height : size.width) / _designW;
  196. }
  197. }