Skip to content

Commit c88f48c

Browse files
authored
Merge pull request #181 from Overman775/init_widget_master
Init widget
2 parents eea2cdc + 4fb585c commit c88f48c

File tree

7 files changed

+111
-77
lines changed

7 files changed

+111
-77
lines changed

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,41 @@
1818
### Add dependency:
1919
Please check the latest version before installation.
2020
If there is any problem with the new version, please use the previous version
21-
```
21+
```yaml
2222
dependencies:
2323
flutter:
2424
sdk: flutter
2525
# add flutter_screenutil
2626
flutter_screenutil: ^4.0.0-beta1
2727
```
2828
### Add the following imports to your Dart code:
29-
```
29+
```dart
3030
import 'package:flutter_screenutil/flutter_screenutil.dart';
3131
```
3232

3333
### Property
3434

3535
|Property|Type|Default Value|Description|
36-
|:---|:---|:---|:---|
36+
|:---|:---|:---|:---|
3737
|designSize|Size|Size(1080, 1920)|The size of the device in the design draft, in px|
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
4141
Please set the size of the design draft before use, the width and height of the design draft.
4242

43-
```
43+
```dart
4444
void main() => runApp(MyApp());
4545
4646
class MyApp extends StatelessWidget {
4747
@override
4848
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-
},
49+
//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)
50+
return ScreenUtilInit(
51+
designSize: Size(750, 1334),
52+
allowFontScaling: false,
53+
child: MaterialApp(
54+
...
55+
),
5856
);
5957
}
6058
}
@@ -112,14 +110,14 @@ If your dart sdk>=2.6, you can use extension functions:
112110
example:
113111

