README_CN.md 10.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
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
  # 添加依赖
L
1.0.1  
lizhuoyuan 已提交
30
  flutter_screenutil: ^1.0.1
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
L
1.0.0  
lizhuoyuan 已提交
54
ScreenUtil.init(context);
李卓原 已提交
55

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

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

### 使用:

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

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

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

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

**注意**

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

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

例如:
L
LiZhuoyuan 已提交
81 82

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

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

李卓原 已提交
99
``` 
李卓原 已提交
100
//传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
L
1.0.0  
lizhuoyuan 已提交
101
ScreenUtil().setSp(28)         
李卓原 已提交
102
 
L
1.0.0  
lizhuoyuan 已提交
103 104 105
//传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置)       
ScreenUtil().setSp(24, allowFontScalingSelf: true)     

李卓原 已提交
106 107 108 109 110
//for example:

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

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

L
lizhuoyuan 已提交
139
```dart
L
1.0.0  
lizhuoyuan 已提交
140
import 'package:flutter/material.dart';
readme  
李卓原 已提交
141 142
import 'package:flutter_screenutil/flutter_screenutil.dart';

L
1.0.0  
lizhuoyuan 已提交
143 144 145
void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
李卓原 已提交
146
  @override
李卓原 已提交
147
  Widget build(BuildContext context) {
L
1.0.0  
lizhuoyuan 已提交
148 149 150 151 152 153 154 155 156 157
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter_ScreenUtil',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}
李卓原 已提交
158

L
1.0.0  
lizhuoyuan 已提交
159 160
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
李卓原 已提交
161

L
1.0.0  
lizhuoyuan 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    ScreenUtil.init(context);
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return ExampleWidget(title: 'FlutterScreenUtil示例');
  }
}

class ExampleWidget extends StatefulWidget {
  const ExampleWidget({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _ExampleWidgetState createState() => _ExampleWidgetState();
}
李卓原 已提交
188

L
1.0.0  
lizhuoyuan 已提交
189 190 191 192
class _ExampleWidgetState extends State<ExampleWidget> {
  @override
  Widget build(BuildContext context) {
    printScreenInformation();
李卓原 已提交
193 194 195 196 197 198 199 200 201 202 203
    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
1.0.0  
lizhuoyuan 已提交
204 205 206
                  padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
李卓原 已提交
207 208
                  color: Colors.red,
                  child: Text(
L
1.0.0  
lizhuoyuan 已提交
209 210
                    '我的宽度:${ScreenUtil().setWidth(375)}dp \n'
                    '我的高度:${ScreenUtil().setHeight(200)}dp',
李卓原 已提交
211
                    style: TextStyle(
L
1.0.0  
lizhuoyuan 已提交
212
                        color: Colors.white, fontSize: ScreenUtil().setSp(24)),
李卓原 已提交
213 214 215
                  ),
                ),
                Container(
L
1.0.0  
lizhuoyuan 已提交
216 217 218
                  padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
李卓原 已提交
219
                  color: Colors.blue,
L
1.0.0  
lizhuoyuan 已提交
220 221 222
                  child: Text(
                      '我的宽度:${ScreenUtil().setWidth(375)}dp \n'
                      '我的高度:${ScreenUtil().setHeight(200)}dp',
李卓原 已提交
223
                      style: TextStyle(
L
1.0.0  
lizhuoyuan 已提交
224 225
                          color: Colors.white,
                          fontSize: ScreenUtil().setSp(24))),
李卓原 已提交
226 227 228
                ),
              ],
            ),
李卓原 已提交
229
            Text('设备宽度:${ScreenUtil.screenWidth}px'),
李卓原 已提交
230
            Text('设备高度:${ScreenUtil.screenHeight}px'),
L
1.0.0  
lizhuoyuan 已提交
231 232
            Text('设备宽度:${ScreenUtil.screenWidthDp}dp'),
            Text('设备高度:${ScreenUtil.screenHeightDp}dp'),
李卓原 已提交
233
            Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
L
1.0.0  
lizhuoyuan 已提交
234 235
            Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'),
            Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'),
李卓原 已提交
236
            Text(
L
1.0.0  
lizhuoyuan 已提交
237
              '实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}',
李卓原 已提交
238 239 240
              textAlign: TextAlign.center,
            ),
            Text(
L
1.0.0  
lizhuoyuan 已提交
241
              '实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}',
李卓原 已提交
242 243 244
              textAlign: TextAlign.center,
            ),
            SizedBox(
L
1.0.0  
lizhuoyuan 已提交
245
              height: ScreenUtil().setHeight(100),
李卓原 已提交
246
            ),
L
lizhuoyuan 已提交
247
            Text('系统的字体缩放比例:${ScreenUtil.textScaleFactor}'),
李卓原 已提交
248
            Column(
L
1.0.0  
lizhuoyuan 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text('我的文字大小在设计稿上是24px,不会随着系统的文字缩放比例变化',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: ScreenUtil().setSp(24),
                    )),
                Text('我的文字大小在设计稿上是24px,会随着系统的文字缩放比例变化',
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: ScreenUtil()
                            .setSp(24, allowFontScalingSelf: true))),
              ],
            )
李卓原 已提交
263 264 265
          ],
        ),
      ),
L
1.0.0  
lizhuoyuan 已提交
266 267 268 269 270 271 272 273
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.title),
        onPressed: () {
          ScreenUtil.init(context,
              width: 1500, height: 1334, allowFontScaling: false);
          setState(() {});
        },
      ),
李卓原 已提交
274 275
    );
  }
L
1.0.0  
lizhuoyuan 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

  void printScreenInformation() {
    print('设备宽度:${ScreenUtil.screenWidth}'); //Device width
    print('设备高度:${ScreenUtil.screenHeight}'); //Device height
    print('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
    print(
        '底部安全区距离:${ScreenUtil.bottomBarHeight}dp'); //Bottom safe zone distance,suitable for buttons with full screen
    print(
        '状态栏高度:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px

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

    print(
        '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
    print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
    print('系统的字体缩放比例:${ScreenUtil.textScaleFactor}');
  }
}

readme  
李卓原 已提交
296 297 298 299
```

### 使用示例:

李卓原 已提交
300
[example demo](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/example/lib/main_zh.dart)
readme  
李卓原 已提交
301 302 303
 
效果:

L
1.0.0  
lizhuoyuan 已提交
304 305
![手机效果](demo_zh.png)
![平板效果](demo_tablet_zh.png)