screen_util.dart 7.8 KB

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