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

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

L
li_zy 已提交
5
[README of English](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README.md)
readme  
李卓原 已提交
6

readme  
李卓原 已提交
7 8
github: https://github.com/OpenFlutter/flutter_ScreenUtil

readme  
李卓原 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21
csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477


## 使用方法:

### 安装依赖:

安装之前请查看最新版本
```
dependencies:
  flutter:
    sdk: flutter
  # 添加依赖
李卓原 已提交
22
  flutter_screenutil: ^0.4.0
readme  
李卓原 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
```

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

### 初始化设置尺寸
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px)
一定在MaterialApp的home中的页面设置,以保证在每次使用之前设置好了适配尺寸:

```
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
```

### 使用:

适配尺寸:
```
//传入设计稿的px尺寸:
适配后的宽度width: ScreenUtil().setWidth(540),
适配后的高度height: ScreenUtil().setHeight(200),
李卓原 已提交
46
高度也根据setWidth来做适配可以保证不变形
readme  
李卓原 已提交
47 48 49 50 51 52 53 54

例如:
Container(
           width: ScreenUtil().setWidth(375),
           height: ScreenUtil().setHeight(200),
            ),
```

李卓原 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
适配字体:
``` 
      ScreenUtil().setSp(28)         //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放
      ScreenUtil().setSp(28,false)  //传入字体大小,不会根据系统的“字体大小”辅助选项来进行缩放

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) 
                 )
             ),

```

readme  
李卓原 已提交
71 72 73
其他相关api:
```
    ScreenUtil.pixelRatio       //设备的像素密度
李卓原 已提交
74 75 76 77 78 79
    ScreenUtil.screenWidth      //设备宽度
    ScreenUtil.screenHeight     //设备高度
    ScreenUtil.bottomBarHeight  //底部安全区距离,适用于全面屏下面有按键的
    ScreenUtil.statusBarHeight  //状态栏高度 刘海屏会更高  单位px
    ScreenUtil.textScaleFactory //系统字体缩放比例
    
李卓原 已提交
80 81
    ScreenUtil().scaleWidth  // 实际宽度的dp与设计稿px的比例
    ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例
李卓原 已提交
82
    
readme  
李卓原 已提交
83 84 85 86 87 88 89 90
```

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

...

李卓原 已提交
91
  @override
李卓原 已提交
92 93 94 95 96 97 98 99 100 101 102
  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
    print(
李卓原 已提交
103
        '实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}'); //The width is enlarged relative to the design draft
李卓原 已提交
104
    print(
李卓原 已提交
105
        '实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}'); //The height is enlarged relative to the design draft
李卓原 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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(
                        color: Colors.white,
                        fontSize: ScreenUtil().setSp(28, false)),
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
                  child: Text('我的宽度:${ScreenUtil().setWidth(375)}dp',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenUtil().setSp(28, false))),
                ),
              ],
            ),
李卓原 已提交
140
            Text('设备宽度:${ScreenUtil.screenWidth}px'),
李卓原 已提交
141 142 143 144 145
            Text('设备高度:${ScreenUtil.screenHeight}px'),
            Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
            Text('底部安全区距离:${ScreenUtil.bottomBarHeight}px'),
            Text('状态栏高度:${ScreenUtil.statusBarHeight}px'),
            Text(
李卓原 已提交
146
              '实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}',
李卓原 已提交
147 148 149
              textAlign: TextAlign.center,
            ),
            Text(
李卓原 已提交
150
              '实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}',
李卓原 已提交
151 152 153 154 155 156 157 158 159
              textAlign: TextAlign.center,
            ),
            SizedBox(
              height: ScreenUtil().setHeight(200),
            ),
            Text('系统的字体缩放比例:${ScreenUtil.textScaleFactory}'),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
李卓原 已提交
160
                Text('我的文字大小是14px,不会随着系统的文字大小变化',
李卓原 已提交
161 162
                    style: TextStyle(
                        color: Colors.black,
李卓原 已提交
163 164
                        fontSize: ScreenUtil().setSp(14, false))),
                Text('我的文字大小是14px,会随着系统的文字大小变化',
李卓原 已提交
165
                    style: TextStyle(
李卓原 已提交
166
                        color: Colors.black, fontSize: ScreenUtil().setSp(14))),
李卓原 已提交
167 168 169 170 171 172 173
              ],
            )
          ],
        ),
      ),
    );
  }
readme  
李卓原 已提交
174 175 176 177
```

### 使用示例:

李卓原 已提交
178
[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart)
readme  
李卓原 已提交
179 180 181
 
效果:

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