README.md 6.5 KB
Newer Older
1 2

# flutter_ScreenUtil
李卓原 已提交
3
**A flutter plugin for adapting screen and font size.Guaranteed to look good on different models**
4

readme  
李卓原 已提交
5
[中文文档](https://github.com/OpenFlutter/flutter_ScreenUtil/blob/master/README_CN.md)
李卓原 已提交
6

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


readme  
李卓原 已提交
10
## Usege:
11

readme  
李卓原 已提交
12 13
### Add dependency:
Please check the latest version before installation.
14 15 16 17
```
dependencies:
  flutter:
    sdk: flutter
readme  
李卓原 已提交
18
  # add flutter_ScreenUtil
0.3.0  
李卓原 已提交
19
  flutter_screenutil: ^0.3.0
20 21
```

readme  
李卓原 已提交
22
### Add the following imports to your Dart code:
23 24 25 26
```
import 'package:flutter_screenutil/flutter_screenutil.dart';
```

readme  
李卓原 已提交
27 28 29 30
### Initialize the setup size
Please set the width and height of the design draft before use, the width and height of the design draft (unit px).
If not set, the default size is used. The default is 1080*1920.
Be sure to set the page in the MaterialApp's home to ensure that the fit size is set before each use:
31 32

```
readme  
李卓原 已提交
33 34 35
//Set the fit size (fill in the screen size of the device in the design)
//If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
36 37
```

readme  
李卓原 已提交
38
### Use:
39

李卓原 已提交
40
Adapt screen size:
41
```
readme  
李卓原 已提交
42 43 44 45
Pass the px size of the design draft:

Width after adaptation: ScreenUtil().setWidth(540),
Height after adaptation: ScreenUtil().setHeight(200),
46

readme  
李卓原 已提交
47
//for example:
48 49 50
Container(
           width: ScreenUtil().setWidth(375),
           height: ScreenUtil().setHeight(200),
readme  
李卓原 已提交
51
           ...
52 53 54
            ),
```

李卓原 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
Adapter font:
``` 
      ScreenUtil().setSp(28)         //Incoming font size,the unit is pixel, fonts will scale to respect Text Size accessibility settings
      ScreenUtil().setSp(28,false)  //Incoming font size,the unit is pixel,fonts will not scale to respect Text Size accessibility settings

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
Other related apis:
72
```
readme  
李卓原 已提交
73 74 75 76 77
    ScreenUtil.pixelRatio       //Device pixel density
    ScreenUtil.screenWidth      //Device width
    ScreenUtil.screenHeight     //Device height
    ScreenUtil.bottomBarHeight  //Bottom safe zone distance, suitable for buttons with full screen
    ScreenUtil.statusBarHeight  //Status bar height , Notch will be higher Unit px
李卓原 已提交
78
    ScreenUtil.textScaleFactory //System font scaling factor
79

readme  
李卓原 已提交
80 81
    ScreenUtil().scaleWidth //The width is enlarged relative to the design draft
    ScreenUtil().scaleHeight //Height relative to the magnification of the design draft
82 83 84 85

```

```
readme  
李卓原 已提交
86
//import
87 88 89 90 91 92
import 'package:flutter_screenutil/flutter_screenutil.dart';

...

 @override
  Widget build(BuildContext context) {
李卓原 已提交
93
    //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
94
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
readme  
李卓原 已提交
95 96
    print('Device width:${ScreenUtil.screenWidth}'); //Device width
    print('Device height:${ScreenUtil.screenHeight}'); //Device height
李卓原 已提交
97 98 99 100 101 102 103 104 105 106 107
    print(
        'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density
    print(
        'Bottom safe zone distance:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
    print(
        'Status bar height:${ScreenUtil.statusBarHeight}px'); //Status bar height , Notch will be higher Unit px
    print(
        'Width is enlarged relative to the design draft:${ScreenUtil().scaleWidth}'); //The width is enlarged relative to the design draft
    print(
        'Height is enlarged relative to the design draft:${ScreenUtil().scaleHeight}'); //The height is enlarged relative to the design draft
    print('System font scaling:${ScreenUtil.textScaleFactory}');
108 109 110 111 112 113 114

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Column(
李卓原 已提交
115
          crossAxisAlignment: CrossAxisAlignment.center,
116 117 118 119 120 121 122 123
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.red,
                  child: Text(
readme  
李卓原 已提交
124
                    'My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
125 126 127
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: ScreenUtil().setSp(28, false)),
128 129 130 131 132 133
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
readme  
李卓原 已提交
134
                  child: Text('My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
135 136 137
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenUtil().setSp(28, false))),
138 139 140
                ),
              ],
            ),
readme  
李卓原 已提交
141 142 143 144 145
            Text('Device width:${ScreenUtil.screenWidth}px'),
            Text('Device height:${ScreenUtil.screenHeight}px'),
            Text('Device pixel density:${ScreenUtil.pixelRatio}'),
            Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}px'),
            Text('Status bar height:${ScreenUtil.statusBarHeight}px'),
李卓原 已提交
146 147 148 149 150 151 152 153 154 155 156
            Text(
              'Width is enlarged relative to the design draft:${ScreenUtil().scaleWidth}',
              textAlign: TextAlign.center,
            ),
            Text(
              'Height is enlarged relative to the design draft:${ScreenUtil().scaleHeight}',
              textAlign: TextAlign.center,
            ),
            SizedBox(
              height: ScreenUtil().setHeight(200),
            ),
李卓原 已提交
157
            Text('System font scaling factor:${ScreenUtil.textScaleFactory}'),
李卓原 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                    'My font size is 28px and will not change with the system.',
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: ScreenUtil().setSp(28, false))),
                Text('My font size is 28px and will change with the system.',
                    style: TextStyle(
                        color: Colors.black, fontSize: ScreenUtil().setSp(28))),
              ],
            )
171 172 173 174 175 176 177
          ],
        ),
      ),
    );
  }
```

readme  
李卓原 已提交
178
### example:
179

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

李卓原 已提交
184
![效果](demo_en.png)
185