README.md 3.4 KB
Newer Older
L
li_zy 已提交
1 2

# flutter_ScreenUtil
L
li_zy 已提交
3
**flutter 屏幕适配方案**
L
li_zy 已提交
4

L
li_zy 已提交
5
github: https://github.com/lizhuoyuan/flutter_ScreenUtil </br>
L
li_zy 已提交
6
csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477
L
li_zy 已提交
7 8


L
li_zy 已提交
9
## 使用方法:
L
li_zy 已提交
10

L
li_zy 已提交
11
### 安装依赖:
L
li_zy 已提交
12
```
L
li_zy 已提交
13 14 15 16 17 18 19 20 21
dependencies:
  flutter:
    sdk: flutter
  # 添加依赖
  flutter_screenutil:
    git:
      url: git://github.com/lizhuoyuan/flutter_ScreenUtil
```

L
li_zy 已提交
22
### 在每个使用的地方导入包:
L
li_zy 已提交
23 24 25
```
import 'package:flutter_screenutil/flutter_screenutil.dart';

L
li_zy 已提交
26
```
L
li_zy 已提交
27

L
li_zy 已提交
28 29 30
### 初始化设置尺寸
在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px)
如果不设置则使用默认尺寸,默认为1080*1920
L
li_zy 已提交
31
推荐在项目入口中设置,以保证在每次使用之前设置好了适配尺寸:
L
li_zy 已提交
32

L
li_zy 已提交
33
```
L
li_zy 已提交
34
ScreenUtil.instance = new ScreenUtil(width: 360, height: 720);
L
li_zy 已提交
35
```
L
li_zy 已提交
36

L
li_zy 已提交
37
### 使用:
L
li_zy 已提交
38

L
li_zy 已提交
39
适配尺寸:
L
li_zy 已提交
40 41
```
//传入设计稿的px尺寸:
L
li_zy 已提交
42 43
width: ScreenUtil().setWidth(540),
height: ScreenUtil().setHeight(200),
L
li_zy 已提交
44
```
L
li_zy 已提交
45 46

其他相关api:
L
li_zy 已提交
47
```
李卓原 已提交
48 49 50 51
    ScreenUtil.pixelRatio       //设备的像素密度
    ScreenUtil.screenWidth    //设备宽度
    ScreenUtil.screenHeight    //设备高度
    ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
L
li_zy 已提交
52
    ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高  单位px
L
li_zy 已提交
53

李卓原 已提交
54 55
    ScreenUtil().scaleWidth //宽度相对于设计稿放大的倍数
    ScreenUtil().scaleHeight //高度相对于设计稿放大的倍数
L
li_zy 已提交
56 57 58 59 60 61 62 63 64 65

```

```
import 'package:flutter_app/ScreenUtil.dart';  //导入

@override
  Widget build(BuildContext context) { 
   
 print(ScreenUtil().setWidth(180));
L
li_zy 已提交
66 67 68 69 70 71 72 73
    print('设备的像素密度:${ScreenUtil.pixelRatio}'); //设备的像素密度
    print('设备宽度:${ScreenUtil.screenWidth}'); //设备宽度
    print('设备高度:${ScreenUtil.screenHeight}'); //设备高度
    print('底部安全区距离:${ScreenUtil.bottomBarHeight}'); //底部安全区距离,适用于全面屏下面有按键的
    print('状态栏高度:${ScreenUtil.statusBarHeight}px'); //状态栏高度 刘海屏会更高

    print('宽度相对于设计稿放大的倍数:${ScreenUtil().scaleWidth}'); //宽度相对于设计稿放大的倍数
    print('高度相对于设计稿放大的倍数:${ScreenUtil().scaleHeight}'); //高度相对于设计稿放大的倍数
L
li_zy 已提交
74 75 76 77
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
L
li_zy 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
     body: new Center(
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.red,
                  child: Text(ScreenUtil().setWidth(375).toString()),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
                  child: Text(ScreenUtil().setWidth(375).toString()),
                ),
              ],
            ),
            Text(ScreenUtil.screenWidth.toString()),
            Text(ScreenUtil.screenHeight.toString()),
          ],
L
li_zy 已提交
100 101 102 103
      ),
    );
  }
```
L
li_zy 已提交
104 105 106 107

### 使用示例:

[example demo](/example)
L
li_zy 已提交
108
 
L
li_zy 已提交
109 110
效果:

李卓原 已提交
111
![效果](demo.png)
L
li_zy 已提交
112