README.md 7.1 KB
Newer Older
1 2

# flutter_ScreenUtil
李卓原 已提交
3
**A flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes!**
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
## Usage:
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
L
LiZhuoyuan 已提交
19
  flutter_screenutil: ^0.4.1
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
### 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).
李卓原 已提交
29
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:
30 31

```
readme  
李卓原 已提交
32 33 34
//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);
35 36
```

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

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

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

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

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

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

```

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

...

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

readme  
李卓原 已提交
191
### example:
192

李卓原 已提交
193
[example demo](/example/lib/main_zh.dart)
194
 
readme  
李卓原 已提交
195
effect:
196

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