README.md 8.4 KB
Newer Older
1

L
LiZhuoyuan 已提交
2
# flutter_screenutil
L
LiZhuoyuan 已提交
3
[![pub package](https://img.shields.io/pub/v/flutter_screenutil.svg)](https://pub.dartlang.org/packages/flutter_screenutil)
L
LiZhuoyuan 已提交
4

李卓原 已提交
5
**A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes!**
6

L
LiZhuoyuan 已提交
7
[中文文档](https://github.com/OpenFlutter/flutter_screenutil/blob/master/README_CN.md)
李卓原 已提交
8

L
LiZhuoyuan 已提交
9
github: https://github.com/OpenFlutter/flutter_screenutil
10

L
LiZhuoyuan 已提交
11
[Update log](/CHANGELOG.md)
12

readme  
李卓原 已提交
13
## Usage:
14

readme  
李卓原 已提交
15 16
### Add dependency:
Please check the latest version before installation.
17 18 19 20
```
dependencies:
  flutter:
    sdk: flutter
readme  
李卓原 已提交
21
  # add flutter_ScreenUtil
卓原 已提交
22
  flutter_screenutil: ^0.4.2
23 24
```

readme  
李卓原 已提交
25
### Add the following imports to your Dart code:
26 27 28 29
```
import 'package:flutter_screenutil/flutter_screenutil.dart';
```

李卓原 已提交
30 31 32 33 34 35 36 37 38
### 属性
   
|Property|Type|Default Value|Description|
|:---|:---|:---|:---| 
|width|int|1080px|The width of the device in the design draft, in px|
|height|int|1920px|The height of the device in the design draft, in px|
|allowFontScaling|bool|false|Sets whether the font size is scaled according to the system's "font size" assist option|

### Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option
readme  
李卓原 已提交
39
Please set the width and height of the design draft before use, the width and height of the design draft (unit px).
李卓原 已提交
40
Be sure to set the page in the MaterialApp's home(ie the entry file, just set it once) to ensure that the fit size is set before each use:
41 42

```
李卓原 已提交
43 44 45

//fill in the screen size of the device in the design

李卓原 已提交
46 47 48
//default value : width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.instance = ScreenUtil()..init(context);

readme  
李卓原 已提交
49 50
//If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
李卓原 已提交
51 52 53 54

//If you wang to set the font size is scaled according to the system's "font size" assist option
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334, allowFontScaling: true)..init(context);

55 56
```

readme  
李卓原 已提交
57
### Use:
58

L
LiZhuoyuan 已提交
59 60
#### Adapt screen size:

readme  
李卓原 已提交
61 62
Pass the px size of the design draft:

L
LiZhuoyuan 已提交
63 64 65 66 67 68 69
Adapted to screen width: ScreenUtil().setWidth(540),

Adapted to screen height: ScreenUtil().setHeight(200),

**Note** 

Height is also adapted according to setWidth to ensure no deformation (when you want a square) 
70

L
LiZhuoyuan 已提交
71 72 73
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.

```
readme  
李卓原 已提交
74
//for example:
L
LiZhuoyuan 已提交
75
//rectangle
76 77 78
Container(
           width: ScreenUtil().setWidth(375),
           height: ScreenUtil().setHeight(200),
readme  
李卓原 已提交
79
           ...
80
            ),
L
LiZhuoyuan 已提交
81 82 83 84 85 86 87
            
////If you want to display a square:
Container(
           width: ScreenUtil().setWidth(300),
           height: ScreenUtil().setWidth(300),
            ),
            
88 89
```

L
LiZhuoyuan 已提交
90
#### Adapter font:
李卓原 已提交
91
``` 
92 93
      ScreenUtil().setSp(28)         //Incoming font size,the unit is pixel, fonts will not scale to respect Text Size accessibility settings
      ScreenUtil().setSp(28,true)  //Incoming font size,the unit is pixel,fonts will scale to respect Text Size accessibility settings
李卓原 已提交
94 95 96 97 98 99 100 101 102 103 104 105

for example:
        Text(
             'My font size is 28px and will not change with the system.',
                 style: TextStyle(
                   color: Colors.black,
                   fontSize: ScreenUtil().setSp(28, false) 
                 )
             ),

```

L
LiZhuoyuan 已提交
106
#### Other related apis:
107
```
readme  
李卓原 已提交
108 109 110 111 112
    ScreenUtil.pixelRatio       //Device pixel density
    ScreenUtil.screenWidth      //Device width
    ScreenUtil.screenHeight     //Device height
    ScreenUtil.bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen
    ScreenUtil.statusBarHeight  //Status bar height , Notch will be higher Unit px
李卓原 已提交
113
    ScreenUtil.textScaleFactory //System font scaling factor
114

李卓原 已提交
115 116
    ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px
    ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px
117 118 119 120

```

```
readme  
李卓原 已提交
121
//import
122 123 124 125
import 'package:flutter_screenutil/flutter_screenutil.dart';

...

李卓原 已提交
126
  @override
127
  Widget build(BuildContext context) {
李卓原 已提交
128
    ///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)
129
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
readme  
李卓原 已提交
130 131
    print('Device width:${ScreenUtil.screenWidth}'); //Device width
    print('Device height:${ScreenUtil.screenHeight}'); //Device height
李卓原 已提交
132 133 134 135 136 137 138
    print(
        'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density
    print(
        'Bottom safe zone distance:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
    print(
        'Status bar height:${ScreenUtil.statusBarHeight}px'); //Status bar height , Notch will be higher Unit px
    print(
李卓原 已提交
139
        'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); 
李卓原 已提交
140
    print(
李卓原 已提交
141 142 143 144 145
        'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}'); 
    print(
        'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
    print(
        'The ratio of  height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
146 147 148 149 150 151
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Column(
李卓原 已提交
152
          crossAxisAlignment: CrossAxisAlignment.center,
153 154 155 156 157 158 159 160
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.red,
                  child: Text(
readme  
李卓原 已提交
161
                    'My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
162 163
                    style: TextStyle(
                        color: Colors.white,
李卓原 已提交
164
                        fontSize: ScreenUtil().setSp(12, false)),
165 166 167 168 169 170
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
readme  
李卓原 已提交
171
                  child: Text('My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
172 173
                      style: TextStyle(
                          color: Colors.white,
李卓原 已提交
174
                          fontSize: ScreenUtil().setSp(12, false))),
175 176 177
                ),
              ],
            ),
李卓原 已提交
178
            Text('Device width:${ScreenUtil.screenWidth}px'),
readme  
李卓原 已提交
179 180 181 182
            Text('Device height:${ScreenUtil.screenHeight}px'),
            Text('Device pixel density:${ScreenUtil.pixelRatio}'),
            Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}px'),
            Text('Status bar height:${ScreenUtil.statusBarHeight}px'),
李卓原 已提交
183 184 185 186 187 188 189 190
            Text(
              'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}',
              textAlign: TextAlign.center,
            ),
            Text(
              'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}',
              textAlign: TextAlign.center,
            ),
李卓原 已提交
191
            Text(
李卓原 已提交
192
              'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}',
李卓原 已提交
193 194 195
              textAlign: TextAlign.center,
            ),
            Text(
李卓原 已提交
196
              'The ratio of  height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}',
李卓原 已提交
197 198 199
              textAlign: TextAlign.center,
            ),
            SizedBox(
李卓原 已提交
200
              height: ScreenUtil().setHeight(100),
李卓原 已提交
201
            ),
李卓原 已提交
202
            Text('System font scaling factor:${ScreenUtil.textScaleFactory}'),
李卓原 已提交
203 204 205 206
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
李卓原 已提交
207
                    'My font size is 14px on the design draft and will not change with the system.',
李卓原 已提交
208
                    style: TextStyle(
李卓原 已提交
209 210 211 212 213
                      color: Colors.black,
                      fontSize: ScreenUtil().setSp(14, false),
                    )),
                Text(
                    'My font size is 14px on the design draft and will change with the system.',
李卓原 已提交
214
                    style: TextStyle(
李卓原 已提交
215 216 217
                      color: Colors.black,
                      fontSize: ScreenUtil().setSp(14),
                    )),
李卓原 已提交
218 219
              ],
            )
220 221 222 223 224 225 226
          ],
        ),
      ),
    );
  }
```

readme  
李卓原 已提交
227
### example:
228

李卓原 已提交
229
[example demo](/example/lib/main_zh.dart)
230
 
readme  
李卓原 已提交
231
effect:
232

李卓原 已提交
233
![效果](demo_en.png)
234