1
-
2
1
# flutter_screenutil
3
2
[ ![ pub package] ( https://img.shields.io/pub/v/flutter_screenutil.svg )] ( https://pub.dev/packages/flutter_screenutil )
4
3
@@ -29,9 +28,8 @@ dependencies:
29
28
flutter:
30
29
sdk: flutter
31
30
# add flutter_screenutil
32
- flutter_screenutil: ^2. 3.0
31
+ flutter_screenutil: ^3.0.0-beta.1
33
32
```
34
-
35
33
### Add the following imports to your Dart code:
36
34
```
37
35
import 'package:flutter_screenutil/flutter_screenutil.dart';
@@ -41,26 +39,31 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
41
39
42
40
| Property| Type| Default Value| Description|
43
41
| :---| :---| :---| :---|
44
- | width| double| 1080px| The width of the device in the design draft, in px|
45
- | height| double| 1920px| The height of the device in the design draft, in px|
42
+ | designSize| Size| Size(1080, 1920)| The size of the device in the design draft, in px|
46
43
| allowFontScaling| bool| false| Sets whether the font size is scaled according to the system's "font size" assist option|
47
44
48
45
### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option
49
- Please set the width and height of the design draft before use, the width and height of the design draft (unit px).
50
- Be sure to set the page in the MaterialApp's home/initialRoute(ie the entry file, just set it once) to ensure that the fit size is set before each use:
46
+ Please set the size of the design draft before use, the width and height of the design draft (unit px).
51
47
52
- ``` dart
48
+ ```
49
+
50
+ void main() {
51
+ WidgetsFlutterBinding.ensureInitialized();
52
+ //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)
53
+ ScreenUtil.init(designSize: Size(750, 1334), allowFontScaling: false);
54
+ runApp(MyApp());
55
+ }
53
56
54
57
//fill in the screen size of the device in the design
55
58
56
59
//default value : width : 1080px , height:1920px , allowFontScaling:false
57
- ScreenUtil.init(context );
60
+ ScreenUtil.init();
58
61
59
62
//If the design is based on the size of the iPhone6 (iPhone6 750*1334)
60
- ScreenUtil.init(context, width: 750, height: 1334);
63
+ ScreenUtil.init(designSize: Size( 750, 1334) );
61
64
62
65
//If you want to set the font size is scaled according to the system's "font size" assist option
63
- ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true);
66
+ ScreenUtil.init(designSize: Size( 750, 1334) , allowFontScaling: true);
64
67
65
68
```
66
69
@@ -77,12 +80,12 @@ ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true);
77
80
ScreenUtil().setSp(24, allowFontScalingSelf: true) (sdk>=2.6 : 24.ssp) //Adapter font(fonts will scale to respect Text Size accessibility settings)
78
81
ScreenUtil().setSp(24, allowFontScalingSelf: false) (sdk>=2.6 : 24.nsp) //Adapter font(fonts will not scale to respect Text Size accessibility settings)
79
82
80
- ScreenUtil.pixelRatio //Device pixel density
81
- ScreenUtil.screenWidth (sdk>=2.6 : 1.wp) //Device width
82
- ScreenUtil.screenHeight (sdk>=2.6 : 1.hp) //Device height
83
- ScreenUtil.bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen
84
- ScreenUtil.statusBarHeight //Status bar height , Notch will be higher Unit px
85
- ScreenUtil.textScaleFactor //System font scaling factor
83
+ ScreenUtil() .pixelRatio //Device pixel density
84
+ ScreenUtil() .screenWidth (sdk>=2.6 : 1.wp) //Device width
85
+ ScreenUtil() .screenHeight (sdk>=2.6 : 1.hp) //Device height
86
+ ScreenUtil() .bottomBarHeight //Bottom safe zone distance, suitable for buttons with full screen
87
+ ScreenUtil() .statusBarHeight //Status bar height , Notch will be higher Unit px
88
+ ScreenUtil() .textScaleFactor //System font scaling factor
86
89
87
90
ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px
88
91
ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px
@@ -119,7 +122,7 @@ height:200.h
119
122
```
120
123
** Note**
121
124
122
- Height is also adapted according to setWidth to ensure no deformation (when you want a square)
125
+ The height can also use setWidth to ensure that it is not deformed (when you want a square)
123
126
124
127
setHeight method is mainly adapted in height, you want to control the height and actuality of a screen on the UIUsed when the same is displayed.
125
128
@@ -135,23 +138,28 @@ Container(
135
138
////If you want to display a square:
136
139
Container(
137
140
width: ScreenUtil().setWidth(300),
138
- height: ScreenUtil().setWidth( 300) ,
141
+ height: 300.w ,
139
142
),
140
-
143
+
141
144
```
142
145
143
146
#### Adapter font:
144
147
``` dart
145
148
//Incoming font size,the unit is pixel, fonts will not scale to respect Text Size accessibility settings
146
149
//(AllowallowFontScaling when initializing ScreenUtil)
147
- ScreenUtil().setSp(28)
150
+ ScreenUtil().setSp(28)
151
+ 28.sp
148
152
149
153
//Incoming font size,the unit is pixel,fonts will scale to respect Text Size accessibility settings
150
- //(If somewhere does not follow the global allowFontScaling setting)
154
+ //(If somewhere follow the global allowFontScaling setting)
151
155
ScreenUtil().setSp(24, allowFontScalingSelf: true)
156
+ 28.ssp
152
157
153
- //for example:
158
+ //(If somewhere does not follow the global allowFontScaling setting)
159
+ ScreenUtil().setSp(24, allowFontScalingSelf: false)
160
+ 28.nsp
154
161
162
+ //for example:
155
163
Column(
156
164
crossAxisAlignment: CrossAxisAlignment.start,
157
165
children: <Widget>[
@@ -171,167 +179,11 @@ Column(
171
179
)
172
180
```
173
181
174
- ``` dart
175
- //import
176
- import 'package:flutter/material.dart';
177
- import 'package:flutter_screenutil/flutter_screenutil.dart';
178
-
179
- void main() => runApp(MyApp());
180
-
181
- class MyApp extends StatelessWidget {
182
- @override
183
- Widget build(BuildContext context) {
184
- return MaterialApp(
185
- debugShowCheckedModeBanner: false,
186
- title: 'Flutter_ScreenUtil',
187
- theme: ThemeData(
188
- primarySwatch: Colors.blue,
189
- ),
190
- home: MyHomePage(),
191
- );
192
- }
193
- }
194
-
195
- class MyHomePage extends StatefulWidget {
196
- @override
197
- _MyHomePageState createState() => _MyHomePageState();
198
- }
199
-
200
- class _MyHomePageState extends State<MyHomePage> {
201
- @override
202
- Widget build(BuildContext context) {
203
- //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)
204
- ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
205
-
206
- return ExampleWidget(title: 'FlutterScreenUtil Demo');
207
- }
208
- }
209
-
210
- class ExampleWidget extends StatefulWidget {
211
- const ExampleWidget({Key key, this.title}) : super(key: key);
212
-
213
- final String title;
214
-
215
- @override
216
- _ExampleWidgetState createState() => _ExampleWidgetState();
217
- }
218
-
219
- class _ExampleWidgetState extends State<ExampleWidget> {
220
- @override
221
- Widget build(BuildContext context) {
222
- printScreenInformation();
223
- return Scaffold(
224
- appBar: AppBar(
225
- title: Text(widget.title),
226
- ),
227
- body: SingleChildScrollView(
228
- child: Column(
229
- crossAxisAlignment: CrossAxisAlignment.center,
230
- children: <Widget>[
231
- Row(
232
- children: <Widget>[
233
- Container(
234
- padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
235
- width: ScreenUtil().setWidth(375),
236
- height: ScreenUtil().setHeight(200),
237
- color: Colors.red,
238
- child: Text(
239
- 'My width:${ScreenUtil().setWidth(375)}dp \n'
240
- 'My height:${ScreenUtil().setHeight(200)}dp',
241
- style: TextStyle(
242
- color: Colors.white, fontSize: ScreenUtil().setSp(24)),
243
- ),
244
- ),
245
- Container(
246
- padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
247
- width: ScreenUtil().setWidth(375),
248
- height: ScreenUtil().setHeight(200),
249
- color: Colors.blue,
250
- child: Text(
251
- 'My width:${ScreenUtil().setWidth(375)}dp \n'
252
- 'My height:${ScreenUtil().setHeight(200)}dp',
253
- style: TextStyle(
254
- color: Colors.white,
255
- fontSize: ScreenUtil().setSp(24))),
256
- ),
257
- ],
258
- ),
259
- Text('Device width:${ScreenUtil.screenWidth}dp'),
260
- Text('Device height:${ScreenUtil.screenHeight}dp'),
261
- Text('Device width:${ScreenUtil.screenWidthPx}px'),
262
- Text('Device height:${ScreenUtil.screenHeightPx}px'),
263
- Text('Device pixel density:${ScreenUtil.pixelRatio}'),
264
- Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}dp'),
265
- Text('Status bar height:${ScreenUtil.statusBarHeight}dp'),
266
- Text(
267
- 'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}',
268
- textAlign: TextAlign.center,
269
- ),
270
- Text(
271
- 'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}',
272
- textAlign: TextAlign.center,
273
- ),
274
- SizedBox(
275
- height: ScreenUtil().setHeight(100),
276
- ),
277
- Text('System font scaling factor:${ScreenUtil.textScaleFactor}'),
278
- Column(
279
- crossAxisAlignment: CrossAxisAlignment.start,
280
- children: <Widget>[
281
- Text(
282
- 'My font size is 24px on the design draft and will not change with the system.',
283
- style: TextStyle(
284
- color: Colors.black,
285
- fontSize: ScreenUtil().setSp(24),
286
- )),
287
- Text(
288
- 'My font size is 24px on the design draft and will change with the system.',
289
- style: TextStyle(
290
- color: Colors.black,
291
- fontSize: ScreenUtil()
292
- .setSp(24, allowFontScalingSelf: true))),
293
- ],
294
- )
295
- ],
296
- ),
297
- ),
298
- floatingActionButton: FloatingActionButton(
299
- child: Icon(Icons.title),
300
- onPressed: () {
301
- ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
302
- setState(() {});
303
- },
304
- ),
305
- );
306
- }
307
-
308
- void printScreenInformation() {
309
- print('Device width dp:${ScreenUtil.screenWidth}'); //Device width
310
- print('Device height dp:${ScreenUtil.screenHeight}'); //Device height
311
- print(
312
- 'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density
313
- print(
314
- 'Bottom safe zone distance dp:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
315
- print(
316
- 'Status bar height dp:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit dp
317
- print(
318
- 'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}');
319
- print(
320
- 'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}');
321
- print(
322
- 'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
323
- print(
324
- 'The ratio of height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
325
- }
326
- }
327
-
328
- ```
329
-
330
- ### example:
182
+ ### Example:
331
183
332
184
[ example demo] ( /example/lib/main.dart )
333
185
334
- effect :
186
+ ### Effect :
335
187
336
188
![ effect] ( demo_en.png )
337
189
![ tablet effect] ( demo_tablet_en.png )
0 commit comments