screen_util.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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;
  25. _designH = h;
  26. _designD = density;
  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. /// returns the size after adaptation according to the screen width.(unit dp or pt)
  109. /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
  110. /// size 单位 dp or pt
  111. static double getScaleW(BuildContext context, double size) {
  112. if (context == null || getScreenW(context) == 0.0) return size;
  113. return size * getScreenW(context) / _designW;
  114. }
  115. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  116. /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
  117. /// size unit dp or pt
  118. static double getScaleH(BuildContext context, double size) {
  119. if (context == null || getScreenH(context) == 0.0) return size;
  120. return size * getScreenH(context) / _designH;
  121. }
  122. /// returns the font size after adaptation according to the screen density.
  123. /// 返回根据屏幕宽适配后字体尺寸
  124. /// fontSize 字体尺寸
  125. static double getScaleSp(BuildContext context, double fontSize) {
  126. if (context == null || getScreenDensity(context) == 0.0) return fontSize;
  127. return fontSize * getScreenW(context) / _designW;
  128. }
  129. /// Orientation
  130. /// 设备方向(portrait, landscape)
  131. static Orientation getOrientation(BuildContext context) {
  132. MediaQueryData mediaQuery = MediaQuery.of(context);
  133. return mediaQuery.orientation;
  134. }
  135. /// returns the size after adaptation according to the screen width.(unit dp or pt)
  136. /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
  137. /// size 单位 dp or pt
  138. double getWidth(double size) {
  139. return _screenWidth == 0.0 ? size : (size * _screenWidth / _designW);
  140. }
  141. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  142. /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
  143. /// size unit dp or pt
  144. double getHeight(double size) {
  145. return _screenHeight == 0.0 ? size : (size * _screenHeight / _designH);
  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
  152. ? (sizePx / _designD)
  153. : (sizePx * _screenWidth / (_designW * _designD));
  154. }
  155. /// returns the size after adaptation according to the screen height.(unit dp or pt)
  156. /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
  157. /// sizePx unit px
  158. double getHeightPx(double sizePx) {
  159. return _screenHeight == 0.0
  160. ? (sizePx / _designD)
  161. : (sizePx * _screenHeight / (_designH * _designD));
  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. }