README_CN.md 9.4 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)
readme  
李卓原 已提交
12

readme  
李卓原 已提交
13 14
github: https://github.com/OpenFlutter/flutter_ScreenUtil

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

L
LiZhuoyuan 已提交
17
[Update log](/CHANGELOG.md)
readme  
李卓原 已提交
18 19 20 21 22 23 24 25 26 27 28

## 使用方法:

### 安装依赖:

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

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

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

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

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

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

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

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

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

### 使用:

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

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

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

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

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

**注意**

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

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

例如:
L
LiZhuoyuan 已提交
83 84 85

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

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

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

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

李卓原 已提交
122 123 124

```

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

```
//导入
import 'package:flutter_screenutil/flutter_screenutil.dart';

...

李卓原 已提交
145
  @override
李卓原 已提交
146 147 148
  Widget build(BuildContext context) {
    //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
L
LiZhuoyuan 已提交
149
    
李卓原 已提交
150 151 152 153 154 155 156
    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
李卓原 已提交
157

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

李卓原 已提交
161
    print(
162
        '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}'); 
李卓原 已提交
163
    print(
164
        '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}'); 
李卓原 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177
    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(
178 179
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(200),
李卓原 已提交
180 181
                  color: Colors.red,
                  child: Text(
182
                    '我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
李卓原 已提交
183
                    style: TextStyle(
李卓原 已提交
184
                      color: Colors.white,
185
                      fontSize: ScreenUtil.getInstance().setSp(12),
李卓原 已提交
186
                    ),
李卓原 已提交
187 188 189
                  ),
                ),
                Container(
190 191
                  width: ScreenUtil.getInstance().setWidth(375),
                  height: ScreenUtil.getInstance().setHeight(200),
李卓原 已提交
192
                  color: Colors.blue,
193
                  child: Text('我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
李卓原 已提交
194
                      style: TextStyle(
李卓原 已提交
195
                        color: Colors.white,
196
                        fontSize: ScreenUtil.getInstance().setSp(12),
李卓原 已提交
197
                      )),
李卓原 已提交
198 199 200
                ),
              ],
            ),
李卓原 已提交
201
            Text('设备宽度:${ScreenUtil.screenWidth}px'),
李卓原 已提交
202 203 204 205 206
            Text('设备高度:${ScreenUtil.screenHeight}px'),
            Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
            Text('底部安全区距离:${ScreenUtil.bottomBarHeight}px'),
            Text('状态栏高度:${ScreenUtil.statusBarHeight}px'),
            Text(
207
              '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
李卓原 已提交
208 209 210
              textAlign: TextAlign.center,
            ),
            Text(
211
              '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
李卓原 已提交
212 213 214
              textAlign: TextAlign.center,
            ),
            Text(
215
              '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}',
李卓原 已提交
216 217 218
              textAlign: TextAlign.center,
            ),
            Text(
219
              '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}',
李卓原 已提交
220 221 222
              textAlign: TextAlign.center,
            ),
            SizedBox(
223
              height: ScreenUtil.getInstance().setHeight(100),
李卓原 已提交
224 225 226
            ),
            Text('系统的字体缩放比例:${ScreenUtil.textScaleFactory}'),
            Column(
0.4.5  
李卓原 已提交
227 228 229 230
                         crossAxisAlignment: CrossAxisAlignment.start,
                         children: <Widget>[
                           Text('我的文字大小在设计稿上是25px,不会随着系统的文字缩放比例变化',
                               style: TextStyle(
231
                                   color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(24))),
0.4.5  
李卓原 已提交
232 233 234 235 236
                           Text('我的文字大小在设计稿上是25px,会随着系统的文字缩放比例变化',
                               style: TextStyle(
                                   color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
                         ],
                       )
李卓原 已提交
237 238 239 240 241
          ],
        ),
      ),
    );
  }
readme  
李卓原 已提交
242 243 244 245
```

### 使用示例:

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

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