@@ -28,7 +28,7 @@ dependencies:
28
28
flutter:
29
29
sdk: flutter
30
30
# 添加依赖
31
- flutter_screenutil: ^3.1.0
31
+ flutter_screenutil: ^4.0.0-beta
32
32
```
33
33
### 在每个使用的地方导入包:
34
34
```
@@ -38,32 +38,42 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
38
38
### 属性
39
39
40
40
| 属性| 类型| 默认值| 描述|
41
- | :---| :---| :---| :---|
42
- | width| double| 1080px| 设计稿中设备的宽度,单位px|
43
- | height| double| 1920px| 设计稿中设备的高度,单位px|
41
+ | :---| :---| :---| :---|
42
+ | designSize| Size| Size(1080, 1920)| 设计稿中设备的尺寸(单位随意,但在使用过程中必须保持一致)|
44
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();
54
- //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
55
- ScreenUtil.init(context,designSize: Size(750, 1334), allowFontScaling: false);
56
- runApp(MyApp());
57
- }
58
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) {
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
+ }
59
69
//默认 width : 1080px , height:1920px , allowFontScaling:false
60
- ScreenUtil.init(context );
70
+ ScreenUtil.init(constraints );
61
71
62
72
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
63
- ScreenUtil.init(context , designSize: Size(750, 1334));
73
+ ScreenUtil.init(constraints , designSize: Size(750, 1334));
64
74
65
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
83
#### 传入设计稿的px尺寸 px px px !
74
84
``` dart
75
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
87
ScreenUtil().setSp(24) (sdk>=2.6 : 24.sp) //适配字体
78
88
ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //适配字体(根据系统的“字体大小”辅助选项来进行缩放)
79
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
92
ScreenUtil.screenWidth (sdk>=2.6 : 1.sw) //设备宽度
83
93
ScreenUtil.screenHeight (sdk>=2.6 : 1.sh) //设备高度
84
94
ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
85
- ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px
95
+ ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高
86
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
101
0.2.sw //屏幕宽度的0.2倍
92
102
0.5.sh //屏幕高度的50%
@@ -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
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
123
//UI上是长方形:
114
124
Container(
115
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
151
```
142
152
143
153
#### 适配字体:
144
- 传入设计稿的px尺寸 :
154
+ 传入设计稿的字体大小 :
145
155
146
156
```
147
- //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
157
+ //传入字体大小(单位和初始化时的单位保持一致) ,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
148
158
ScreenUtil().setSp(28)
149
159
或
150
160
28.sp (dart sdk>=2.6)
@@ -183,36 +193,34 @@ void main() => runApp(MyApp());
183
193
class MyApp extends StatelessWidget {
184
194
@override
185
195
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
+ },
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
217
final String title;
210
218
211
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
224
@override
217
225
Widget build(BuildContext context) {
218
226
printScreenInformation();
@@ -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
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
310
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
319
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
314
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
328
-
329
315
print('屏幕宽度的0.5:${0.5.sw}');
330
316
print('屏幕高度的0.5:${0.5.sh}');
331
317
}
0 commit comments