114112
instead of :
115-
```
113+
```dart
116114
Container(
117115
width: ScreenUtil().setWidth(50),
118116
height:ScreenUtil().setHeight(200),
119117
)
120118
```
121119
you can use it like this:
122-
```
120+
```dart
123121
Container(
124122
width: 50.w,
125123
height:200.h

README_CN.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,48 @@
2323

2424
安装之前请查看最新版本
2525
新版本如有问题请使用上一版
26-
```
26+
```yaml
2727
dependencies:
2828
flutter:
2929
sdk: flutter
3030
# 添加依赖
3131
flutter_screenutil: ^4.0.0-beta1
3232
```
3333
### 在每个使用的地方导入包:
34-
```
34+
```dart
3535
import 'package:flutter_screenutil/flutter_screenutil.dart';
3636
```
3737

3838
### 属性
3939

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

4545
### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
4646
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位随意,但在使用过程中必须保持一致)
4747
一定要进行初始化(只需设置一次),以保证在每次使用之前设置好了适配尺寸:
4848

49-
```
49+
```dart
5050
//填入设计稿中设备的屏幕尺寸
5151
5252
void main() => runApp(MyApp());
5353
5454
class MyApp extends StatelessWidget {
5555
@override
5656
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-
},
57+
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
58+
return ScreenUtilInit(
59+
designSize: Size(750, 1334),
60+
allowFontScaling: false,
61+
child: MaterialApp(
62+
...
63+
),
6664
);
6765
}
6866
}
67+
6968
//默认 width : 1080px , height:1920px , allowFontScaling:false
7069
ScreenUtil.init(constraints);
7170
@@ -119,7 +118,7 @@ setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的
119118

120119
例如:
121120

122-
```
121+
```dart
123122
//UI上是长方形:
124123
Container(
125124
width: ScreenUtil().setWidth(375),
@@ -136,14 +135,14 @@ Container(
136135
如果你的dart sdk>=2.6,可以使用扩展函数:
137136
example:
138137
不用这个:
139-
```
138+
```dart
140139
Container(
141140
width: ScreenUtil().setWidth(50),
142141
height:ScreenUtil().setHeight(200),
143142
)
144143
```
145144
而是用这个:
146-
```
145+
```dart
147146
Container(
148147
width: 50.w,
149148
height:200.h
@@ -153,7 +152,7 @@ height:200.h
153152
#### 适配字体:
154153
传入设计稿的字体大小:
155154

156-
```
155+
```dart
157156
//传入字体大小(单位和初始化时的单位保持一致),默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
158157
ScreenUtil().setSp(28)
159158
@@ -184,12 +183,12 @@ Column(
184183
)
185184
```
186185

187-
```
186+
188187
[widget test](https://github.com/OpenFlutter/flutter_screenutil/issues/115)
189188

190189
### 使用示例:
191190

192-
[example demo](https://github.com/OpenFlutter/flutter_screenutil/blob/master/example/lib/main_zh.dart)
191+
[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart)
193192

194193
效果:
195194

README_PT.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
### Adicionando a dependência:
2020
Por favor, verifique a última versão antes da instalação.
2121
Se houver algum problema com a nova versão, use a versão anterior
22-
```
22+
```yaml
2323
dependencies:
2424
flutter:
2525
sdk: flutter
2626
# add flutter_screenutil
27-
flutter_screenutil: ^4.0.0-beta1
27+
flutter_screenutil: ^4.0.0-beta
2828
```
2929
3030
### Adicione o seguinte import em seu código Dart:
31-
```
31+
```dart
3232
import 'package:flutter_screenutil/flutter_screenutil.dart';
3333
```
3434

@@ -45,12 +45,20 @@ Por favor, defina a largura e altura do protótipo de design antes de usar (em p
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

4747
```dart
48-
//Preencha o tamanho da tela do dispositivo no protótipo de design
49-
void main() {
50-
WidgetsFlutterBinding.ensureInitialized();
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-
runApp(MyApp());
48+
void main() => runApp(MyApp());
49+
50+
class MyApp extends StatelessWidget {
51+
@override
52+
Widget build(BuildContext context) {
53+
//Preencha o tamanho da tela do dispositivo no protótipo de design
54+
return ScreenUtilInit(
55+
designSize: Size(750, 1334),
56+
allowFontScaling: false,
57+
child: MaterialApp(
58+
...
59+
),
60+
);
61+
}
5462
}
5563
5664
//Valor padrão: width : 1080px , height:1920px , allowFontScaling:false

example/lib/main.dart

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ void main() => runApp(MyApp());
66
class MyApp extends StatelessWidget {
77
@override
88
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-
14-
return MaterialApp(
15-
debugShowCheckedModeBanner: false,
16-
title: 'Flutter_ScreenUtil',
17-
theme: ThemeData(
18-
primarySwatch: Colors.blue,
19-
textTheme: TextTheme(button: TextStyle(fontSize: 80.nsp)),
20-
),
21-
home: HomePage(title: 'FlutterScreenUtil Demo'),
22-
);
23-
},
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(750, 1334),
12+
allowFontScaling: false,
13+
child: MaterialApp(
14+
debugShowCheckedModeBanner: false,
15+
title: 'Flutter_ScreenUtil',
16+
theme: ThemeData(
17+
primarySwatch: Colors.blue,
18+
),
19+
home: HomePage(title: 'FlutterScreenUtil Demo'),
20+
),
2421
);
2522
}
2623
}
@@ -130,10 +127,8 @@ class _HomePageState extends State<HomePage> {
130127
print('Device width dp:${1.sw}dp');
131128
print('Device height dp:${1.sh}dp');
132129
print('Device pixel density:${ScreenUtil().pixelRatio}');
133-
print(
134-
'Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp');
135-
print(
136-
'Status bar height dp:${ScreenUtil().statusBarHeight}dp');
130+
print('Bottom safe zone distance dp:${ScreenUtil().bottomBarHeight}dp');
131+
print('Status bar height dp:${ScreenUtil().statusBarHeight}dp');
137132
print('Ratio of actual width dp to UI Design:${ScreenUtil().scaleWidth}');
138133
print('Ratio of actual height dp to UI Design:${ScreenUtil().scaleHeight}');
139134
print('System font scaling:${ScreenUtil().textScaleFactor}');

example/lib/main_zh.dart

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@ void main() => runApp(MyApp());
66
class MyApp extends StatelessWidget {
77
@override
88
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-
14-
return MaterialApp(
15-
debugShowCheckedModeBanner: false,
16-
title: 'Flutter_ScreenUtil',
17-
theme: ThemeData(
18-
primarySwatch: Colors.blue,
19-
textTheme: TextTheme(button: TextStyle(fontSize: 80.nsp)),
20-
),
21-
home: HomePage(title: 'FlutterScreenUtil Demo'),
22-
);
23-
},
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(750, 1334),
12+
allowFontScaling: false,
13+
child: MaterialApp(
14+
debugShowCheckedModeBanner: false,
15+
title: 'Flutter_ScreenUtil',
16+
theme: ThemeData(
17+
primarySwatch: Colors.blue,
18+
),
19+
home: HomePage(title: 'FlutterScreenUtil Demo'),
20+
),
2421
);
2522
}
2623
}

lib/flutter_screenutil.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ library flutter_screenutil;
77

88
export 'size_extension.dart';
99
export 'screenutil.dart';
10+
export 'screenutil_init.dart';

lib/screenutil_init.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:flutter/widgets.dart';
2+
3+
import 'screenutil.dart';
4+
5+
class ScreenUtilInit extends StatelessWidget {
6+
/// A helper widget that initializes [ScreenUtil]
7+
const ScreenUtilInit({
8+
@required this.child,
9+
this.designSize = ScreenUtil.defaultSize,
10+
this.allowFontScaling = false,
11+
Key key,
12+
}) : super(key: key);
13+
14+
final Widget child;
15+
16+
/// The [Size] of the device in the design draft, in px
17+
final Size designSize;
18+
19+
/// Sets whether the font size is scaled according to the system's "font size" assist option
20+
final bool allowFontScaling;
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return LayoutBuilder(
25+
builder: (_, BoxConstraints constraints) {
26+
ScreenUtil.init(
27+
constraints,
28+
designSize: designSize,
29+
allowFontScaling: allowFontScaling,
30+
);
31+
32+
return child;
33+
},
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)