Skip to content

Commit f29a851

Browse files
author
李卓原
committed
添加新的初始化方式
1 parent f9908fb commit f29a851

File tree

6 files changed

+76
-32
lines changed

6 files changed

+76
-32
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
4040
### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option
4141
Please set the size of the design draft before use, the width and height of the design draft.
4242

43+
The first way:
4344
```dart
4445
void main() => runApp(MyApp());
4546
@@ -57,6 +58,24 @@ class MyApp extends StatelessWidget {
5758
}
5859
}
5960
```
61+
The second way:
62+
```
63+
class MyApp extends StatelessWidget {
64+
@override
65+
Widget build(BuildContext context) {
66+
//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 360*690
67+
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
68+
return MaterialApp(
69+
debugShowCheckedModeBanner: false,
70+
title: 'Flutter_ScreenUtil',
71+
theme: ThemeData(
72+
primarySwatch: Colors.blue,
73+
),
74+
home: HomePage(title: 'FlutterScreenUtil Demo'),
75+
);
76+
}
77+
}
78+
```
6079

6180
### Use:
6281

README_CN.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
4646
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
4747
一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸:
4848

49+
方式一:
4950
```dart
50-
//填入设计稿中设备的屏幕尺寸
51-
5251
void main() => runApp(MyApp());
5352
5453
class MyApp extends StatelessWidget {
5554
@override
5655
Widget build(BuildContext context) {
56+
//填入设计稿中设备的屏幕尺寸,单位dp
5757
return ScreenUtilInit(
5858
designSize: Size(360, 690),
5959
allowFontScaling: false,
@@ -69,6 +69,24 @@ class MyApp extends StatelessWidget {
6969
}
7070
}
7171
```
72+
方式二:
73+
```
74+
class MyApp extends StatelessWidget {
75+
@override
76+
Widget build(BuildContext context) {
77+
//填入设计稿中设备的屏幕尺寸 (例如:360*690) , 单位dp
78+
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
79+
return MaterialApp(
80+
debugShowCheckedModeBanner: false,
81+
title: 'Flutter_ScreenUtil',
82+
theme: ThemeData(
83+
primarySwatch: Colors.blue,
84+
),
85+
home: HomePage(title: 'FlutterScreenUtil Demo'),
86+
);
87+
}
88+
}
89+
```
7290

7391
### 使用
7492

README_PT.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
4444
Por favor, defina a largura e altura do protótipo de design antes de usar (em pixels).
4545
Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou seja, no arquivo de entrada, defina apenas uma vez) para garantir que o tamanho de ajuste seja o mesmo antes de cada uso:
4646

47+
The first way:
4748
```dart
4849
void main() => runApp(MyApp());
4950
5051
class MyApp extends StatelessWidget {
5152
@override
5253
Widget build(BuildContext context) {
53-
//Preencha o tamanho da tela do dispositivo no protótipo de design , in dp
54+
//Set the fit size (fill in the screen size of the device in the design,in dp)
5455
return ScreenUtilInit(
5556
designSize: Size(360, 690),
5657
allowFontScaling: false,
@@ -61,6 +62,24 @@ class MyApp extends StatelessWidget {
6162
}
6263
}
6364
```
65+
The second way:
66+
```
67+
class MyApp extends StatelessWidget {
68+
@override
69+
Widget build(BuildContext context) {
70+
//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 360*690
71+
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
72+
return MaterialApp(
73+
debugShowCheckedModeBanner: false,
74+
title: 'Flutter_ScreenUtil',
75+
theme: ThemeData(
76+
primarySwatch: Colors.blue,
77+
),
78+
home: HomePage(title: 'FlutterScreenUtil Demo'),
79+
);
80+
}
81+
}
82+
```
6483

6584
### Uso:
6685

example/lib/main_zh.dart

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@ void main() => runApp(MyApp());
66
class MyApp extends StatelessWidget {
77
@override
88
Widget build(BuildContext context) {
9-
//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)
10-
return ScreenUtilInit(
11-
designSize: Size(360, 690),
12-
allowFontScaling: false,
13-
builder: () => MaterialApp(
14-
debugShowCheckedModeBanner: false,
15-
title: 'Flutter_ScreenUtil',
16-
theme: ThemeData(
17-
primarySwatch: Colors.blue,
18-
),
19-
home: HomePage(title: 'FlutterScreenUtil Demo'),
9+
//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 360*690
10+
ScreenUtil.init(BoxConstraints(maxWidth: 360, maxHeight: 690));
11+
return MaterialApp(
12+
debugShowCheckedModeBanner: false,
13+
title: 'Flutter_ScreenUtil',
14+
theme: ThemeData(
15+
primarySwatch: Colors.blue,
2016
),
17+
home: HomePage(title: 'FlutterScreenUtil Demo'),
2118
);
2219
}
2320
}
@@ -64,9 +61,7 @@ class _HomePageState extends State<HomePage> {
6461
child: Text(
6562
'我的设计稿宽度: 180dp \n'
6663
'我的设计稿高度: 200dp',
67-
style: TextStyle(
68-
color: Colors.white,
69-
fontSize: ScreenUtil().setSp(12))),
64+
style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(12))),
7065
),
7166
],
7267
),
@@ -133,10 +128,8 @@ class _HomePageState extends State<HomePage> {
133128
print('状态栏高度:${ScreenUtil().statusBarHeight}dp');
134129
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
135130
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
136-
print(
137-
'宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
138-
print(
139-
'高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
131+
print('宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil().pixelRatio}');
132+
print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil().pixelRatio}');
140133
print('系统的字体缩放比例:${ScreenUtil().textScaleFactor}');
141134
print('屏幕宽度的0.5:${0.5.sw}dp');
142135
print('屏幕高度的0.5:${0.5.sh}dp');

lib/screen_util.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class ScreenUtil {
3737
}
3838

3939
static void init(
40-
BoxConstraints constraints,
41-
Orientation orientation, {
40+
BoxConstraints constraints, {
41+
Orientation orientation = Orientation.portrait,
4242
Size designSize = defaultSize,
4343
bool allowFontScaling = false,
4444
}) {
@@ -125,12 +125,7 @@ class ScreenUtil {
125125
///Font size adaptation method
126126
///- [fontSize] The size of the font on the UI design, in dp.
127127
///- [allowFontScaling]
128-
double setSp(num fontSize, {bool? allowFontScalingSelf}) =>
129-
allowFontScalingSelf == null
130-
? (allowFontScaling
131-
? (fontSize * scaleText) * _textScaleFactor
132-
: (fontSize * scaleText))
133-
: (allowFontScalingSelf
134-
? (fontSize * scaleText) * _textScaleFactor
135-
: (fontSize * scaleText));
128+
double setSp(num fontSize, {bool? allowFontScalingSelf}) => allowFontScalingSelf == null
129+
? (allowFontScaling ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText))
130+
: (allowFontScalingSelf ? (fontSize * scaleText) * _textScaleFactor : (fontSize * scaleText));
136131
}

lib/screenutil_init.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ScreenUtilInit extends StatelessWidget {
2626
if (constraints.maxWidth != 0) {
2727
ScreenUtil.init(
2828
constraints,
29-
orientation,
29+
orientation: orientation,
3030
designSize: designSize,
3131
allowFontScaling: allowFontScaling,
3232
);

0 commit comments

Comments
 (0)