README_CN.md 8.5 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
li_zy 已提交
9
[README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md)
readme  
李卓原 已提交
10

readme  
李卓原 已提交
11 12
github: https://github.com/OpenFlutter/flutter_ScreenUtil

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

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

## 使用方法:

### 安装依赖:

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

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

李卓原 已提交
35
### 属性
L
LiZhuoyuan 已提交
36

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

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

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

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

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

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

### 使用:

L
LiZhuoyuan 已提交
63 64 65
#### 适配尺寸: 

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

L
LiZhuoyuan 已提交
67
根据屏幕宽度适配 width: ScreenUtil().setWidth(540),
L
LiZhuoyuan 已提交
68

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

**注意**

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

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

例如:
L
LiZhuoyuan 已提交
78 79 80

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

L
LiZhuoyuan 已提交
93
#### 适配字体:
L
LiZhuoyuan 已提交
94 95
传入设计稿的px尺寸:

李卓原 已提交
96
``` 
0.4.5  
李卓原 已提交
97 98 99
      ScreenUtil().setSp(28)         //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放
      ScreenUtil(allowFontScaling: true).setSp(28)         //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放
     
李卓原 已提交
100 101 102 103 104 105 106 107 108 109 110
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 已提交
111
#### 其他相关api:
readme  
李卓原 已提交
112 113
```
    ScreenUtil.pixelRatio       //设备的像素密度
李卓原 已提交
114 115 116 117 118 119
    ScreenUtil.screenWidth      //设备宽度
    ScreenUtil.screenHeight     //设备高度
    ScreenUtil.bottomBarHeight  //底部安全区距离,适用于全面屏下面有按键的
    ScreenUtil.statusBarHeight  //状态栏高度 刘海屏会更高  单位px
    ScreenUtil.textScaleFactory //系统字体缩放比例
    
李卓原 已提交
120 121
    ScreenUtil().scaleWidth  // 实际宽度的dp与设计稿px的比例
    ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例
李卓原 已提交
122
    
readme  
李卓原 已提交
123 124 125 126 127 128 129 130
```

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

...

李卓原 已提交
131
  @override
李卓原 已提交
132 133 134 135 136 137 138 139 140 141
  Widget build(BuildContext context) {
    //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    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
李卓原 已提交
142 143 144 145

    print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
    print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');

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

### 使用示例:

李卓原 已提交
231
[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart)
readme  
李卓原 已提交
232 233 234
 
效果:

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