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)
L
LiZhuoyuan 已提交
12

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

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

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

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

## 使用方法:

### 安装依赖:

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

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

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

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

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

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

李卓原 已提交
54
//默认 width : 1080px , height:1920px , allowFontScaling:false
L
1.0.0  
lizhuoyuan 已提交
55
ScreenUtil.init(context);
李卓原 已提交
56

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

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

### 使用:

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

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

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

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

**注意**

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

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

例如:
L
LiZhuoyuan 已提交
82 83

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

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

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

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

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

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

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

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

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

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

L
1.0.0  
lizhuoyuan 已提交
163 164 165
  final String title;

  @override
L
lizhuoyuan 已提交
166
  _MyHomePageState createState() => _MyHomePageState();
L
1.0.0  
lizhuoyuan 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

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();
}
李卓原 已提交
189

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

  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  
李卓原 已提交
297 298 299 300
```

### 使用示例:

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

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