Skip to content

Commit 26fa97b

Browse files
author
李卓原
committed
4.0.0-beta
1 parent 0aef18d commit 26fa97b

File tree

8 files changed

+267
-270
lines changed

8 files changed

+267
-270
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @Description: Update log
77
-->
88

9+
# 4.0.0-beta
10+
- Modified the initialization method
11+
912
# 3.2.0
1013
- Modify the method name to be more semantic: wp->sw , hp->sh
1114
- Remove the restriction of flutter version

README.md

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ dependencies:
2323
flutter:
2424
sdk: flutter
2525
# add flutter_screenutil
26-
flutter_screenutil: ^3.1.0
26+
flutter_screenutil: ^4.0.0-beta
2727
```
2828
### Add the following imports to your Dart code:
2929
```
@@ -34,31 +34,41 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
3434

3535
|Property|Type|Default Value|Description|
3636
|:---|:---|:---|:---|
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|
3838
|allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option|
3939

4040
### 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.
4242

4343
```
44-
45-
void main() {
46-
WidgetsFlutterBinding.ensureInitialized();
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)
48-
ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: false);
49-
runApp(MyApp());
44+
void main() => runApp(MyApp());
45+
46+
class MyApp extends StatelessWidget {
47+
@override
48+
Widget build(BuildContext context) {
49+
return LayoutBuilder(
50+
builder: (context, constraints) {
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)
52+
ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false);
53+
54+
return MaterialApp(
55+
...
56+
);
57+
},
58+
);
59+
}
5060
}
5161
5262
//fill in the screen size of the device in the design
5363
5464
//default value : width : 1080px , height:1920px , allowFontScaling:false
55-
ScreenUtil.init(context);
65+
ScreenUtil.init(constraints);
5666
5767
//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));
5969
6070
//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);
6272
6373
```
6474

@@ -69,33 +79,33 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
6979
#### Pass the px size of the design draft
7080

7181
```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)
7787
7888
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
8191
ScreenUtil().bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen
8292
ScreenUtil().statusBarHeight //Status bar height , Notch will be higher Unit px
8393
ScreenUtil().textScaleFactor //System font scaling factor
8494
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
8797
8898
0.2.sw //0.2 times the screen width
8999
0.5.sh //50% of screen height
90100
```
91101

92102
#### Adapt screen size:
93103

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))
95105

96106
Adapted to screen width: `ScreenUtil().setWidth(540)`,
97107

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
99109

100110
If your dart sdk>=2.6, you can use extension functions:
101111

@@ -123,11 +133,12 @@ setHeight method is mainly adapted in height, you want to control the height and
123133

124134
```dart
125135
//for example:
126-
//rectangle
136+
137+
///If you want to display a square:
138+
///rectangle
127139
Container(
128140
width: ScreenUtil().setWidth(375),
129-
height: ScreenUtil().setHeight(200),
130-
...
141+
height: ScreenUtil().setWidth(200),
131142
),
132143
133144
////If you want to display a square:
@@ -138,9 +149,10 @@ Container(
138149
139150
```
140151

