Showing
8 changed files
with
232 additions
and
235 deletions
@@ -6,6 +6,9 @@ | @@ -6,6 +6,9 @@ | ||
6 | * @Description: Update log | 6 | * @Description: Update log |
7 | --> | 7 | --> |
8 | 8 | ||
9 | +# 4.0.0-beta | ||
10 | +- Modified the initialization method | ||
11 | + | ||
9 | # 3.2.0 | 12 | # 3.2.0 |
10 | - Modify the method name to be more semantic: wp->sw , hp->sh | 13 | - Modify the method name to be more semantic: wp->sw , hp->sh |
11 | - Remove the restriction of flutter version | 14 | - Remove the restriction of flutter version |
@@ -23,7 +23,7 @@ dependencies: | @@ -23,7 +23,7 @@ dependencies: | ||
23 | flutter: | 23 | flutter: |
24 | sdk: flutter | 24 | sdk: flutter |
25 | # add flutter_screenutil | 25 | # add flutter_screenutil |
26 | - flutter_screenutil: ^3.1.0 | 26 | + flutter_screenutil: ^4.0.0-beta |
27 | ``` | 27 | ``` |
28 | ### Add the following imports to your Dart code: | 28 | ### Add the following imports to your Dart code: |
29 | ``` | 29 | ``` |
@@ -34,31 +34,41 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -34,31 +34,41 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
34 | 34 | ||
35 | |Property|Type|Default Value|Description| | 35 | |Property|Type|Default Value|Description| |
36 | |:---|:---|:---|:---| | 36 | |:---|:---|:---|:---| |
37 | -|designSize|Size|Size(1080, 1920)|The size of the device in the design draft, in px| | 37 | +|designSize|Size|Size(1080, 1920)|The size of the ui design draft can be in any unit, but it must be consistent during use| |
38 | |allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option| | 38 | |allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option| |
39 | 39 | ||
40 | ### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option | 40 | ### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option |
41 | -Please set the size of the design draft before use, the width and height of the design draft (unit px). | 41 | +Please set the size of the design draft before use, the width and height of the design draft. |
42 | 42 | ||
43 | ``` | 43 | ``` |
44 | +void main() => runApp(MyApp()); | ||
44 | 45 | ||
45 | -void main() { | ||
46 | - WidgetsFlutterBinding.ensureInitialized(); | 46 | +class MyApp extends StatelessWidget { |
47 | + @override | ||
48 | + Widget build(BuildContext context) { | ||
49 | + return LayoutBuilder( | ||
50 | + builder: (context, constraints) { | ||
47 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | 51 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) |
48 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
49 | - runApp(MyApp()); | 52 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); |
53 | + | ||
54 | + return MaterialApp( | ||
55 | + ... | ||
56 | + ); | ||
57 | + }, | ||
58 | + ); | ||
59 | + } | ||
50 | } | 60 | } |
51 | 61 | ||
52 | //fill in the screen size of the device in the design | 62 | //fill in the screen size of the device in the design |
53 | 63 | ||
54 | //default value : width : 1080px , height:1920px , allowFontScaling:false | 64 | //default value : width : 1080px , height:1920px , allowFontScaling:false |
55 | -ScreenUtil.init(context); | 65 | +ScreenUtil.init(constraints); |
56 | 66 | ||
57 | //If the design is based on the size of the iPhone6 (iPhone6 750*1334) | 67 | //If the design is based on the size of the iPhone6 (iPhone6 750*1334) |
58 | -ScreenUtil.init(context, designSize: Size(750, 1334)); | 68 | +ScreenUtil.init(constraints, designSize: Size(750, 1334)); |
59 | 69 | ||
60 | //If you want to set the font size is scaled according to the system's "font size" assist option | 70 | //If you want to set the font size is scaled according to the system's "font size" assist option |
61 | -ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | 71 | +ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: true); |
62 | 72 | ||
63 | ``` | 73 | ``` |
64 | 74 | ||
@@ -69,21 +79,21 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | @@ -69,21 +79,21 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | ||
69 | #### Pass the px size of the design draft | 79 | #### Pass the px size of the design draft |
70 | 80 | ||
71 | ```dart | 81 | ```dart |
72 | - ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //Adapted to screen width | ||
73 | - ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //Adapted to screen height | ||
74 | - ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //Adapter font | ||
75 | - ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings) | ||
76 | - ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //Adapter font(fonts will not scale to respect Text Size accessibility settings) | 82 | + ScreenUtil().setWidth(540) (dart sdk>=2.6 : 540.w) //Adapted to screen width |
83 | + ScreenUtil().setHeight(200) (dart sdk>=2.6 : 200.h) //Adapted to screen height , under normal circumstances, the height still uses x.w | ||
84 | + ScreenUtil().setSp(24) (dart sdk>=2.6 : 24.sp) //Adapter font | ||
85 | + ScreenUtil().setSp(24, allowFontScalingSelf: true) (dart sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings) | ||
86 | + ScreenUtil().setSp(24, allowFontScalingSelf: false) (dart sdk>=2.6 : 24.nsp) //Adapter font(fonts will not scale to respect Text Size accessibility settings) | ||
77 | 87 | ||
78 | ScreenUtil().pixelRatio //Device pixel density | 88 | ScreenUtil().pixelRatio //Device pixel density |
79 | - ScreenUtil().screenWidth (sdk>=2.6 : 1.sw) //Device width | ||
80 | - ScreenUtil().screenHeight (sdk>=2.6 : 1.sh) //Device height | 89 | + ScreenUtil().screenWidth (dart sdk>=2.6 : 1.sw) //Device width |
90 | + ScreenUtil().screenHeight (dart sdk>=2.6 : 1.sh) //Device height | ||
81 | ScreenUtil().bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen | 91 | ScreenUtil().bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen |
82 | ScreenUtil().statusBarHeight //Status bar height , Notch will be higher Unit px | 92 | ScreenUtil().statusBarHeight //Status bar height , Notch will be higher Unit px |
83 | ScreenUtil().textScaleFactor //System font scaling factor | 93 | ScreenUtil().textScaleFactor //System font scaling factor |
84 | 94 | ||
85 | - ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px | ||
86 | - ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px | 95 | + ScreenUtil().scaleWidth //Ratio of actual width dp to ui design |
96 | + ScreenUtil().scaleHeight //Ratio of actual height dp to ui design | ||
87 | 97 | ||
88 | 0.2.sw //0.2 times the screen width | 98 | 0.2.sw //0.2 times the screen width |
89 | 0.5.sh //50% of screen height | 99 | 0.5.sh //50% of screen height |
@@ -91,11 +101,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | @@ -91,11 +101,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | ||
91 | 101 | ||
92 | #### Adapt screen size: | 102 | #### Adapt screen size: |
93 | 103 | ||
94 | -Pass the px size of the design draft: | 104 | +Pass the px size of the design draft((The unit is the same as the unit at initialization)): |
95 | 105 | ||
96 | Adapted to screen width: `ScreenUtil().setWidth(540)`, | 106 | Adapted to screen width: `ScreenUtil().setWidth(540)`, |
97 | 107 | ||
98 | -Adapted to screen height: `ScreenUtil().setHeight(200)`, | 108 | +Adapted to screen height: `ScreenUtil().setHeight(200)`, In general, the height is best to adapt to the width |
99 | 109 | ||
100 | If your dart sdk>=2.6, you can use extension functions: | 110 | If your dart sdk>=2.6, you can use extension functions: |
101 | 111 | ||
@@ -123,11 +133,12 @@ setHeight method is mainly adapted in height, you want to control the height and | @@ -123,11 +133,12 @@ setHeight method is mainly adapted in height, you want to control the height and | ||
123 | 133 | ||
124 | ```dart | 134 | ```dart |
125 | //for example: | 135 | //for example: |
126 | -//rectangle | 136 | + |
137 | +///If you want to display a square: | ||
138 | +///rectangle | ||
127 | Container( | 139 | Container( |
128 | width: ScreenUtil().setWidth(375), | 140 | width: ScreenUtil().setWidth(375), |
129 | - height: ScreenUtil().setHeight(200), | ||
130 | - ... | 141 | + height: ScreenUtil().setWidth(200), |
131 | ), | 142 | ), |
132 | 143 | ||
133 | ////If you want to display a square: | 144 | ////If you want to display a square: |
@@ -138,9 +149,10 @@ Container( | @@ -138,9 +149,10 @@ Container( | ||
138 | 149 | ||
139 | ``` | 150 | ``` |
140 | 151 | ||
152 | + | ||
141 | #### Adapter font: | 153 | #### Adapter font: |
142 | ``` dart | 154 | ``` dart |
143 | -//Incoming font size,the unit is pixel, fonts will not scale to respect Text Size accessibility settings | 155 | +//Incoming font size(The unit is the same as the unit at initialization), fonts will not scale to respect Text Size accessibility settings |
144 | //(AllowallowFontScaling when initializing ScreenUtil) | 156 | //(AllowallowFontScaling when initializing ScreenUtil) |
145 | ScreenUtil().setSp(28) | 157 | ScreenUtil().setSp(28) |
146 | 28.sp | 158 | 28.sp |
@@ -28,7 +28,7 @@ dependencies: | @@ -28,7 +28,7 @@ dependencies: | ||
28 | flutter: | 28 | flutter: |
29 | sdk: flutter | 29 | sdk: flutter |
30 | # 添加依赖 | 30 | # 添加依赖 |
31 | - flutter_screenutil: ^3.1.0 | 31 | + flutter_screenutil: ^4.0.0-beta |
32 | ``` | 32 | ``` |
33 | ### 在每个使用的地方导入包: | 33 | ### 在每个使用的地方导入包: |
34 | ``` | 34 | ``` |
@@ -39,31 +39,41 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -39,31 +39,41 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
39 | 39 | ||
40 | |属性|类型|默认值|描述| | 40 | |属性|类型|默认值|描述| |
41 | |:---|:---|:---|:---| | 41 | |:---|:---|:---|:---| |
42 | -|width|double|1080px|设计稿中设备的宽度,单位px| | ||
43 | -|height|double|1920px|设计稿中设备的高度,单位px| | 42 | +|designSize|Size|Size(1080, 1920)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)| |
44 | |allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放| | 43 | |allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放| |
45 | 44 | ||
46 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 | 45 | ### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放 |
47 | -在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px) | ||
48 | -一定在MaterialApp的home中的页面设置(即入口文件,只需设置一次),以保证在每次使用之前设置好了适配尺寸: | 46 | +在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致) |
47 | +一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸: | ||
49 | 48 | ||
50 | ``` | 49 | ``` |
51 | //填入设计稿中设备的屏幕尺寸 | 50 | //填入设计稿中设备的屏幕尺寸 |
52 | -void main() { | ||
53 | - WidgetsFlutterBinding.ensureInitialized(); | 51 | + |
52 | +void main() => runApp(MyApp()); | ||
53 | + | ||
54 | +class MyApp extends StatelessWidget { | ||
55 | + @override | ||
56 | + Widget build(BuildContext context) { | ||
57 | + return LayoutBuilder( | ||
58 | + builder: (context, constraints) { | ||
54 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 59 | //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
55 | - ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false); | ||
56 | - runApp(MyApp()); | ||
57 | -} | 60 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); |
58 | 61 | ||
62 | + return MaterialApp( | ||
63 | + ... | ||
64 | + ); | ||
65 | + }, | ||
66 | + ); | ||
67 | + } | ||
68 | +} | ||
59 | //默认 width : 1080px , height:1920px , allowFontScaling:false | 69 | //默认 width : 1080px , height:1920px , allowFontScaling:false |
60 | -ScreenUtil.init(context); | 70 | +ScreenUtil.init(constraints); |
61 | 71 | ||
62 | //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | 72 | //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) |
63 | -ScreenUtil.init(context, designSize: Size(750, 1334)); | 73 | +ScreenUtil.init(constraints, designSize: Size(750, 1334)); |
64 | 74 | ||
65 | //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false | 75 | //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false |
66 | -ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | 76 | +ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: true); |
67 | 77 | ||
68 | ``` | 78 | ``` |
69 | 79 | ||
@@ -73,7 +83,7 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | @@ -73,7 +83,7 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | ||
73 | #### 传入设计稿的px尺寸 px px px ! | 83 | #### 传入设计稿的px尺寸 px px px ! |
74 | ```dart | 84 | ```dart |
75 | ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸 | 85 | ScreenUtil().setWidth(540) (sdk>=2.6 : 540.w) //根据屏幕宽度适配尺寸 |
76 | - ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸 | 86 | + ScreenUtil().setHeight(200) (sdk>=2.6 : 200.h) //根据屏幕高度适配尺寸(一般根据宽度适配即可) |
77 | ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体 | 87 | ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体 |
78 | ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放) | 88 | ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放) |
79 | ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放) | 89 | ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放) |
@@ -82,11 +92,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | @@ -82,11 +92,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | ||
82 | ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度 | 92 | ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度 |
83 | ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度 | 93 | ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度 |
84 | ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 | 94 | ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的 |
85 | - ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px | 95 | + ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 |
86 | ScreenUtil.textScaleFactor //系统字体缩放比例 | 96 | ScreenUtil.textScaleFactor //系统字体缩放比例 |
87 | 97 | ||
88 | - ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例 | ||
89 | - ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例 | 98 | + ScreenUtil().scaleWidth // 实际宽度的dp与设计稿宽度的比例 |
99 | + ScreenUtil().scaleHeight // 实际高度的dp与设计稿高度度的比例 | ||
90 | 100 | ||
91 | 0.2.sw //屏幕宽度的0.2倍 | 101 | 0.2.sw //屏幕宽度的0.2倍 |
92 | 0.5.sh //屏幕高度的50% | 102 | 0.5.sh //屏幕高度的50% |
@@ -95,11 +105,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | @@ -95,11 +105,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | ||
95 | 105 | ||
96 | #### 适配尺寸 | 106 | #### 适配尺寸 |
97 | 107 | ||
98 | -传入设计稿的px尺寸: | 108 | +传入设计稿的尺寸(单位与初始化时的单位相同): |
99 | 109 | ||
100 | 根据屏幕宽度适配 `width: ScreenUtil().setWidth(540)`, | 110 | 根据屏幕宽度适配 `width: ScreenUtil().setWidth(540)`, |
101 | 111 | ||
102 | -根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, | 112 | +根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, 一般来说,控件高度也根据宽度进行适配 |
103 | 113 | ||
104 | **注意** | 114 | **注意** |
105 | 115 | ||
@@ -113,7 +123,7 @@ setHeight譁ケ豕穂クサ隕∵弍蝨ィ鬮伜コヲ荳願ソ幄。碁る, 蝨ィ菴諠ウ謗ァ蛻カUI荳贋ク螻冗噪 | @@ -113,7 +123,7 @@ setHeight譁ケ豕穂クサ隕∵弍蝨ィ鬮伜コヲ荳願ソ幄。碁る, 蝨ィ菴諠ウ謗ァ蛻カUI荳贋ク螻冗噪 | ||
113 | //UI上是长方形: | 123 | //UI上是长方形: |
114 | Container( | 124 | Container( |
115 | width: ScreenUtil().setWidth(375), | 125 | width: ScreenUtil().setWidth(375), |
116 | - height: ScreenUtil().setHeight(200), | 126 | + height: ScreenUtil().setHeight(375), |
117 | ), | 127 | ), |
118 | 128 | ||
119 | //如果你想显示一个正方形: | 129 | //如果你想显示一个正方形: |
@@ -141,10 +151,10 @@ height:200.h | @@ -141,10 +151,10 @@ height:200.h | ||
141 | ``` | 151 | ``` |
142 | 152 | ||
143 | #### 适配字体: | 153 | #### 适配字体: |
144 | -传入设计稿的px尺寸: | 154 | +传入设计稿的字体大小: |
145 | 155 | ||
146 | ``` | 156 | ``` |
147 | -//传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) | 157 | +//传入字体大小(单位和初始化时的单位保持一致),默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling) |
148 | ScreenUtil().setSp(28) | 158 | ScreenUtil().setSp(28) |
149 | 或 | 159 | 或 |
150 | 28.sp (dart sdk>=2.6) | 160 | 28.sp (dart sdk>=2.6) |
@@ -183,36 +193,34 @@ void main() => runApp(MyApp()); | @@ -183,36 +193,34 @@ void main() => runApp(MyApp()); | ||
183 | class MyApp extends StatelessWidget { | 193 | class MyApp extends StatelessWidget { |
184 | @override | 194 | @override |
185 | Widget build(BuildContext context) { | 195 | Widget build(BuildContext context) { |
196 | + return LayoutBuilder( | ||
197 | + builder: (context, constraints) { | ||
198 | + //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
199 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
200 | + | ||
186 | return MaterialApp( | 201 | return MaterialApp( |
187 | debugShowCheckedModeBanner: false, | 202 | debugShowCheckedModeBanner: false, |
188 | title: 'Flutter_ScreenUtil', | 203 | title: 'Flutter_ScreenUtil', |
189 | theme: ThemeData( | 204 | theme: ThemeData( |
190 | primarySwatch: Colors.blue, | 205 | primarySwatch: Colors.blue, |
191 | ), | 206 | ), |
192 | - home: MyHomePage(), | 207 | + home: HomePage(title: 'FlutterScreenUtil Demo'), |
208 | + ); | ||
209 | + }, | ||
193 | ); | 210 | ); |
194 | } | 211 | } |
195 | } | 212 | } |
196 | 213 | ||
197 | -class MyHomePage extends StatelessWidget { | ||
198 | - @override | ||
199 | - Widget build(BuildContext context) { | ||
200 | - //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | ||
201 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
202 | - return ExampleWidget(title: 'FlutterScreenUtil 示例'); | ||
203 | - } | ||
204 | -} | ||
205 | - | ||
206 | -class ExampleWidget extends StatefulWidget { | ||
207 | - const ExampleWidget({Key key, this.title}) : super(key: key); | 214 | +class HomePage extends StatefulWidget { |
215 | + const HomePage({Key key, this.title}) : super(key: key); | ||
208 | 216 | ||
209 | final String title; | 217 | final String title; |
210 | 218 | ||
211 | @override | 219 | @override |
212 | - _ExampleWidgetState createState() => _ExampleWidgetState(); | 220 | + _HomePageState createState() => _HomePageState(); |
213 | } | 221 | } |
214 | 222 | ||
215 | -class _ExampleWidgetState extends State<ExampleWidget> { | 223 | +class _HomePageState extends State<HomePage> { |
216 | @override | 224 | @override |
217 | Widget build(BuildContext context) { | 225 | Widget build(BuildContext context) { |
218 | printScreenInformation(); | 226 | printScreenInformation(); |
@@ -290,42 +298,20 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -290,42 +298,20 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
290 | ], | 298 | ], |
291 | ), | 299 | ), |
292 | ), | 300 | ), |
293 | - floatingActionButton: FloatingActionButton( | ||
294 | - child: Icon(Icons.title), | ||
295 | - onPressed: () { | ||
296 | - ScreenUtil.init( | ||
297 | - context, | ||
298 | - designSize: Size(750, 1334), | ||
299 | - allowFontScaling: false, | ||
300 | - ); | ||
301 | - setState(() {}); | ||
302 | - }, | ||
303 | - ), | ||
304 | ); | 301 | ); |
305 | } | 302 | } |
306 | 303 | ||
307 | void printScreenInformation() { | 304 | void printScreenInformation() { |
308 | - print('设备宽度:${ScreenUtil().screenWidth}'); //Device width | ||
309 | - print('设备高度:${ScreenUtil().screenHeight}'); //Device height | ||
310 | - print('设备的像素密度:${ScreenUtil().pixelRatio}'); //Device pixel density | ||
311 | - print( | ||
312 | - '底部安全区距离:${ScreenUtil().bottomBarHeight}dp', | ||
313 | - ); //Bottom safe zone distance,suitable for buttons with full screen | ||
314 | - print( | ||
315 | - '状态栏高度:${ScreenUtil().statusBarHeight}dp', | ||
316 | - ); //Status bar height , Notch will be higher Unit px | ||
317 | - | 305 | + print('设备宽度:${1.sw}'); |
306 | + print('设备高度:${1.sh}'); | ||
307 | + print('设备的像素密度:${ScreenUtil().pixelRatio}'); | ||
308 | + print('底部安全区距离:${ScreenUtil().bottomBarHeight}dp'); | ||
309 | + print('状态栏高度:${ScreenUtil().statusBarHeight}dp'); | ||
318 | print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}'); | 310 | print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}'); |
319 | print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}'); | 311 | print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}'); |
320 | - | ||
321 | - print( | ||
322 | - '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}', | ||
323 | - ); | ||
324 | - print( | ||
325 | - '高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}', | ||
326 | - ); | 312 | + print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}'); |
313 | + print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}'); | ||
327 | print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}'); | 314 | print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}'); |
328 | - | ||
329 | print('屏幕宽度的0.5:${0.5.sw}'); | 315 | print('屏幕宽度的0.5:${0.5.sw}'); |
330 | print('屏幕高度的0.5:${0.5.sh}'); | 316 | print('屏幕高度的0.5:${0.5.sh}'); |
331 | } | 317 | } |
1 | +# I don’t speak Portuguese, and other developers have translated the first version. I later modified it based on the translation software. It is recommended to read the English/Chinese version. | ||
1 | # flutter_screenutil | 2 | # flutter_screenutil |
2 | [](https://pub.dartlang.org/packages/flutter_screenutil) | 3 | [](https://pub.dartlang.org/packages/flutter_screenutil) |
3 | 4 | ||
@@ -23,7 +24,7 @@ dependencies: | @@ -23,7 +24,7 @@ dependencies: | ||
23 | flutter: | 24 | flutter: |
24 | sdk: flutter | 25 | sdk: flutter |
25 | # add flutter_screenutil | 26 | # add flutter_screenutil |
26 | - flutter_screenutil: ^3.1.0 | 27 | + flutter_screenutil: ^4.0.0-beta |
27 | ``` | 28 | ``` |
28 | 29 | ||
29 | ### Adicione o seguinte import em seu código Dart: | 30 | ### Adicione o seguinte import em seu código Dart: |
@@ -48,18 +49,18 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej | @@ -48,18 +49,18 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej | ||
48 | void main() { | 49 | void main() { |
49 | WidgetsFlutterBinding.ensureInitialized(); | 50 | WidgetsFlutterBinding.ensureInitialized(); |
50 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | 51 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) |
51 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | 52 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); |
52 | runApp(MyApp()); | 53 | runApp(MyApp()); |
53 | } | 54 | } |
54 | 55 | ||
55 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false | 56 | //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false |
56 | -ScreenUtil.init(context); | 57 | +ScreenUtil.init(constraints); |
57 | 58 | ||
58 | //Se o design é baseado no iPhone6 (iPhone6 750*1334) | 59 | //Se o design é baseado no iPhone6 (iPhone6 750*1334) |
59 | -ScreenUtil.init(context, designSize: Size(750, 1334)); | 60 | +ScreenUtil.init(constraints, designSize: Size(750, 1334)); |
60 | 61 | ||
61 | //Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema | 62 | //Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema |
62 | -ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true); | 63 | +ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: true); |
63 | 64 | ||
64 | ``` | 65 | ``` |
65 | 66 | ||
@@ -183,33 +184,48 @@ Column( | @@ -183,33 +184,48 @@ Column( | ||
183 | ``` | 184 | ``` |
184 | 185 | ||
185 | ```dart | 186 | ```dart |
186 | -//import | 187 | +... |
188 | +import 'package:flutter/material.dart'; | ||
187 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 189 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
188 | 190 | ||
189 | -... | 191 | +void main() => runApp(MyApp()); |
190 | 192 | ||
193 | +class MyApp extends StatelessWidget { | ||
191 | @override | 194 | @override |
192 | Widget build(BuildContext context) { | 195 | Widget build(BuildContext context) { |
193 | - ///Define o tamanho de ajuste (preenche o tamanho da tela do dispositivo no design). Se o design é baseado no tamanho do iPhone6 (iPhone6 750*1334) | ||
194 | - ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false); | 196 | + return LayoutBuilder( |
197 | + builder: (context, constraints) { | ||
198 | + //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
199 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
195 | 200 | ||
196 | - print('Largura do dispositivo:${ScreenUtil().screenWidth}'); //Largura do dispositivo | ||
197 | - print('Altura do dispositivo:${ScreenUtil().screenHeight}'); //Altura do dispositivo | ||
198 | - print( | ||
199 | - 'Densidade de pixels do dispositivo:${ScreenUtil().pixelRatio}'); //Densidade de pixels do dispositivo | ||
200 | - print( | ||
201 | - 'Distância segura do rodapé:${ScreenUtil().bottomBarHeight}'); //Distância segura do rodapé, adequada para botões em tela cheia | ||
202 | - print( | ||
203 | - 'Altura da status bar:${ScreenUtil().statusBarHeight}px'); //Altura da status bar em pixels, Notch será maior | ||
204 | - print( | ||
205 | - 'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}'); | ||
206 | - print( | ||
207 | - 'Razão entre a altura atual e a altura do protótipo de design em pixels:${ScreenUtil().scaleHeight}'); | ||
208 | - print( | ||
209 | - 'Razão da fonte e largura para o tamanho do design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}'); | ||
210 | - print( | ||
211 | - 'Razão da fonte e altura para o tamanho do design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}'); | 201 | + return MaterialApp( |
202 | + debugShowCheckedModeBanner: false, | ||
203 | + title: 'Flutter_ScreenUtil', | ||
204 | + theme: ThemeData( | ||
205 | + primarySwatch: Colors.blue, | ||
206 | + ), | ||
207 | + home: HomePage(title: 'FlutterScreenUtil Demo'), | ||
208 | + ); | ||
209 | + }, | ||
210 | + ); | ||
211 | + } | ||
212 | +} | ||
213 | + | ||
214 | +class HomePage extends StatefulWidget { | ||
215 | + const HomePage({Key key, this.title}) : super(key: key); | ||
216 | + | ||
217 | + final String title; | ||
218 | + | ||
219 | + @override | ||
220 | + _HomePageState createState() => _HomePageState(); | ||
221 | +} | ||
212 | 222 | ||
223 | +class _HomePageState extends State<HomePage> { | ||
224 | + @override | ||
225 | + Widget build(BuildContext context) { | ||
226 | + //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
227 | + | ||
228 | + printScreenInformation(); | ||
213 | return Scaffold( | 229 | return Scaffold( |
214 | appBar: AppBar( | 230 | appBar: AppBar( |
215 | title: Text(widget.title), | 231 | title: Text(widget.title), |
@@ -220,70 +236,74 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -220,70 +236,74 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
220 | children: <Widget>[ | 236 | children: <Widget>[ |
221 | Row( | 237 | Row( |
222 | children: <Widget>[ | 238 | children: <Widget>[ |
239 | + // Using Extensions | ||
223 | Container( | 240 | Container( |
224 | - padding: EdgeInsets.all(ScreenUtil().setWidth(10)), | ||
225 | - width: ScreenUtil().setWidth(375), | ||
226 | - height: ScreenUtil().setHeight(200), | 241 | + padding: EdgeInsets.all(10.w), |
242 | + width: 0.5.sw, | ||
243 | + height: 200.h, | ||
227 | color: Colors.red, | 244 | color: Colors.red, |
228 | child: Text( | 245 | child: Text( |
229 | - 'Minha largura:${ScreenUtil().setWidth(375)}dp', | 246 | + 'My width:${0.5.sw}dp \n' |
247 | + 'My height:${200.h}dp', | ||
230 | style: TextStyle( | 248 | style: TextStyle( |
231 | color: Colors.white, | 249 | color: Colors.white, |
232 | - fontSize: ScreenUtil().setSp(12)), | 250 | + fontSize: 24.sp, |
251 | + ), | ||
233 | ), | 252 | ), |
234 | ), | 253 | ), |
254 | + // Without using Extensions | ||
235 | Container( | 255 | Container( |
236 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), | 256 | padding: EdgeInsets.all(ScreenUtil().setWidth(10)), |
237 | - width: ScreenUtil().setWidth(375), | 257 | + width: ScreenUtil().screenWidth * 0.5, |
238 | height: ScreenUtil().setHeight(200), | 258 | height: ScreenUtil().setHeight(200), |
239 | color: Colors.blue, | 259 | color: Colors.blue, |
240 | - child: Text('Minha largura:${ScreenUtil().setWidth(375)}dp', | 260 | + child: Text( |
261 | + 'My width:${ScreenUtil().screenWidth * 0.5}dp \n' | ||
262 | + 'My height:${ScreenUtil().setHeight(200)}dp', | ||
241 | style: TextStyle( | 263 | style: TextStyle( |
242 | color: Colors.white, | 264 | color: Colors.white, |
243 | - fontSize: ScreenUtil().setSp(12))), | 265 | + fontSize: ScreenUtil().setSp(24), |
244 | ), | 266 | ), |
245 | - ], | ||
246 | ), | 267 | ), |
247 | - Text('Largura do dispositivo:${ScreenUtil().screenWidth}dp'), | ||
248 | - Text('Altura do dispositivo:${ScreenUtil().screenHeight}dp'), | ||
249 | - Text('Densidade de pixels do dispositivo:${ScreenUtil().pixelRatio}'), | ||
250 | - Text('Distância segura do rodapé:${ScreenUtil().bottomBarHeight}dp'), | ||
251 | - Text('Altura da status bar:${ScreenUtil().statusBarHeight}dp'), | ||
252 | - Text( | ||
253 | - 'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}', | ||
254 | - textAlign: TextAlign.center, | ||
255 | ), | 268 | ), |
256 | - Text( | ||
257 | - 'Razão entre a altura atual e a altura do protótipo de design em pixels:${ScreenUtil().scaleHeight}', | ||
258 | - textAlign: TextAlign.center, | 269 | + ], |
259 | ), | 270 | ), |
271 | + Text('Device width:${ScreenUtil().screenWidthPx}px'), | ||
272 | + Text('Device height:${ScreenUtil().screenHeightPx}px'), | ||
273 | + Text('Device width:${ScreenUtil().screenWidth}dp'), | ||
274 | + Text('Device height:${ScreenUtil().screenHeight}dp'), | ||
275 | + Text('Device pixel density:${ScreenUtil().pixelRatio}'), | ||
276 | + Text('Bottom safe zone distance:${ScreenUtil().bottomBarHeight}dp'), | ||
277 | + Text('Status bar height:${ScreenUtil().statusBarHeight}dp'), | ||
260 | Text( | 278 | Text( |
261 | - 'Razão da fonte e largura para o tamanho do design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}', | 279 | + 'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}', |
262 | textAlign: TextAlign.center, | 280 | textAlign: TextAlign.center, |
263 | ), | 281 | ), |
264 | Text( | 282 | Text( |
265 | - 'Razão da fonte e altura para o tamanho do design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}', | 283 | + 'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}', |
266 | textAlign: TextAlign.center, | 284 | textAlign: TextAlign.center, |
267 | ), | 285 | ), |
268 | SizedBox( | 286 | SizedBox( |
269 | - height: ScreenUtil().setHeight(100), | 287 | + height: 5.h, |
270 | ), | 288 | ), |
271 | - Text('Fator de escala da fonte do sistema:${ScreenUtil.textScaleFactor}'), | 289 | + Text('System font scaling factor:${ScreenUtil().textScaleFactor}'), |
272 | Column( | 290 | Column( |
273 | crossAxisAlignment: CrossAxisAlignment.start, | 291 | crossAxisAlignment: CrossAxisAlignment.start, |
274 | children: <Widget>[ | 292 | children: <Widget>[ |
275 | Text( | 293 | Text( |
276 | - 'Minha fonte tem 24px no protótipo de design e não irá mudar com o sistema.', | 294 | + 'My font size is 24px on the design draft and will not change with the system.', |
277 | style: TextStyle( | 295 | style: TextStyle( |
278 | color: Colors.black, | 296 | color: Colors.black, |
279 | - fontSize: ScreenUtil().setSp(24), | ||
280 | - )), | 297 | + fontSize: 24.sp, |
298 | + ), | ||
299 | + ), | ||
281 | Text( | 300 | Text( |
282 | - 'Minha fonte tem 24px no protótipo de design e poderá mudar com o sistema.', | 301 | + 'My font size is 24px on the design draft and will change with the system.', |
283 | style: TextStyle( | 302 | style: TextStyle( |
284 | color: Colors.black, | 303 | color: Colors.black, |
285 | - fontSize: ScreenUtil().setSp(24, allowFontScalingSelf: true), | ||
286 | - )), | 304 | + fontSize: 24.ssp, |
305 | + ), | ||
306 | + ), | ||
287 | ], | 307 | ], |
288 | ) | 308 | ) |
289 | ], | 309 | ], |
@@ -291,6 +311,23 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | @@ -291,6 +311,23 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; | ||
291 | ), | 311 | ), |
292 | ); | 312 | ); |
293 | } | 313 | } |
314 | + | ||
315 | + void printScreenInformation() { | ||
316 | + print('Device width dp:${1.sw}'); //Device width | ||
317 | + print('Device height dp:${1.sh}'); //Device height | ||
318 | + print('Device pixel density:${ScreenUtil().pixelRatio}'); //Device pixel density | ||
319 | + print( | ||
320 | + 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen | ||
321 | + print( | ||
322 | + 'Status bar height px:${ScreenUtil().statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px | ||
323 | + print('Ratio of actual width dp to UI Design:${ScreenUtil().scaleWidth}'); | ||
324 | + print('Ratio of actual height dp to UI Design:${ScreenUtil().scaleHeight}'); | ||
325 | + print('System font scaling:${ScreenUtil().textScaleFactor}'); | ||
326 | + print('0.5 times the screen width:${0.5.sw}'); | ||
327 | + print('0.5 times the screen height:${0.5.sh}'); | ||
328 | + } | ||
329 | +} | ||
330 | + | ||
294 | ``` | 331 | ``` |
295 | [widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115) | 332 | [widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115) |
296 | 333 |
1 | -import 'dart:ui'; | ||
2 | - | ||
3 | import 'package:flutter/material.dart'; | 1 | import 'package:flutter/material.dart'; |
4 | import 'package:flutter_screenutil/flutter_screenutil.dart'; | 2 | import 'package:flutter_screenutil/flutter_screenutil.dart'; |
5 | 3 | ||
@@ -8,40 +6,37 @@ void main() => runApp(MyApp()); | @@ -8,40 +6,37 @@ void main() => runApp(MyApp()); | ||
8 | class MyApp extends StatelessWidget { | 6 | class MyApp extends StatelessWidget { |
9 | @override | 7 | @override |
10 | Widget build(BuildContext context) { | 8 | Widget build(BuildContext context) { |
9 | + return LayoutBuilder( | ||
10 | + builder: (context, constraints) { | ||
11 | + //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
12 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
13 | + | ||
11 | return MaterialApp( | 14 | return MaterialApp( |
12 | debugShowCheckedModeBanner: false, | 15 | debugShowCheckedModeBanner: false, |
13 | title: 'Flutter_ScreenUtil', | 16 | title: 'Flutter_ScreenUtil', |
14 | theme: ThemeData( | 17 | theme: ThemeData( |
15 | primarySwatch: Colors.blue, | 18 | primarySwatch: Colors.blue, |
16 | ), | 19 | ), |
17 | - home: MyHomePage(), | 20 | + home: HomePage(title: 'FlutterScreenUtil Demo'), |
21 | + ); | ||
22 | + }, | ||
18 | ); | 23 | ); |
19 | } | 24 | } |
20 | } | 25 | } |
21 | 26 | ||
22 | -class MyHomePage extends StatelessWidget { | ||
23 | - @override | ||
24 | - Widget build(BuildContext context) { | ||
25 | - //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
26 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
27 | - return ExampleWidget(title: 'FlutterScreenUtil Demo'); | ||
28 | - } | ||
29 | -} | ||
30 | - | ||
31 | -class ExampleWidget extends StatefulWidget { | ||
32 | - const ExampleWidget({Key key, this.title}) : super(key: key); | 27 | +class HomePage extends StatefulWidget { |
28 | + const HomePage({Key key, this.title}) : super(key: key); | ||
33 | 29 | ||
34 | final String title; | 30 | final String title; |
35 | 31 | ||
36 | @override | 32 | @override |
37 | - _ExampleWidgetState createState() => _ExampleWidgetState(); | 33 | + _HomePageState createState() => _HomePageState(); |
38 | } | 34 | } |
39 | 35 | ||
40 | -class _ExampleWidgetState extends State<ExampleWidget> { | 36 | +class _HomePageState extends State<HomePage> { |
41 | @override | 37 | @override |
42 | Widget build(BuildContext context) { | 38 | Widget build(BuildContext context) { |
43 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | 39 | //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) |
44 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
45 | 40 | ||
46 | printScreenInformation(); | 41 | printScreenInformation(); |
47 | return Scaffold( | 42 | return Scaffold( |
@@ -127,30 +122,19 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -127,30 +122,19 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
127 | ], | 122 | ], |
128 | ), | 123 | ), |
129 | ), | 124 | ), |
130 | - floatingActionButton: FloatingActionButton( | ||
131 | - child: Icon(Icons.title), | ||
132 | - onPressed: () { | ||
133 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
134 | - setState(() {}); | ||
135 | - }, | ||
136 | - ), | ||
137 | ); | 125 | ); |
138 | } | 126 | } |
139 | 127 | ||
140 | void printScreenInformation() { | 128 | void printScreenInformation() { |
141 | - print('Device width dp:${ScreenUtil().screenWidth}'); //Device width | ||
142 | - print('Device height dp:${ScreenUtil().screenHeight}'); //Device height | 129 | + print('Device width dp:${1.sw}'); //Device width |
130 | + print('Device height dp:${1.sh}'); //Device height | ||
143 | print('Device pixel density:${ScreenUtil().pixelRatio}'); //Device pixel density | 131 | print('Device pixel density:${ScreenUtil().pixelRatio}'); //Device pixel density |
144 | print( | 132 | print( |
145 | 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen | 133 | 'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen |
146 | print( | 134 | print( |
147 | 'Status bar height px:${ScreenUtil().statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px | 135 | 'Status bar height px:${ScreenUtil().statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px |
148 | - print('Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); | ||
149 | - print('Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}'); | ||
150 | - print( | ||
151 | - 'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}'); | ||
152 | - print( | ||
153 | - 'The ratio of height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}'); | 136 | + print('Ratio of actual width dp to UI Design:${ScreenUtil().scaleWidth}'); |
137 | + print('Ratio of actual height dp to UI Design:${ScreenUtil().scaleHeight}'); | ||
154 | print('System font scaling:${ScreenUtil().textScaleFactor}'); | 138 | print('System font scaling:${ScreenUtil().textScaleFactor}'); |
155 | print('0.5 times the screen width:${0.5.sw}'); | 139 | print('0.5 times the screen width:${0.5.sw}'); |
156 | print('0.5 times the screen height:${0.5.sh}'); | 140 | print('0.5 times the screen height:${0.5.sh}'); |
@@ -6,36 +6,34 @@ void main() => runApp(MyApp()); | @@ -6,36 +6,34 @@ void main() => runApp(MyApp()); | ||
6 | class MyApp extends StatelessWidget { | 6 | class MyApp extends StatelessWidget { |
7 | @override | 7 | @override |
8 | Widget build(BuildContext context) { | 8 | Widget build(BuildContext context) { |
9 | + return LayoutBuilder( | ||
10 | + builder: (context, constraints) { | ||
11 | + //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 (iPhone6 750*1334) | ||
12 | + ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false); | ||
13 | + | ||
9 | return MaterialApp( | 14 | return MaterialApp( |
10 | debugShowCheckedModeBanner: false, | 15 | debugShowCheckedModeBanner: false, |
11 | title: 'Flutter_ScreenUtil', | 16 | title: 'Flutter_ScreenUtil', |
12 | theme: ThemeData( | 17 | theme: ThemeData( |
13 | primarySwatch: Colors.blue, | 18 | primarySwatch: Colors.blue, |
14 | ), | 19 | ), |
15 | - home: MyHomePage(), | 20 | + home: HomePage(title: 'FlutterScreenUtil Demo'), |
21 | + ); | ||
22 | + }, | ||
16 | ); | 23 | ); |
17 | } | 24 | } |
18 | } | 25 | } |
19 | 26 | ||
20 | -class MyHomePage extends StatelessWidget { | ||
21 | - @override | ||
22 | - Widget build(BuildContext context) { | ||
23 | - //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) | ||
24 | - ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false); | ||
25 | - return ExampleWidget(title: 'FlutterScreenUtil 示例'); | ||
26 | - } | ||
27 | -} | ||
28 | - | ||
29 | -class ExampleWidget extends StatefulWidget { | ||
30 | - const ExampleWidget({Key key, this.title}) : super(key: key); | 27 | +class HomePage extends StatefulWidget { |
28 | + const HomePage({Key key, this.title}) : super(key: key); | ||
31 | 29 | ||
32 | final String title; | 30 | final String title; |
33 | 31 | ||
34 | @override | 32 | @override |
35 | - _ExampleWidgetState createState() => _ExampleWidgetState(); | 33 | + _HomePageState createState() => _HomePageState(); |
36 | } | 34 | } |
37 | 35 | ||
38 | -class _ExampleWidgetState extends State<ExampleWidget> { | 36 | +class _HomePageState extends State<HomePage> { |
39 | @override | 37 | @override |
40 | Widget build(BuildContext context) { | 38 | Widget build(BuildContext context) { |
41 | printScreenInformation(); | 39 | printScreenInformation(); |
@@ -113,42 +111,20 @@ class _ExampleWidgetState extends State<ExampleWidget> { | @@ -113,42 +111,20 @@ class _ExampleWidgetState extends State<ExampleWidget> { | ||
113 | ], | 111 | ], |
114 | ), | 112 | ), |
115 | ), | 113 | ), |
116 | - floatingActionButton: FloatingActionButton( | ||
117 | - child: Icon(Icons.title), | ||
118 | - onPressed: () { | ||
119 | - ScreenUtil.init( | ||
120 | - context, | ||
121 | - designSize: Size(750, 1334), | ||
122 | - allowFontScaling: false, | ||
123 | - ); | ||
124 | - setState(() {}); | ||
125 | - }, | ||
126 | - ), | ||
127 | ); | 114 | ); |
128 | } | 115 | } |
129 | 116 | ||
130 | void printScreenInformation() { | 117 | void printScreenInformation() { |
131 | - print('设备宽度:${ScreenUtil().screenWidth}'); //Device width | ||
132 | - print('设备高度:${ScreenUtil().screenHeight}'); //Device height | ||
133 | - print('设备的像素密度:${ScreenUtil().pixelRatio}'); //Device pixel density | ||
134 | - print( | ||
135 | - '底部安全区距离:${ScreenUtil().bottomBarHeight}dp', | ||
136 | - ); //Bottom safe zone distance,suitable for buttons with full screen | ||
137 | - print( | ||
138 | - '状态栏高度:${ScreenUtil().statusBarHeight}dp', | ||
139 | - ); //Status bar height , Notch will be higher Unit px | ||
140 | - | 118 | + print('设备宽度:${1.sw}'); |
119 | + print('设备高度:${1.sh}'); | ||
120 | + print('设备的像素密度:${ScreenUtil().pixelRatio}'); | ||
121 | + print('底部安全区距离:${ScreenUtil().bottomBarHeight}dp'); | ||
122 | + print('状态栏高度:${ScreenUtil().statusBarHeight}dp'); | ||
141 | print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}'); | 123 | print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}'); |
142 | print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}'); | 124 | print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}'); |
143 | - | ||
144 | - print( | ||
145 | - '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}', | ||
146 | - ); | ||
147 | - print( | ||
148 | - '高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}', | ||
149 | - ); | 125 | + print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}'); |
126 | + print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}'); | ||
150 | print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}'); | 127 | print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}'); |
151 | - | ||
152 | print('屏幕宽度的0.5:${0.5.sw}'); | 128 | print('屏幕宽度的0.5:${0.5.sw}'); |
153 | print('屏幕高度的0.5:${0.5.sh}'); | 129 | print('屏幕高度的0.5:${0.5.sh}'); |
154 | } | 130 | } |
@@ -4,6 +4,7 @@ | @@ -4,6 +4,7 @@ | ||
4 | */ | 4 | */ |
5 | 5 | ||
6 | import 'package:flutter/material.dart'; | 6 | import 'package:flutter/material.dart'; |
7 | +import 'dart:ui' as ui; | ||
7 | 8 | ||
8 | class ScreenUtil { | 9 | class ScreenUtil { |
9 | static ScreenUtil _instance; | 10 | static ScreenUtil _instance; |
@@ -17,12 +18,12 @@ class ScreenUtil { | @@ -17,12 +18,12 @@ class ScreenUtil { | ||
17 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. | 18 | /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. |
18 | bool allowFontScaling = false; | 19 | bool allowFontScaling = false; |
19 | 20 | ||
20 | - static double _pixelRatio; | 21 | + static double _pixelRatio = 1; |
22 | + static double _textScaleFactor = 1; | ||
21 | static double _screenWidth; | 23 | static double _screenWidth; |
22 | static double _screenHeight; | 24 | static double _screenHeight; |
23 | static double _statusBarHeight; | 25 | static double _statusBarHeight; |
24 | static double _bottomBarHeight; | 26 | static double _bottomBarHeight; |
25 | - static double _textScaleFactor; | ||
26 | 27 | ||
27 | ScreenUtil._(); | 28 | ScreenUtil._(); |
28 | 29 | ||
@@ -35,7 +36,7 @@ class ScreenUtil { | @@ -35,7 +36,7 @@ class ScreenUtil { | ||
35 | } | 36 | } |
36 | 37 | ||
37 | static void init( | 38 | static void init( |
38 | - BuildContext context, { | 39 | + BoxConstraints constraints, { |
39 | Size designSize = defaultSize, | 40 | Size designSize = defaultSize, |
40 | bool allowFontScaling = false, | 41 | bool allowFontScaling = false, |
41 | }) { | 42 | }) { |
@@ -43,10 +44,11 @@ class ScreenUtil { | @@ -43,10 +44,11 @@ class ScreenUtil { | ||
43 | _instance | 44 | _instance |
44 | ..uiSize = designSize | 45 | ..uiSize = designSize |
45 | ..allowFontScaling = allowFontScaling; | 46 | ..allowFontScaling = allowFontScaling; |
46 | - MediaQueryData mediaQuery = MediaQuery.of(context); | 47 | + _screenWidth = constraints.maxWidth; |
48 | + _screenHeight = constraints.maxHeight; | ||
49 | + | ||
50 | + ui.Window mediaQuery = ui.window; | ||
47 | _pixelRatio = mediaQuery.devicePixelRatio; | 51 | _pixelRatio = mediaQuery.devicePixelRatio; |
48 | - _screenWidth = mediaQuery.size.width; | ||
49 | - _screenHeight = mediaQuery.size.height; | ||
50 | _statusBarHeight = mediaQuery.padding.top; | 52 | _statusBarHeight = mediaQuery.padding.top; |
51 | _bottomBarHeight = mediaQuery.padding.bottom; | 53 | _bottomBarHeight = mediaQuery.padding.bottom; |
52 | _textScaleFactor = mediaQuery.textScaleFactor; | 54 | _textScaleFactor = mediaQuery.textScaleFactor; |
@@ -78,11 +80,11 @@ class ScreenUtil { | @@ -78,11 +80,11 @@ class ScreenUtil { | ||
78 | 80 | ||
79 | /// 状态栏高度 dp 刘海屏会更高 | 81 | /// 状态栏高度 dp 刘海屏会更高 |
80 | /// The offset from the top | 82 | /// The offset from the top |
81 | - double get statusBarHeight => _statusBarHeight; | 83 | + double get statusBarHeight => _statusBarHeight / _pixelRatio; |
82 | 84 | ||
83 | /// 底部安全区距离 dp | 85 | /// 底部安全区距离 dp |
84 | /// The offset from the bottom. | 86 | /// The offset from the bottom. |
85 | - double get bottomBarHeight => _bottomBarHeight; | 87 | + double get bottomBarHeight => _bottomBarHeight / _pixelRatio; |
86 | 88 | ||
87 | /// 实际的dp与UI设计px的比例 | 89 | /// 实际的dp与UI设计px的比例 |
88 | /// The ratio of the actual dp to the design draft px | 90 | /// The ratio of the actual dp to the design draft px |
@@ -114,11 +116,8 @@ class ScreenUtil { | @@ -114,11 +116,8 @@ class ScreenUtil { | ||
114 | ///Font size adaptation method | 116 | ///Font size adaptation method |
115 | ///- [fontSize] The size of the font on the UI design, in px. | 117 | ///- [fontSize] The size of the font on the UI design, in px. |
116 | ///- [allowFontScaling] | 118 | ///- [allowFontScaling] |
117 | - double setSp(num fontSize, {bool allowFontScalingSelf}) => | ||
118 | - allowFontScalingSelf == null | ||
119 | - ? (allowFontScaling | ||
120 | - ? (fontSize * scaleText) | ||
121 | - : ((fontSize * scaleText) / _textScaleFactor)) | 119 | + double setSp(num fontSize, {bool allowFontScalingSelf}) => allowFontScalingSelf == null |
120 | + ? (allowFontScaling ? (fontSize * scaleText) : ((fontSize * scaleText) / _textScaleFactor)) | ||
122 | : (allowFontScalingSelf | 121 | : (allowFontScalingSelf |
123 | ? (fontSize * scaleText) | 122 | ? (fontSize * scaleText) |
124 | : ((fontSize * scaleText) / _textScaleFactor)); | 123 | : ((fontSize * scaleText) / _textScaleFactor)); |
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: 3.2.0 | ||
4 | -homepage: https://github.com/OpenFlutter/flutter_screenutil/tree/beta | 3 | +version: 4.0.0-beta |
4 | +homepage: https://github.com/OpenFlutter/flutter_screenutil | ||
5 | 5 | ||
6 | environment: | 6 | environment: |
7 | sdk: ">=2.6.0 <3.0.0" | 7 | sdk: ">=2.6.0 <3.0.0" |
-
Please register or login to post a comment