README.md 7.2 KB
Newer Older
1 2

# flutter_ScreenUtil
L
LiZhuoyuan 已提交
3
[![pub package](https://img.shields.io/pub/v/flutter_screenutil.svg)](https://pub.dartlang.org/packages/flutter_screenutil)
L
LiZhuoyuan 已提交
4

李卓原 已提交
5
**A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes!**
6

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

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

L
LiZhuoyuan 已提交
11
[Update log](/CHANGELOG.md)
12

readme  
李卓原 已提交
13
## Usage:
14

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

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

readme  
李卓原 已提交
30 31
### 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).
李卓原 已提交
32
Be sure to set the page in the MaterialApp's home(ie the entry file, just set it once) to ensure that the fit size is set before each use:
33 34

```
readme  
李卓原 已提交
35 36 37
//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);
38 39
```

readme  
李卓原 已提交
40
### Use:
41

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

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

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

李卓原 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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  
李卓原 已提交
73
Other related apis:
74
```
readme  
李卓原 已提交
75 76 77 78 79
    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
李卓原 已提交
80
    ScreenUtil.textScaleFactory //System font scaling factor
81

李卓原 已提交
82 83
    ScreenUtil().scaleWidth //Ratio of actual width dp to design draft px
    ScreenUtil().scaleHeight //Ratio of actual height dp to design draft px
84 85 86 87

```

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

...

李卓原 已提交
93
  @override
94
  Widget build(BuildContext context) {
李卓原 已提交
95
    ///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)
96
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
readme  
李卓原 已提交
97 98
    print('Device width:${ScreenUtil.screenWidth}'); //Device width
    print('Device height:${ScreenUtil.screenHeight}'); //Device height
李卓原 已提交
99 100 101 102 103 104 105
    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(
李卓原 已提交
106
        'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); 
李卓原 已提交
107
    print(
李卓原 已提交
108 109 110 111 112
        'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}'); 
    print(
        'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
    print(
        'The ratio of  height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
113 114 115 116 117 118
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Column(
李卓原 已提交
119
          crossAxisAlignment: CrossAxisAlignment.center,
120 121 122 123 124 125 126 127
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.red,
                  child: Text(
readme  
李卓原 已提交
128
                    'My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
129 130
                    style: TextStyle(
                        color: Colors.white,
李卓原 已提交
131
                        fontSize: ScreenUtil().setSp(12, false)),
132 133 134 135 136 137
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
readme  
李卓原 已提交
138
                  child: Text('My width:${ScreenUtil().setWidth(375)}dp',
李卓原 已提交
139 140
                      style: TextStyle(
                          color: Colors.white,
李卓原 已提交
141
                          fontSize: ScreenUtil().setSp(12, false))),
142 143 144
                ),
              ],
            ),
李卓原 已提交
145
            Text('Device width:${ScreenUtil.screenWidth}px'),
readme  
李卓原 已提交
146 147 148 149
            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'),
李卓原 已提交
150 151 152 153 154 155 156 157
            Text(
              'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}',
              textAlign: TextAlign.center,
            ),
            Text(
              'Ratio of actual height dp to design draft px:${ScreenUtil().scaleHeight}',
              textAlign: TextAlign.center,
            ),
李卓原 已提交
158
            Text(
李卓原 已提交
159
              'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}',
李卓原 已提交
160 161 162
              textAlign: TextAlign.center,
            ),
            Text(
李卓原 已提交
163
              'The ratio of  height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}',
李卓原 已提交
164 165 166
              textAlign: TextAlign.center,
            ),
            SizedBox(
李卓原 已提交
167
              height: ScreenUtil().setHeight(100),
李卓原 已提交
168
            ),
李卓原 已提交
169
            Text('System font scaling factor:${ScreenUtil.textScaleFactory}'),
李卓原 已提交
170 171 172 173
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
李卓原 已提交
174
                    'My font size is 14px on the design draft and will not change with the system.',
李卓原 已提交
175
                    style: TextStyle(
李卓原 已提交
176 177 178 179 180
                      color: Colors.black,
                      fontSize: ScreenUtil().setSp(14, false),
                    )),
                Text(
                    'My font size is 14px on the design draft and will change with the system.',
李卓原 已提交
181
                    style: TextStyle(
李卓原 已提交
182 183 184
                      color: Colors.black,
                      fontSize: ScreenUtil().setSp(14),
                    )),
李卓原 已提交
185 186
              ],
            )
187 188 189 190 191 192 193
          ],
        ),
      ),
    );
  }
```

readme  
李卓原 已提交
194
### example:
195

李卓原 已提交
196
[example demo](/example/lib/main_zh.dart)
197
 
readme  
李卓原 已提交
198
effect:
199

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