152+
141153
#### Adapter font:
142154
``` 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
144156
//(AllowallowFontScaling when initializing ScreenUtil)
145157
ScreenUtil().setSp(28)
146158
28.sp

README_CN.md

Lines changed: 59 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies:
2828
flutter:
2929
sdk: flutter
3030
# 添加依赖
31-
flutter_screenutil: ^3.1.0
31+
flutter_screenutil: ^4.0.0-beta
3232
```
3333
### 在每个使用的地方导入包:
3434
```
@@ -38,32 +38,42 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
3838
### 属性
3939

4040
|属性|类型|默认值|描述|
41-
|:---|:---|:---|:---|
42-
|width|double|1080px|设计稿中设备的宽度,单位px|
43-
|height|double|1920px|设计稿中设备的高度,单位px|
41+
|:---|:---|:---|:---|
42+
|designSize|Size|Size(1080, 1920)|设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)|
4443
|allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放|
4544

4645
### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
47-
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px)
48-
一定在MaterialApp的home中的页面设置(即入口文件,只需设置一次),以保证在每次使用之前设置好了适配尺寸:
46+
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
47+
一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸:
4948

5049
```
5150
//填入设计稿中设备的屏幕尺寸
52-
void main() {
53-
WidgetsFlutterBinding.ensureInitialized();
54-
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
55-
ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false);
56-
runApp(MyApp());
57-
}
5851
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) {
59+
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
60+
ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: false);
61+
62+
return MaterialApp(
63+
...
64+
);
65+
},
66+
);
67+
}
68+
}
5969
//默认 width : 1080px , height:1920px , allowFontScaling:false
60-
ScreenUtil.init(context);
70+
ScreenUtil.init(constraints);
6171
6272
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
63-
ScreenUtil.init(context, designSize: Size(750, 1334));
73+
ScreenUtil.init(constraints, designSize: Size(750, 1334));
6474
6575
//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
66-
ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
76+
ScreenUtil.init(constraints, designSize: Size(750, 1334), allowFontScaling: true);
6777
6878
```
6979

@@ -73,7 +83,7 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
7383
#### 传入设计稿的px尺寸 px px px !
7484
```dart
7585
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) //根据屏幕高度适配尺寸(一般根据宽度适配即可)
7787
ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体
7888
ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放)
7989
ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //适配字体(不会根据系统的“字体大小”辅助选项来进行缩放)
@@ -82,11 +92,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
8292
ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度
8393
ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度
8494
ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
85-
ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px
95+
ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高
8696
ScreenUtil.textScaleFactor //系统字体缩放比例
8797
88-
ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例
89-
ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例
98+
ScreenUtil().scaleWidth // 实际宽度的dp与设计稿宽度的比例
99+
ScreenUtil().scaleHeight // 实际高度的dp与设计稿高度度的比例
90100
91101
0.2.sw //屏幕宽度的0.2倍
92102
0.5.sh //屏幕高度的50%
@@ -95,11 +105,11 @@ ScreenUtil.init(context, designSize: Size(750, 1334), allowFontScaling: true);
95105

96106
#### 适配尺寸
97107

98-
传入设计稿的px尺寸
108+
传入设计稿的尺寸(单位与初始化时的单位相同)
99109

100110
根据屏幕宽度适配 `width: ScreenUtil().setWidth(540)`,
101111

102-
根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`,
112+
根据屏幕高度适配 `height: ScreenUtil().setHeight(200)`, 一般来说,控件高度也根据宽度进行适配
103113

104114
**注意**
105115

@@ -113,7 +123,7 @@ setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的
113123
//UI上是长方形:
114124
Container(
115125
width: ScreenUtil().setWidth(375),
116-
height: ScreenUtil().setHeight(200),
126+
height: ScreenUtil().setHeight(375),
117127
),
118128
119129
//如果你想显示一个正方形:
@@ -141,10 +151,10 @@ height:200.h
141151
```
142152

143153
#### 适配字体:
144-
传入设计稿的px尺寸
154+
传入设计稿的字体大小
145155

146156
```
147-
//传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
157+
//传入字体大小(单位和初始化时的单位保持一致),默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
148158
ScreenUtil().setSp(28)
149159
150160
28.sp (dart sdk>=2.6)
@@ -183,36 +193,34 @@ void main() => runApp(MyApp());
183193
class MyApp extends StatelessWidget {
184194
@override
185195
Widget build(BuildContext context) {
186-
return MaterialApp(
187-
debugShowCheckedModeBanner: false,
188-
title: 'Flutter_ScreenUtil',
189-
theme: ThemeData(
190-
primarySwatch: Colors.blue,
191-
),
192-
home: MyHomePage(),
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+
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+
},
193210
);
194211
}
195212
}
196213
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);
208216
209217
final String title;
210218
211219
@override
212-
_ExampleWidgetState createState() => _ExampleWidgetState();
220+
_HomePageState createState() => _HomePageState();
213221
}
214222
215-
class _ExampleWidgetState extends State<ExampleWidget> {
223+
class _HomePageState extends State<HomePage> {
216224
@override
217225
Widget build(BuildContext context) {
218226
printScreenInformation();
@@ -290,42 +298,20 @@ class _ExampleWidgetState extends State<ExampleWidget> {
290298
],
291299
),
292300
),
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-
),
304301
);
305302
}
306303
307304
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');
318310
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
319311
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}');
327314
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
328-
329315
print('屏幕宽度的0.5:${0.5.sw}');
330316
print('屏幕高度的0.5:${0.5.sh}');
331317
}

0 commit comments

Comments
 (0)