screen_util.dart 7.4 KB

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