thl 7 years ago
parent
commit
b73936e3ae
3 changed files with 95 additions and 35 deletions
  1. 9 4
      example/lib/main.dart
  2. 84 29
      lib/src/screen_util.dart
  3. 2 2
      pubspec.yaml

+ 9 - 4
example/lib/main.dart

@@ -89,14 +89,19 @@ class MainPageState extends State<MainPage> {
 
 
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
-    double width = ScreenUtil().screenWidth;
-    double height = ScreenUtil().screenHeight;
+    // 如果使用ScreenUtil.getInstance() 需要MainPageState build 调用MediaQuery.of(context)
+    MediaQuery.of(context);
+
+    double width = ScreenUtil.getInstance().screenWidth;
+    double height = ScreenUtil.getInstance().screenHeight;
     double density = ScreenUtil.getInstance().screenDensity;
     double density = ScreenUtil.getInstance().screenDensity;
     double tempW = ScreenUtil.getInstance().getWidth(360.0);
     double tempW = ScreenUtil.getInstance().getWidth(360.0);
-    double tempH = ScreenUtil().getHeight(360.0);
+    double tempH = ScreenUtil.getInstance().getHeight(360.0);
+    double textScaleFactor =
+        ScreenUtil.getInstance().mediaQueryData.textScaleFactor;
 
 
     print(
     print(
-        "width: $width, height: $height, density: $density, tempW: $tempW, tempH: $tempH");
+        "width: $width, height: $height, density: $density, tempW: $tempW, tempH: $tempH, textScaleFactor: $textScaleFactor");
     double _width = width * density;
     double _width = width * density;
     double _height = height * density;
     double _height = height * density;
     double __tempW = ScreenUtil.getInstance().getWidthPx(90.0);
     double __tempW = ScreenUtil.getInstance().getWidthPx(90.0);

+ 84 - 29
lib/src/screen_util.dart

@@ -29,35 +29,35 @@ void setDesignWHD(double w, double h, {double density: 3.0}) {
 
 
 /// Screen Util.
 /// Screen Util.
 class ScreenUtil {
 class ScreenUtil {
-  double _screenWidth;
-  double _screenHeight;
-  double _screenDensity;
-  double _statusBarHeight;
-  double _bottomBarHeight;
-  double _appBarHeight;
-  double _textScaleFactor;
+  double _screenWidth = 0.0;
+  double _screenHeight = 0.0;
+  double _screenDensity = 0.0;
+  double _statusBarHeight = 0.0;
+  double _bottomBarHeight = 0.0;
+  double _appBarHeight = 0.0;
+  double _textScaleFactor = 0.0;
   MediaQueryData _mediaQueryData;
   MediaQueryData _mediaQueryData;
 
 
-  static final ScreenUtil _singleton = ScreenUtil._init();
+  static final ScreenUtil _singleton = ScreenUtil();
 
 
   static ScreenUtil getInstance() {
   static ScreenUtil getInstance() {
+    _singleton._init();
     return _singleton;
     return _singleton;
   }
   }
 
 
-  factory ScreenUtil() {
-    return _singleton;
-  }
 
 
-  ScreenUtil._init() {
+  _init() {
     MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
     MediaQueryData mediaQuery = MediaQueryData.fromWindow(ui.window);
-    _mediaQueryData = mediaQuery;
-    _screenWidth = mediaQuery.size.width;
-    _screenHeight = mediaQuery.size.height;
-    _screenDensity = mediaQuery.devicePixelRatio;
-    _statusBarHeight = mediaQuery.padding.top;
-    _bottomBarHeight = mediaQuery.padding.bottom;
-    _textScaleFactor = mediaQuery.textScaleFactor;
-    _appBarHeight = kToolbarHeight;
+    if (_mediaQueryData != mediaQuery) {
+      _mediaQueryData = mediaQuery;
+      _screenWidth = mediaQuery.size.width;
+      _screenHeight = mediaQuery.size.height;
+      _screenDensity = mediaQuery.devicePixelRatio;
+      _statusBarHeight = mediaQuery.padding.top;
+      _bottomBarHeight = mediaQuery.padding.bottom;
+      _textScaleFactor = mediaQuery.textScaleFactor;
+      _appBarHeight = kToolbarHeight;
+    }
   }
   }
 
 
   /// screen width
   /// screen width
@@ -88,16 +88,68 @@ class ScreenUtil {
 
 
   /// screen width
   /// screen width
   /// 当前屏幕 宽
   /// 当前屏幕 宽
-  static double getScreenWidth(BuildContext context) {
+  static double getScreenW(BuildContext context) {
     MediaQueryData mediaQuery = MediaQuery.of(context);
     MediaQueryData mediaQuery = MediaQuery.of(context);
     return mediaQuery.size.width;
     return mediaQuery.size.width;
   }
   }
 
 
-  /// screen height
-  /// 当前屏幕 高
-  static double getScreenHeight(BuildContext context) {
+  /// screen width
+  /// 当前屏幕 宽
+  static double getScreenH(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.size.width;
+  }
+
+  /// screen density
+  /// 当前屏幕 像素密度
+  static double getScreenDensity(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.devicePixelRatio;
+  }
+
+  /// status bar Height
+  /// 当前状态栏高度
+  static double getStatusBarH(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery.padding.top;
+  }
+
+  /// status bar Height
+  /// 当前状态栏高度
+  static double getBottomBarH(BuildContext context) {
     MediaQueryData mediaQuery = MediaQuery.of(context);
     MediaQueryData mediaQuery = MediaQuery.of(context);
-    return mediaQuery.size.height;
+    return mediaQuery.padding.bottom;
+  }
+
+  /// 当前MediaQueryData
+  static MediaQueryData getMediaQueryData(BuildContext context) {
+    MediaQueryData mediaQuery = MediaQuery.of(context);
+    return mediaQuery;
+  }
+
+  /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
+  /// size 单位 dp or pt
+  static double getScaleW(BuildContext context, double size) {
+    if (context == null || getScreenW(context) == 0.0) return size;
+    return size * getScreenW(context) / _designW;
+  }
+
+  /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
+  /// size 单位 dp or pt
+  static double getScaleH(BuildContext context, double size) {
+    if (context == null || getScreenH(context) == 0.0) return size;
+    return size * getScreenH(context) / _designH;
+  }
+
+  /// 返回根据屏幕宽适配后字体尺寸
+  /// fontSize 字体尺寸
+  /// sySystem 是否跟随系统字体大小设置,默认 true。
+  static double getScaleSp(BuildContext context, double fontSize,
+      {bool sySystem: true}) {
+    if (context == null || getScreenW(context) == 0.0) return fontSize;
+    return (sySystem ? MediaQuery
+        .of(context)
+        .textScaleFactor : 1.0) * fontSize * getScreenW(context) / _designW;
   }
   }
 
 
   /// Orientation
   /// Orientation
@@ -110,31 +162,34 @@ class ScreenUtil {
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// size 单位 dp or pt
   /// size 单位 dp or pt
   double getWidth(double size) {
   double getWidth(double size) {
-    return size * _screenWidth / _designW;
+    return _screenWidth == 0.0 ? size : (size * _screenWidth / _designW);
   }
   }
 
 
   /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
   /// 返回根据屏幕高适配后尺寸 (单位 dp or pt)
   /// size 单位 dp or pt
   /// size 单位 dp or pt
   double getHeight(double size) {
   double getHeight(double size) {
-    return size * _screenHeight / _designH;
+    return _screenHeight == 0.0 ? size : (size * _screenHeight / _designH);
   }
   }
 
 
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// 返回根据屏幕宽适配后尺寸(单位 dp or pt)
   /// sizePx 单位px
   /// sizePx 单位px
   double getWidthPx(double sizePx) {
   double getWidthPx(double sizePx) {
-    return sizePx * _screenWidth / (_designW * _designD);
+    return _screenWidth == 0.0 ? (sizePx / _designD) :
+    (sizePx * _screenWidth / (_designW * _designD));
   }
   }
 
 
   /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
   /// 返回根据屏幕高适配后尺寸(单位 dp or pt)
   /// sizePx 单位px
   /// sizePx 单位px
   double getHeightPx(double sizePx) {
   double getHeightPx(double sizePx) {
-    return sizePx * _screenHeight / (_designH * _designD);
+    return _screenHeight == 0.0 ? (sizePx / _designD) :
+    (sizePx * _screenHeight / (_designH * _designD));
   }
   }
 
 
   /// 返回根据屏幕宽适配后字体尺寸
   /// 返回根据屏幕宽适配后字体尺寸
   /// fontSize 字体尺寸
   /// fontSize 字体尺寸
   /// sySystem 是否跟随系统字体大小设置,默认 true。
   /// sySystem 是否跟随系统字体大小设置,默认 true。
   double getSp(double fontSize, {bool sySystem: true}) {
   double getSp(double fontSize, {bool sySystem: true}) {
+    if (_screenWidth == 0.0) return fontSize;
     return (sySystem ? _textScaleFactor : 1.0) *
     return (sySystem ? _textScaleFactor : 1.0) *
         fontSize *
         fontSize *
         _screenWidth /
         _screenWidth /

+ 2 - 2
pubspec.yaml

@@ -1,6 +1,6 @@
 name: flustars
 name: flustars
 description: Flutter common utils library. DioUtil, SpUtil, ScreenUtil, WidgetUtil.
 description: Flutter common utils library. DioUtil, SpUtil, ScreenUtil, WidgetUtil.
-version: 0.1.7
+version: 0.1.8
 author: thl <863764940@qq.com>
 author: thl <863764940@qq.com>
 homepage: https://github.com/Sky24n/flustars
 homepage: https://github.com/Sky24n/flustars
 
 
@@ -16,7 +16,7 @@ dependencies:
   # https://github.com/flutter/plugins/tree/master/packages/shared_preferences
   # https://github.com/flutter/plugins/tree/master/packages/shared_preferences
   shared_preferences: ^0.4.3
   shared_preferences: ^0.4.3
   # https://github.com/flutterchina/dio
   # https://github.com/flutterchina/dio
-  dio: ^1.0.12
+  dio: ^1.0.13
 
 
 
 
 # For information on the generic Dart part of this file, see the
 # For information on the generic Dart part of this file, see the