@@ -181,10 +181,12 @@ Column(
181
181
```
182
182
183
183
``` dart
184
- import 'package:flutter/material.dart';
185
- import 'package:flutter_screenutil/flutter_screenutil.dart';
186
-
187
- void main() => runApp( MyApp());
184
+ void main() {
185
+ WidgetsFlutterBinding.ensureInitialized();
186
+ //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
187
+ ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false);
188
+ runApp(MyApp());
189
+ }
188
190
189
191
class MyApp extends StatelessWidget {
190
192
@override
@@ -195,32 +197,11 @@ class MyApp extends StatelessWidget {
195
197
theme: ThemeData(
196
198
primarySwatch: Colors.blue,
197
199
),
198
- home: MyHomePage( ),
200
+ home: ExampleWidget(title: 'FlutterScreenUtil示例' ),
199
201
);
200
202
}
201
203
}
202
204
203
- class MyHomePage extends StatefulWidget {
204
- MyHomePage({Key key, this.title}) : super(key: key);
205
-
206
- final String title;
207
-
208
- @override
209
- _MyHomePageState createState() => _MyHomePageState();
210
- }
211
-
212
- class _MyHomePageState extends State<MyHomePage> {
213
- @override
214
- Widget build(BuildContext context) {
215
- //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
216
- ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
217
- ScreenUtil.init();
218
- ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
219
-
220
- return ExampleWidget(title: 'FlutterScreenUtil示例');
221
- }
222
- }
223
-
224
205
class ExampleWidget extends StatefulWidget {
225
206
const ExampleWidget({Key key, this.title}) : super(key: key);
226
207
@@ -250,33 +231,33 @@ class _ExampleWidgetState extends State<ExampleWidget> {
250
231
height: ScreenUtil().setHeight(200),
251
232
color: Colors.red,
252
233
child: Text(
253
- '我的宽度:${ScreenUtil().setWidth(375) }dp \n'
234
+ '我的宽度:${0.5.wp }dp \n'
254
235
'我的高度:${ScreenUtil().setHeight(200)}dp',
255
236
style: TextStyle(
256
237
color: Colors.white, fontSize: ScreenUtil().setSp(24)),
257
238
),
258
239
),
259
240
Container(
260
241
padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
261
- width: ScreenUtil().setWidth( 375) ,
262
- height: ScreenUtil().setHeight( 200) ,
242
+ width: 375.w ,
243
+ height: 200.h ,
263
244
color: Colors.blue,
264
245
child: Text(
265
- '我的宽度:${ScreenUtil().setWidth( 375) }dp \n'
266
- '我的高度:${ScreenUtil().setHeight( 200) }dp',
246
+ '我的宽度:${375.w }dp \n'
247
+ '我的高度:${200.h }dp',
267
248
style: TextStyle(
268
249
color: Colors.white,
269
250
fontSize: ScreenUtil().setSp(24))),
270
251
),
271
252
],
272
253
),
273
- Text('设备宽度:${ScreenUtil.screenWidth}dp '),
274
- Text('设备高度:${ScreenUtil.screenHeight}dp '),
275
- Text('设备宽度:${ScreenUtil.screenWidthPx}px '),
276
- Text('设备高度:${ScreenUtil.screenHeightPx}px '),
277
- Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
278
- Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'),
279
- Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'),
254
+ Text('设备宽度:${ScreenUtil().screenWidthPx}px '),
255
+ Text('设备高度:${ScreenUtil().screenHeightPx}px '),
256
+ Text('设备宽度:${ScreenUtil().screenWidth}dp '),
257
+ Text('设备高度:${ScreenUtil().screenHeight}dp '),
258
+ Text('设备的像素密度:${ScreenUtil() .pixelRatio}'),
259
+ Text('底部安全区距离:${ScreenUtil() .bottomBarHeight}dp'),
260
+ Text('状态栏高度:${ScreenUtil() .statusBarHeight}dp'),
280
261
Text(
281
262
'实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}',
282
263
textAlign: TextAlign.center,
@@ -286,22 +267,20 @@ class _ExampleWidgetState extends State<ExampleWidget> {
286
267
textAlign: TextAlign.center,
287
268
),
288
269
SizedBox(
289
- height: ScreenUtil().setHeight( 100) ,
270
+ height: 100.h ,
290
271
),
291
- Text('系统的字体缩放比例:${ScreenUtil.textScaleFactor}'),
272
+ Text('系统的字体缩放比例:${ScreenUtil() .textScaleFactor}'),
292
273
Column(
293
274
crossAxisAlignment: CrossAxisAlignment.start,
294
275
children: <Widget>[
295
- Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化',
296
- style: TextStyle(
297
- color: Colors.black,
298
- fontSize: ScreenUtil().setSp(24),
299
- )),
300
- Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化',
301
- style: TextStyle(
302
- color: Colors.black,
303
- fontSize: ScreenUtil()
304
- .setSp(24, allowFontScalingSelf: true))),
276
+ Text(
277
+ '我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化',
278
+ style: ts.t2,
279
+ ),
280
+ Text(
281
+ '我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化',
282
+ style: ts.t1,
283
+ ),
305
284
],
306
285
)
307
286
],
@@ -310,28 +289,40 @@ class _ExampleWidgetState extends State<ExampleWidget> {
310
289
floatingActionButton: FloatingActionButton(
311
290
child: Icon(Icons.title),
312
291
onPressed: () {
313
- ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
292
+ ScreenUtil.init(
293
+ designSize: Size(1500, 1334),
294
+ allowFontScaling: false,
295
+ );
314
296
setState(() {});
315
297
},
316
298
),
317
299
);
318
300
}
319
301
320
302
void printScreenInformation() {
321
- print('设备宽度:${ScreenUtil.screenWidth}'); //Device width
322
- print('设备高度:${ScreenUtil.screenHeight}'); //Device height
323
- print('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
303
+ print('设备宽度:${ScreenUtil() .screenWidth}'); //Device width
304
+ print('设备高度:${ScreenUtil() .screenHeight}'); //Device height
305
+ print('设备的像素密度:${ScreenUtil() .pixelRatio}'); //Device pixel density
324
306
print(
325
- '底部安全区距离:${ScreenUtil.bottomBarHeight}dp'); //Bottom safe zone distance,suitable for buttons with full screen
307
+ '底部安全区距离:${ScreenUtil().bottomBarHeight}dp',
308
+ ); //Bottom safe zone distance,suitable for buttons with full screen
326
309
print(
327
- '状态栏高度:${ScreenUtil.statusBarHeight}dp'); //状态栏高度 , 刘海屏会更高,单位dp
310
+ '状态栏高度:${ScreenUtil().statusBarHeight}dp',
311
+ ); //Status bar height , Notch will be higher Unit px
328
312
329
313
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
330
314
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
315
+
331
316
print(
332
- '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
333
- print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
334
- print('系统的字体缩放比例:${ScreenUtil.textScaleFactor}');
317
+ '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}',
318
+ );
319
+ print(
320
+ '高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}',
321
+ );
322
+ print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
323
+
324
+ print('屏幕宽度的0.5:${0.5.wp}');
325
+ print('屏幕高度的0.5:${0.5.hp}');
335
326
}
336
327
}
337
328
0 commit comments