README_CN.md 9.6 KB
Newer Older
readme  
李卓原 已提交
1 2

# flutter_ScreenUtil
L
LiZhuoyuan 已提交
3 4 5 6

[![pub package](https://img.shields.io/pub/v/flutter_screenutil.svg)](https://pub.dartlang.org/packages/flutter_screenutil)


李卓原 已提交
7
**flutter 屏幕适配方案,让你的UI在不同尺寸的屏幕上都能显示合理的布局!**
readme  
李卓原 已提交
8

L
LiZhuoyuan 已提交
9 10
*注意*:此插件仍处于开发阶段,某些API可能尚未推出。

L
li_zy 已提交
11
[README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md)
T
Thalles Santos 已提交
12
[README em Português](https://github.com/OpenFlutter/flutter_screenutil/blob/master/README_PT.md)
readme  
李卓原 已提交
13

0.5.2  
李卓原 已提交
14
github: https://github.com/OpenFlutter/flutter_screenutil
readme  
李卓原 已提交
15

readme  
李卓原 已提交
16 17
csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477

L
LiZhuoyuan 已提交
18
[更新日志](/CHANGELOG.md)
readme  
李卓原 已提交
19 20 21 22 23 24 25 26 27 28 29

## 使用方法:

### 安装依赖:

安装之前请查看最新版本
```
dependencies:
  flutter:
    sdk: flutter
  # 添加依赖
李卓原 已提交
30
  flutter_screenutil: ^0.5.3
readme  
李卓原 已提交
31 32 33 34 35 36 37
```

### 在每个使用的地方导入包:
```
import 'package:flutter_screenutil/flutter_screenutil.dart';
```

李卓原 已提交
38
### 属性
L
LiZhuoyuan 已提交
39

0.4.5  
李卓原 已提交
40 41
|属性|类型|默认值|描述|
|:---|:---|:---|:---| 
0.5.2  
李卓原 已提交
42 43
|width|double|1080px|设计稿中设备的宽度,单位px|
|height|double|1920px|设计稿中设备的高度,单位px|
0.4.5  
李卓原 已提交
44 45
|allowFontScaling|bool|false|设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放|

李卓原 已提交
46 47 48 49
### 初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px)
一定在MaterialApp的home中的页面设置(即入口文件,只需设置一次),以保证在每次使用之前设置好了适配尺寸:

L
LiZhuoyuan 已提交
50
```
李卓原 已提交
51 52
//填入设计稿中设备的屏幕尺寸

李卓原 已提交
53
//默认 width : 1080px , height:1920px , allowFontScaling:false
54
ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
李卓原 已提交
55

李卓原 已提交
56
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) 
李卓原 已提交
57 58
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);

L
LiZhuoyuan 已提交
59
//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
李卓原 已提交
60
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334, allowFontScaling: true)..init(context);
L
LiZhuoyuan 已提交
61
    
readme  
李卓原 已提交
62 63 64 65
```

### 使用:

L
LiZhuoyuan 已提交
66 67 68
#### 适配尺寸: 

传入设计稿的px尺寸:
L
LiZhuoyuan 已提交
69

70
根据屏幕宽度适配 `width: ScreenUtil.getInstance().setWidth(540)`,
L
LiZhuoyuan 已提交
71

72
根据屏幕高度适配 `height: ScreenUtil.getInstance().setHeight(200)`,
L
LiZhuoyuan 已提交
73

李卓原 已提交
74 75
也可以使用 `ScreenUtil()` 替代 `ScreenUtil.getInstance()`,
例如:`ScreenUtil().setHeight(200)`
L
LiZhuoyuan 已提交
76 77 78 79

**注意**

高度也根据setWidth来做适配可以保证不变形(当你想要一个正方形的时候)
L
LiZhuoyuan 已提交
80

L
LiZhuoyuan 已提交
81
setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的高度与实际中显示一样时使用.
readme  
李卓原 已提交
82 83

例如:
L
LiZhuoyuan 已提交
84 85 86

```
//长方形:
readme  
李卓原 已提交
87
Container(
88 89
           width: ScreenUtil.getInstance().setWidth(375),
           height: ScreenUtil.getInstance().setHeight(200),
readme  
李卓原 已提交
90
            ),
L
LiZhuoyuan 已提交
91
            
L
LiZhuoyuan 已提交
92
//如果你想显示一个正方形:
L
LiZhuoyuan 已提交
93
Container(
94 95
           width: ScreenUtil.getInstance().setWidth(300),
           height: ScreenUtil.getInstance().setWidth(300),
L
LiZhuoyuan 已提交
96
            ),
readme  
李卓原 已提交
97 98
```

L
LiZhuoyuan 已提交
99
#### 适配字体:
L
LiZhuoyuan 已提交
100 101
传入设计稿的px尺寸:

李卓原 已提交
102
``` 
李卓原 已提交
103
//传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
104
ScreenUtil.getInstance().setSp(28)         
李卓原 已提交
105 106 107
 
//传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置)     
ScreenUtil(allowFontScaling: true).setSp(28)        
0.4.5  
李卓原 已提交
108
     
李卓原 已提交
109 110 111 112 113 114 115
//for example:

Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text('我的文字大小在设计稿上是25px,不会随着系统的文字缩放比例变化',
                    style: TextStyle(
116
                        color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(24))),
李卓原 已提交
117 118 119 120 121 122
                Text('我的文字大小在设计稿上是25px,会随着系统的文字缩放比例变化',
                    style: TextStyle(
                        color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
              ],
            )

李卓原 已提交
123 124 125

```

L
LiZhuoyuan 已提交
126
#### 其他相关api:
readme  
李卓原 已提交
127 128
```
    ScreenUtil.pixelRatio       //设备的像素密度
李卓原 已提交
129 130 131 132 133 134
    ScreenUtil.screenWidth      //设备宽度
    ScreenUtil.screenHeight     //设备高度
    ScreenUtil.bottomBarHeight  //底部安全区距离,适用于全面屏下面有按键的
    ScreenUtil.statusBarHeight  //状态栏高度 刘海屏会更高  单位px
    ScreenUtil.textScaleFactory //系统字体缩放比例
    
135 136
    ScreenUtil.getInstance().scaleWidth  // 实际宽度的dp与设计稿px的比例
    ScreenUtil.getInstance().scaleHeight // 实际高度的dp与设计稿px的比例
李卓原 已提交
137
    
readme  
李卓原 已提交
138 139
```

L
lizhuoyuan 已提交
140
```dart
readme  
李卓原 已提交
141 142 143 144
//导入
import 'package:flutter_screenutil/flutter_screenutil.dart';

...
L
lizhuoyuan 已提交
145
 
李卓原 已提交
146
  @override
李卓原 已提交
147 148 149
  Widget build(BuildContext context) {
    //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
L
LiZhuoyuan 已提交
150
    
李卓原 已提交
151 152 153 154 155 156 157
    print('设备宽度:${ScreenUtil.screenWidth}'); //Device width
    print('设备高度:${ScreenUtil.screenHeight}'); //Device height
    print('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
    print(
        '底部安全区距离:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
    print(
        '状态栏高度:${ScreenUtil.statusBarHeight}px'); //Status bar height , Notch will be higher Unit px
李卓原 已提交
158

159 160
    print('实际宽度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleWidth}');
    print('实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}');
李卓原 已提交
161

李卓原 已提交
162
    print(
163
        '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}'); 
李卓原 已提交
164
    print(
165
        '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}'); 
李卓原 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
    print('系统的字体缩放比例:${ScreenUtil.textScaleFactory}');

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
L
lizhuoyuan 已提交
179
                  padding: EdgeInsets.all(ScreenUtil.getInstance().setWidth(10)),
180 181
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(200),
李卓原 已提交
182 183
                  color: Colors.red,
                  child: Text(
184
                    '我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
李卓原 已提交
185
                    style: TextStyle(
李卓原 已提交
186
                      color: Colors.white,
187
                      fontSize: ScreenUtil.getInstance().setSp(12),
李卓原 已提交
188
                    ),
李卓原 已提交
189 190 191
                  ),
                ),
                Container(
L
lizhuoyuan 已提交
192
                  padding: EdgeInsets.all(ScreenUtil.getInstance().setWidth(10)),
193 194
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(200),
李卓原 已提交
195
                  color: Colors.blue,
196
                  child: Text('我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
李卓原 已提交
197
                      style: TextStyle(
李卓原 已提交
198
                        color: Colors.white,
199
                        fontSize: ScreenUtil.getInstance().setSp(12),
李卓原 已提交
200
                      )),
李卓原 已提交
201 202 203
                ),
              ],
            ),
李卓原 已提交
204
            Text('设备宽度:${ScreenUtil.screenWidth}px'),
李卓原 已提交
205 206 207 208 209
            Text('设备高度:${ScreenUtil.screenHeight}px'),
            Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
            Text('底部安全区距离:${ScreenUtil.bottomBarHeight}px'),
            Text('状态栏高度:${ScreenUtil.statusBarHeight}px'),
            Text(
210
              '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
李卓原 已提交
211 212 213
              textAlign: TextAlign.center,
            ),
            Text(
214
              '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
李卓原 已提交
215 216 217
              textAlign: TextAlign.center,
            ),
            Text(
218
              '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}',
李卓原 已提交
219 220 221
              textAlign: TextAlign.center,
            ),
            Text(
222
              '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}',
李卓原 已提交
223 224 225
              textAlign: TextAlign.center,
            ),
            SizedBox(
226
              height: ScreenUtil.getInstance().setHeight(100),
李卓原 已提交
227 228 229
            ),
            Text('系统的字体缩放比例:${ScreenUtil.textScaleFactory}'),
            Column(
0.4.5  
李卓原 已提交
230 231
                         crossAxisAlignment: CrossAxisAlignment.start,
                         children: <Widget>[
T
Thalles Santos 已提交
232
                           Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化',
0.4.5  
李卓原 已提交
233
                               style: TextStyle(
234
                                   color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(24))),
T
Thalles Santos 已提交
235
                           Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化',
0.4.5  
李卓原 已提交
236 237 238 239
                               style: TextStyle(
                                   color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
                         ],
                       )
李卓原 已提交
240 241 242 243 244
          ],
        ),
      ),
    );
  }
readme  
李卓原 已提交
245 246 247 248
```

### 使用示例:

李卓原 已提交
249
[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart)
readme  
李卓原 已提交
250 251 252
 
效果:

李卓原 已提交
253
![效果](demo_zh.png)
readme  
李卓原 已提交
254