Showing
13 changed files
with
275 additions
and
172 deletions
| 1 | -# Generated by pub on 2020-01-06 17:46:26.285635. | 1 | +# Generated by pub on 2020-03-17 15:00:35.134973. |
| 2 | archive:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/archive-2.0.11/lib/ | 2 | archive:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/archive-2.0.11/lib/ |
| 3 | args:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.5.2/lib/ | 3 | args:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/args-1.5.2/lib/ |
| 4 | async:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.4.0/lib/ | 4 | async:file:///D:/flutter/.pub-cache/hosted/pub.dartlang.org/async-2.4.0/lib/ |
| @@ -5,6 +5,14 @@ | @@ -5,6 +5,14 @@ | ||
| 5 | * @LastEditTime: 2020年1月14日 12:11:02 | 5 | * @LastEditTime: 2020年1月14日 12:11:02 |
| 6 | * @Description: Update log | 6 | * @Description: Update log |
| 7 | --> | 7 | --> |
| 8 | + | ||
| 9 | +#1.1.0 | ||
| 10 | +- support ExtensionMethod Dart-SDK-2.6.0 | ||
| 11 | +- you can use 'width: 50.w' instead of 'width: ScreenUtil().setWidth(50)' | ||
| 12 | + '50.h' instead of 'ScreenUtil().setHeight(50)' | ||
| 13 | + '24.sp' instead of 'ScreenUtil().setSp(24)' | ||
| 14 | + '24.ssp' instead of 'ScreenUtil().setSp(24, allowFontScalingSelf: true)' | ||
| 15 | + | ||
| 8 | # 1.0.2 | 16 | # 1.0.2 |
| 9 | - fix #89 | 17 | - fix #89 |
| 10 | - 优化屏幕旋转效果 | 18 | - 优化屏幕旋转效果 |
| @@ -60,6 +60,25 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | @@ -60,6 +60,25 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | ||
| 60 | 60 | ||
| 61 | ### Use: | 61 | ### Use: |
| 62 | 62 | ||
| 63 | +#### API | ||
| 64 | + | ||
| 65 | +```dart | ||
| 66 | + ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //Adapted to screen width | ||
| 67 | + ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //Adapted to screen height | ||
| 68 | + ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //Adapter font | ||
| 69 | + ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings) | ||
| 70 | + | ||
| 71 | + ScreenUtil.pixelRatio //Device pixel density | ||
| 72 | + ScreenUtil.screenWidth //Device width | ||
| 73 | + ScreenUtil.screenHeight //Device height | ||
| 74 | + ScreenUtil.bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen | ||
| 75 | + ScreenUtil.statusBarHeight //Status bar height , Notch will be higher Unit px | ||
| 76 | + ScreenUtil.textScaleFactor //System font scaling factor | ||
| 77 | + | ||
| 78 | + ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px | ||
| 79 | + ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px | ||
| 80 | +``` | ||
| 81 | + | ||
| 63 | #### Adapt screen size: | 82 | #### Adapt screen size: |
| 64 | 83 | ||
| 65 | Pass the px size of the design draft: | 84 | Pass the px size of the design draft: |
| @@ -68,6 +87,23 @@ Adapted to screen width: `ScreenUtil().setWidth(540)`, | @@ -68,6 +87,23 @@ Adapted to screen width: `ScreenUtil().setWidth(540)`, | ||
| 68 | 87 | ||
| 69 | Adapted to screen height: `ScreenUtil().setHeight(200)`, | 88 | Adapted to screen height: `ScreenUtil().setHeight(200)`, |
| 70 | 89 | ||
| 90 | +If your dart sdk>=2.6, you can use extension functions: | ||
| 91 | + | ||
| 92 | +example: | ||
| 93 | +instead of : | ||
| 94 | +``` | ||
| 95 | +Container( | ||
| 96 | +width: ScreenUtil().setWidth(50), | ||
| 97 | +height:ScreenUtil().setHeight(200), | ||
| 98 | +) | ||
| 99 | +``` | ||
| 100 | +you can use it like this: | ||
| 101 | +``` | ||
| 102 | +Container( | ||
| 103 | +width: 50.w, | ||
| 104 | +height:200.h | ||
| 105 | +) | ||
| 106 | +``` | ||
| 71 | **Note** | 107 | **Note** |
| 72 | 108 | ||
| 73 | Height is also adapted according to setWidth to ensure no deformation (when you want a square) | 109 | Height is also adapted according to setWidth to ensure no deformation (when you want a square) |
| @@ -122,20 +158,6 @@ Column( | @@ -122,20 +158,6 @@ Column( | ||
| 122 | ) | 158 | ) |
| 123 | ``` | 159 | ``` |
| 124 | 160 | ||
| 125 | -#### Other related apis: | ||
| 126 | -```dart | ||
| 127 | - ScreenUtil.pixelRatio //Device pixel density | ||
| 128 | - ScreenUtil.screenWidth //Device width | ||
| 129 | - ScreenUtil.screenHeight //Device height | ||
| 130 | - ScreenUtil.bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen | ||
| 131 | - ScreenUtil.statusBarHeight //Status bar height , Notch will be higher Unit px | ||
| 132 | - ScreenUtil.textScaleFactor //System font scaling factor | ||
| 133 | - | ||
| 134 | - ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px | ||
| 135 | - ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px | ||
| 136 | - | ||
| 137 | -``` | ||
| 138 | - | ||
| 139 | ```dart | 161 | ```dart |
| 140 | //import | 162 | //import |
| 141 | import 'package:flutter/material.dart'; | 163 | import 'package:flutter/material.dart'; |
| @@ -62,9 +62,29 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | @@ -62,9 +62,29 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | ||
| 62 | 62 | ||
| 63 | ``` | 63 | ``` |
| 64 | 64 | ||
| 65 | -### 使用: | 65 | +### 使用 |
| 66 | 66 | ||
| 67 | -#### 适配尺寸: | 67 | +#### API |
| 68 | + | ||
| 69 | +```dart | ||
| 70 | + ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸 | ||
| 71 | + ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸 | ||
| 72 | + ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体 | ||
| 73 | + ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放) | ||
| 74 | + | ||
| 75 | + ScreenUtil.pixelRatio //设备的像素密度 | ||
| 76 | + ScreenUtil.screenWidth //设备宽度 | ||
| 77 | + ScreenUtil.screenHeight //设备高度 | ||
| 78 | + ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 | ||
| 79 | + ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px | ||
| 80 | + ScreenUtil.textScaleFactor //系统字体缩放比例 | ||
| 81 | + | ||
| 82 | + ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例 | ||
| 83 | + ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例 | ||
| 84 | +``` | ||
| 85 | + | ||
| 86 | + | ||
| 87 | +#### 适配尺寸 | ||
| 68 | 88 | ||
| 69 | 传入设计稿的px尺寸: | 89 | 传入设计稿的px尺寸: |
| 70 | 90 | ||
| @@ -94,15 +114,37 @@ Container( | @@ -94,15 +114,37 @@ Container( | ||
| 94 | ), | 114 | ), |
| 95 | ``` | 115 | ``` |
| 96 | 116 | ||
| 117 | +如果你的dart sdk>=2.6,可以使用扩展函数: | ||
| 118 | +example: | ||
| 119 | +不用这个: | ||
| 120 | +``` | ||
| 121 | +Container( | ||
| 122 | +width: ScreenUtil().setWidth(50), | ||
| 123 | +height:ScreenUtil().setHeight(200), | ||
| 124 | +) | ||
| 125 | +``` | ||
| 126 | +而是用这个: | ||
| 127 | +``` | ||
| 128 | +Container( | ||
| 129 | +width: 50.w, | ||
| 130 | +height:200.h | ||
| 131 | +) | ||
| 132 | +``` | ||
| 133 | + | ||
| 97 | #### 适配字体: | 134 | #### 适配字体: |
| 98 | 传入设计稿的px尺寸: | 135 | 传入设计稿的px尺寸: |
| 99 | 136 | ||
| 100 | ``` | 137 | ``` |
| 101 | //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) | 138 | //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) |
| 102 | -ScreenUtil().setSp(28) | 139 | +ScreenUtil().setSp(28) |
| 140 | +或 | ||
| 141 | +28.sp (dart sdk>=2.6) | ||
| 103 | 142 | ||
| 104 | //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置) | 143 | //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置) |
| 105 | -ScreenUtil().setSp(24, allowFontScalingSelf: true) | 144 | +ScreenUtil().setSp(24, allowFontScalingSelf: true) |
| 145 | +或 | ||
| 146 | +24.ssp (dart sdk>=2.6) | ||
| 147 | + | ||
| 106 | 148 | ||
| 107 | //for example: | 149 | //for example: |
| 108 | 150 | ||
| @@ -123,20 +165,6 @@ Column( | @@ -123,20 +165,6 @@ Column( | ||
| 123 | ) | 165 | ) |
| 124 | ``` | 166 | ``` |
| 125 | 167 | ||
| 126 | -#### 其他相关api: | ||
| 127 | -``` | ||
| 128 | - ScreenUtil.pixelRatio //设备的像素密度 | ||
| 129 | - ScreenUtil.screenWidth //设备宽度 | ||
| 130 | - ScreenUtil.screenHeight //设备高度 | ||
| 131 | - ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 | ||
| 132 | - ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px | ||
| 133 | - ScreenUtil.textScaleFactor //系统字体缩放比例 | ||
| 134 | - | ||
| 135 | - ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例 | ||
| 136 | - ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例 | ||
| 137 | - | ||
| 138 | -``` | ||
| 139 | - | ||
| 140 | ```dart | 168 | ```dart |
| 141 | import 'package:flutter/material.dart'; | 169 | import 'package:flutter/material.dart'; |
| 142 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 170 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| @@ -60,6 +60,25 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | @@ -60,6 +60,25 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); | ||
| 60 | 60 | ||
| 61 | ### Uso: | 61 | ### Uso: |
| 62 | 62 | ||
| 63 | +#### API | ||
| 64 | + | ||
| 65 | +```dart | ||
| 66 | + ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //Adapted to screen width | ||
| 67 | + ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //Adapted to screen height | ||
| 68 | + ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //Adapter font | ||
| 69 | + ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings) | ||
| 70 | + | ||
| 71 | + ScreenUtil.pixelRatio //Device pixel density | ||
| 72 | + ScreenUtil.screenWidth //Device width | ||
| 73 | + ScreenUtil.screenHeight //Device height | ||
| 74 | + ScreenUtil.bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen | ||
| 75 | + ScreenUtil.statusBarHeight //Status bar height , Notch will be higher Unit px | ||
| 76 | + ScreenUtil.textScaleFactor //System font scaling factor | ||
| 77 | + | ||
| 78 | + ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px | ||
| 79 | + ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px | ||
| 80 | +``` | ||
| 81 | + | ||
| 63 | #### Adaptar o tamanho da tela: | 82 | #### Adaptar o tamanho da tela: |
| 64 | 83 | ||
| 65 | Informe o tamanho em pixels do protótipo de design: | 84 | Informe o tamanho em pixels do protótipo de design: |
| @@ -68,6 +87,24 @@ Adaptado à largura da tela: `ScreenUtil().setWidth(540)`, | @@ -68,6 +87,24 @@ Adaptado à largura da tela: `ScreenUtil().setWidth(540)`, | ||
| 68 | 87 | ||
| 69 | Adaptado à altura da tela: `ScreenUtil().setHeight(200)`, | 88 | Adaptado à altura da tela: `ScreenUtil().setHeight(200)`, |
| 70 | 89 | ||
| 90 | +If your dart sdk>=2.6, you can use extension functions: | ||
| 91 | + | ||
| 92 | +example: | ||
| 93 | +instead of : | ||
| 94 | +``` | ||
| 95 | +Container( | ||
| 96 | +width: ScreenUtil().setWidth(50), | ||
| 97 | +height:ScreenUtil().setHeight(200), | ||
| 98 | +) | ||
| 99 | +``` | ||
| 100 | +you can use it like this: | ||
| 101 | +``` | ||
| 102 | +Container( | ||
| 103 | +width: 50.w, | ||
| 104 | +height:200.h | ||
| 105 | +) | ||
| 106 | +``` | ||
| 107 | + | ||
| 71 | **Nota** | 108 | **Nota** |
| 72 | 109 | ||
| 73 | Altura também é adaptada de acordo com o setWidth para garantir que não tenha deformação (quando quiser um quadrado) | 110 | Altura também é adaptada de acordo com o setWidth para garantir que não tenha deformação (quando quiser um quadrado) |
| 1 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
| 2 | -import 'package:flutter/services.dart'; | ||
| 3 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
| 4 | 3 | ||
| 5 | void main() => runApp(MyApp()); | 4 | void main() => runApp(MyApp()); |
| @@ -58,12 +57,12 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -58,12 +57,12 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
| 58 | children: <Widget>[ | 57 | children: <Widget>[ |
| 59 | Container( | 58 | Container( |
| 60 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), | 59 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), |
| 61 | - width: ScreenUtil().setWidth(375), | ||
| 62 | - height: ScreenUtil().setHeight(200), | 60 | + width: 375.w, |
| 61 | + height: 200.h, | ||
| 63 | color: Colors.red, | 62 | color: Colors.red, |
| 64 | child: Text( | 63 | child: Text( |
| 65 | - 'My width:${ScreenUtil().setWidth(375)}dp \n' | ||
| 66 | - 'My height:${ScreenUtil().setHeight(200)}dp', | 64 | + 'My width:${375.w}dp \n' |
| 65 | + 'My height:${200.h}dp', | ||
| 67 | style: TextStyle( | 66 | style: TextStyle( |
| 68 | color: Colors.white, fontSize: ScreenUtil().setSp(24)), | 67 | color: Colors.white, fontSize: ScreenUtil().setSp(24)), |
| 69 | ), | 68 | ), |
| @@ -108,7 +107,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -108,7 +107,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
| 108 | 'My font size is 24px on the design draft and will not change with the system.', | 107 | 'My font size is 24px on the design draft and will not change with the system.', |
| 109 | style: TextStyle( | 108 | style: TextStyle( |
| 110 | color: Colors.black, | 109 | color: Colors.black, |
| 111 | - fontSize: ScreenUtil().setSp(24), | 110 | + fontSize: 24.sp, |
| 112 | )), | 111 | )), |
| 113 | Text( | 112 | Text( |
| 114 | 'My font size is 24px on the design draft and will change with the system.', | 113 | 'My font size is 24px on the design draft and will change with the system.', |
| @@ -74,12 +74,12 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -74,12 +74,12 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
| 74 | ), | 74 | ), |
| 75 | Container( | 75 | Container( |
| 76 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), | 76 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), |
| 77 | - width: ScreenUtil().setWidth(375), | ||
| 78 | - height: ScreenUtil().setHeight(200), | 77 | + width: 375.w, |
| 78 | + height: 200.h, | ||
| 79 | color: Colors.blue, | 79 | color: Colors.blue, |
| 80 | child: Text( | 80 | child: Text( |
| 81 | - '我的宽度:${ScreenUtil().setWidth(375)}dp \n' | ||
| 82 | - '我的高度:${ScreenUtil().setHeight(200)}dp', | 81 | + '我的宽度:${375.w}dp \n' |
| 82 | + '我的高度:${200.h}dp', | ||
| 83 | style: TextStyle( | 83 | style: TextStyle( |
| 84 | color: Colors.white, | 84 | color: Colors.white, |
| 85 | fontSize: ScreenUtil().setSp(24))), | 85 | fontSize: ScreenUtil().setSp(24))), |
| @@ -102,7 +102,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -102,7 +102,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
| 102 | textAlign: TextAlign.center, | 102 | textAlign: TextAlign.center, |
| 103 | ), | 103 | ), |
| 104 | SizedBox( | 104 | SizedBox( |
| 105 | - height: ScreenUtil().setHeight(100), | 105 | + height: 100.h, |
| 106 | ), | 106 | ), |
| 107 | Text('系统的字体缩放比例:${ScreenUtil.textScaleFactor}'), | 107 | Text('系统的字体缩放比例:${ScreenUtil.textScaleFactor}'), |
| 108 | Column( | 108 | Column( |
| @@ -111,7 +111,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -111,7 +111,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
| 111 | Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', | 111 | Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化', |
| 112 | style: TextStyle( | 112 | style: TextStyle( |
| 113 | color: Colors.black, | 113 | color: Colors.black, |
| 114 | - fontSize: ScreenUtil().setSp(24), | 114 | + fontSize: 24.sp, |
| 115 | )), | 115 | )), |
| 116 | Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', | 116 | Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化', |
| 117 | style: TextStyle( | 117 | style: TextStyle( |
| @@ -75,7 +75,7 @@ packages: | @@ -75,7 +75,7 @@ packages: | ||
| 75 | path: ".." | 75 | path: ".." |
| 76 | relative: true | 76 | relative: true |
| 77 | source: path | 77 | source: path |
| 78 | - version: "1.0.2" | 78 | + version: "1.1.0" |
| 79 | flutter_test: | 79 | flutter_test: |
| 80 | dependency: "direct dev" | 80 | dependency: "direct dev" |
| 81 | description: flutter | 81 | description: flutter |
| @@ -3,127 +3,7 @@ | @@ -3,127 +3,7 @@ | ||
| 3 | * email: zhuoyuan93@gmail.com | 3 | * email: zhuoyuan93@gmail.com |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | -import 'package:flutter/material.dart'; | 6 | +library flutter_screenutil; |
| 7 | 7 | ||
| 8 | -class ScreenUtil { | ||
| 9 | - static ScreenUtil _instance; | ||
| 10 | - static const int defaultWidth = 1080; | ||
| 11 | - static const int defaultHeight = 1920; | ||
| 12 | - | ||
| 13 | - /// UI设计中手机尺寸 , px | ||
| 14 | - /// Size of the phone in UI Design , px | ||
| 15 | - num uiWidthPx; | ||
| 16 | - num uiHeightPx; | ||
| 17 | - | ||
| 18 | - /// 控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为false。 | ||
| 19 | - /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. | ||
| 20 | - bool allowFontScaling; | ||
| 21 | - | ||
| 22 | - static MediaQueryData _mediaQueryData; | ||
| 23 | - static double _screenWidth; | ||
| 24 | - static double _screenHeight; | ||
| 25 | - static double _pixelRatio; | ||
| 26 | - static double _statusBarHeight; | ||
| 27 | - static double _bottomBarHeight; | ||
| 28 | - static double _textScaleFactor; | ||
| 29 | - | ||
| 30 | - ScreenUtil._(); | ||
| 31 | - | ||
| 32 | - factory ScreenUtil() { | ||
| 33 | - return _instance; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - static void init(BuildContext context, | ||
| 37 | - {num width = defaultWidth, | ||
| 38 | - num height = defaultHeight, | ||
| 39 | - bool allowFontScaling = false}) { | ||
| 40 | - if (_instance == null) { | ||
| 41 | - _instance = ScreenUtil._(); | ||
| 42 | - } | ||
| 43 | - _instance.uiWidthPx = width; | ||
| 44 | - _instance.uiHeightPx = height; | ||
| 45 | - _instance.allowFontScaling = allowFontScaling; | ||
| 46 | - | ||
| 47 | - MediaQueryData mediaQuery = MediaQuery.of(context); | ||
| 48 | - _mediaQueryData = mediaQuery; | ||
| 49 | - _pixelRatio = mediaQuery.devicePixelRatio; | ||
| 50 | - _screenWidth = mediaQuery.size.width; | ||
| 51 | - _screenHeight = mediaQuery.size.height; | ||
| 52 | - _statusBarHeight = mediaQuery.padding.top; | ||
| 53 | - _bottomBarHeight = _mediaQueryData.padding.bottom; | ||
| 54 | - _textScaleFactor = mediaQuery.textScaleFactor; | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - static MediaQueryData get mediaQueryData => _mediaQueryData; | ||
| 58 | - | ||
| 59 | - /// 每个逻辑像素的字体像素数,字体的缩放比例 | ||
| 60 | - /// The number of font pixels for each logical pixel. | ||
| 61 | - static double get textScaleFactor => _textScaleFactor; | ||
| 62 | - | ||
| 63 | - /// 设备的像素密度 | ||
| 64 | - /// The size of the media in logical pixels (e.g, the size of the screen). | ||
| 65 | - static double get pixelRatio => _pixelRatio; | ||
| 66 | - | ||
| 67 | - /// 当前设备宽度 dp | ||
| 68 | - /// The horizontal extent of this size. | ||
| 69 | - static double get screenWidthDp => _screenWidth; | ||
| 70 | - | ||
| 71 | - ///当前设备高度 dp | ||
| 72 | - ///The vertical extent of this size. dp | ||
| 73 | - static double get screenHeightDp => _screenHeight; | ||
| 74 | - | ||
| 75 | - /// 当前设备宽度 px | ||
| 76 | - /// The vertical extent of this size. px | ||
| 77 | - static double get screenWidth => _screenWidth * _pixelRatio; | ||
| 78 | - | ||
| 79 | - /// 当前设备高度 px | ||
| 80 | - /// The vertical extent of this size. px | ||
| 81 | - static double get screenHeight => _screenHeight * _pixelRatio; | ||
| 82 | - | ||
| 83 | - /// 状态栏高度 dp 刘海屏会更高 | ||
| 84 | - /// The offset from the top | ||
| 85 | - static double get statusBarHeight => _statusBarHeight; | ||
| 86 | - | ||
| 87 | - /// 底部安全区距离 dp | ||
| 88 | - /// The offset from the bottom. | ||
| 89 | - static double get bottomBarHeight => _bottomBarHeight; | ||
| 90 | - | ||
| 91 | - /// 实际的dp与UI设计px的比例 | ||
| 92 | - /// The ratio of the actual dp to the design draft px | ||
| 93 | - double get scaleWidth => _screenWidth / uiWidthPx; | ||
| 94 | - | ||
| 95 | - double get scaleHeight => _screenHeight / uiHeightPx; | ||
| 96 | - | ||
| 97 | - double get scaleText => scaleWidth; | ||
| 98 | - | ||
| 99 | - /// 根据UI设计的设备宽度适配 | ||
| 100 | - /// 高度也可以根据这个来做适配可以保证不变形,比如你先要一个正方形的时候. | ||
| 101 | - /// Adapted to the device width of the UI Design. | ||
| 102 | - /// Height can also be adapted according to this to ensure no deformation , | ||
| 103 | - /// if you want a square | ||
| 104 | - num setWidth(num width) => width * scaleWidth; | ||
| 105 | - | ||
| 106 | - /// 根据UI设计的设备高度适配 | ||
| 107 | - /// 当发现UI设计中的一屏显示的与当前样式效果不符合时, | ||
| 108 | - /// 或者形状有差异时,建议使用此方法实现高度适配. | ||
| 109 | - /// 高度适配主要针对想根据UI设计的一屏展示一样的效果 | ||
| 110 | - /// Highly adaptable to the device according to UI Design | ||
| 111 | - /// It is recommended to use this method to achieve a high degree of adaptation | ||
| 112 | - /// when it is found that one screen in the UI design | ||
| 113 | - /// does not match the current style effect, or if there is a difference in shape. | ||
| 114 | - num setHeight(num height) => height * scaleHeight; | ||
| 115 | - | ||
| 116 | - ///字体大小适配方法 | ||
| 117 | - ///@param [fontSize] UI设计上字体的大小,单位px. | ||
| 118 | - ///Font size adaptation method | ||
| 119 | - ///@param [fontSize] The size of the font on the UI design, in px. | ||
| 120 | - ///@param [allowFontScaling] | ||
| 121 | - num setSp(num fontSize, {bool allowFontScalingSelf}) => | ||
| 122 | - allowFontScalingSelf == null | ||
| 123 | - ? (allowFontScaling | ||
| 124 | - ? (fontSize * scaleText) | ||
| 125 | - : ((fontSize * scaleText) / _textScaleFactor)) | ||
| 126 | - : (allowFontScalingSelf | ||
| 127 | - ? (fontSize * scaleText) | ||
| 128 | - : ((fontSize * scaleText) / _textScaleFactor)); | ||
| 129 | -} | 8 | +export 'size_extension.dart'; |
| 9 | +export 'screenutil.dart'; |
lib/screenutil.dart
0 → 100644
| 1 | +/* | ||
| 2 | + * Created by 李卓原 on 2018/9/29. | ||
| 3 | + * email: zhuoyuan93@gmail.com | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | +import 'package:flutter/material.dart'; | ||
| 7 | + | ||
| 8 | +class ScreenUtil { | ||
| 9 | + static ScreenUtil _instance; | ||
| 10 | + static const int defaultWidth = 1080; | ||
| 11 | + static const int defaultHeight = 1920; | ||
| 12 | + | ||
| 13 | + /// UI设计中手机尺寸 , px | ||
| 14 | + /// Size of the phone in UI Design , px | ||
| 15 | + num uiWidthPx; | ||
| 16 | + num uiHeightPx; | ||
| 17 | + | ||
| 18 | + /// 控制字体是否要根据系统的“字体大小”辅助选项来进行缩放。默认值为false。 | ||
| 19 | + /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. | ||
| 20 | + bool allowFontScaling; | ||
| 21 | + | ||
| 22 | + static MediaQueryData _mediaQueryData; | ||
| 23 | + static double _screenWidth; | ||
| 24 | + static double _screenHeight; | ||
| 25 | + static double _pixelRatio; | ||
| 26 | + static double _statusBarHeight; | ||
| 27 | + static double _bottomBarHeight; | ||
| 28 | + static double _textScaleFactor; | ||
| 29 | + | ||
| 30 | + ScreenUtil._(); | ||
| 31 | + | ||
| 32 | + factory ScreenUtil() { | ||
| 33 | + return _instance; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + static void init(BuildContext context, | ||
| 37 | + {num width = defaultWidth, | ||
| 38 | + num height = defaultHeight, | ||
| 39 | + bool allowFontScaling = false}) { | ||
| 40 | + if (_instance == null) { | ||
| 41 | + _instance = ScreenUtil._(); | ||
| 42 | + } | ||
| 43 | + _instance.uiWidthPx = width; | ||
| 44 | + _instance.uiHeightPx = height; | ||
| 45 | + _instance.allowFontScaling = allowFontScaling; | ||
| 46 | + | ||
| 47 | + MediaQueryData mediaQuery = MediaQuery.of(context); | ||
| 48 | + _mediaQueryData = mediaQuery; | ||
| 49 | + _pixelRatio = mediaQuery.devicePixelRatio; | ||
| 50 | + _screenWidth = mediaQuery.size.width; | ||
| 51 | + _screenHeight = mediaQuery.size.height; | ||
| 52 | + _statusBarHeight = mediaQuery.padding.top; | ||
| 53 | + _bottomBarHeight = _mediaQueryData.padding.bottom; | ||
| 54 | + _textScaleFactor = mediaQuery.textScaleFactor; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + static MediaQueryData get mediaQueryData => _mediaQueryData; | ||
| 58 | + | ||
| 59 | + /// 每个逻辑像素的字体像素数,字体的缩放比例 | ||
| 60 | + /// The number of font pixels for each logical pixel. | ||
| 61 | + static double get textScaleFactor => _textScaleFactor; | ||
| 62 | + | ||
| 63 | + /// 设备的像素密度 | ||
| 64 | + /// The size of the media in logical pixels (e.g, the size of the screen). | ||
| 65 | + static double get pixelRatio => _pixelRatio; | ||
| 66 | + | ||
| 67 | + /// 当前设备宽度 dp | ||
| 68 | + /// The horizontal extent of this size. | ||
| 69 | + static double get screenWidthDp => _screenWidth; | ||
| 70 | + | ||
| 71 | + ///当前设备高度 dp | ||
| 72 | + ///The vertical extent of this size. dp | ||
| 73 | + static double get screenHeightDp => _screenHeight; | ||
| 74 | + | ||
| 75 | + /// 当前设备宽度 px | ||
| 76 | + /// The vertical extent of this size. px | ||
| 77 | + static double get screenWidth => _screenWidth * _pixelRatio; | ||
| 78 | + | ||
| 79 | + /// 当前设备高度 px | ||
| 80 | + /// The vertical extent of this size. px | ||
| 81 | + static double get screenHeight => _screenHeight * _pixelRatio; | ||
| 82 | + | ||
| 83 | + /// 状态栏高度 dp 刘海屏会更高 | ||
| 84 | + /// The offset from the top | ||
| 85 | + static double get statusBarHeight => _statusBarHeight; | ||
| 86 | + | ||
| 87 | + /// 底部安全区距离 dp | ||
| 88 | + /// The offset from the bottom. | ||
| 89 | + static double get bottomBarHeight => _bottomBarHeight; | ||
| 90 | + | ||
| 91 | + /// 实际的dp与UI设计px的比例 | ||
| 92 | + /// The ratio of the actual dp to the design draft px | ||
| 93 | + double get scaleWidth => _screenWidth / uiWidthPx; | ||
| 94 | + | ||
| 95 | + double get scaleHeight => _screenHeight / uiHeightPx; | ||
| 96 | + | ||
| 97 | + double get scaleText => scaleWidth; | ||
| 98 | + | ||
| 99 | + /// 根据UI设计的设备宽度适配 | ||
| 100 | + /// 高度也可以根据这个来做适配可以保证不变形,比如你先要一个正方形的时候. | ||
| 101 | + /// Adapted to the device width of the UI Design. | ||
| 102 | + /// Height can also be adapted according to this to ensure no deformation , | ||
| 103 | + /// if you want a square | ||
| 104 | + num setWidth(num width) => width * scaleWidth; | ||
| 105 | + | ||
| 106 | + /// 根据UI设计的设备高度适配 | ||
| 107 | + /// 当发现UI设计中的一屏显示的与当前样式效果不符合时, | ||
| 108 | + /// 或者形状有差异时,建议使用此方法实现高度适配. | ||
| 109 | + /// 高度适配主要针对想根据UI设计的一屏展示一样的效果 | ||
| 110 | + /// Highly adaptable to the device according to UI Design | ||
| 111 | + /// It is recommended to use this method to achieve a high degree of adaptation | ||
| 112 | + /// when it is found that one screen in the UI design | ||
| 113 | + /// does not match the current style effect, or if there is a difference in shape. | ||
| 114 | + num setHeight(num height) => height * scaleHeight; | ||
| 115 | + | ||
| 116 | + ///字体大小适配方法 | ||
| 117 | + ///@param [fontSize] UI设计上字体的大小,单位px. | ||
| 118 | + ///Font size adaptation method | ||
| 119 | + ///@param [fontSize] The size of the font on the UI design, in px. | ||
| 120 | + ///@param [allowFontScaling] | ||
| 121 | + num setSp(num fontSize, {bool allowFontScalingSelf}) => | ||
| 122 | + allowFontScalingSelf == null | ||
| 123 | + ? (allowFontScaling | ||
| 124 | + ? (fontSize * scaleText) | ||
| 125 | + : ((fontSize * scaleText) / _textScaleFactor)) | ||
| 126 | + : (allowFontScalingSelf | ||
| 127 | + ? (fontSize * scaleText) | ||
| 128 | + : ((fontSize * scaleText) / _textScaleFactor)); | ||
| 129 | +} |
| 1 | name: flutter_screenutil | 1 | name: flutter_screenutil |
| 2 | description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models | 2 | description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models |
| 3 | -version: 1.0.2 | 3 | +version: 1.1.0 |
| 4 | homepage: https://github.com/OpenFlutter/flutter_screenutil | 4 | homepage: https://github.com/OpenFlutter/flutter_screenutil |
| 5 | 5 | ||
| 6 | environment: | 6 | environment: |
-
Please register or login to post a comment