README.md 4.6 KB
Newer Older
1 2

# flutter_ScreenUtil
readme  
李卓原 已提交
3
**A flutter plugin for screen adaptation**
4

readme  
李卓原 已提交
5
[中文文档](/README_CN.md)
李卓原 已提交
6

7 8 9 10
github: https://github.com/OpenFlutter/flutter_ScreenUtil </br>
csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477


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

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

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

readme  
李卓原 已提交
28 29 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).
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:
32 33

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

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

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

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

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

readme  
李卓原 已提交
56
Other related apis:
57
```
readme  
李卓原 已提交
58 59 60 61 62
    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
63

readme  
李卓原 已提交
64 65
    ScreenUtil().scaleWidth //The width is enlarged relative to the design draft
    ScreenUtil().scaleHeight //Height relative to the magnification of the design draft
66 67 68 69

```

```
readme  
李卓原 已提交
70
//import
71 72 73 74 75 76
import 'package:flutter_screenutil/flutter_screenutil.dart';

...

 @override
  Widget build(BuildContext context) {
readme  
李卓原 已提交
77
    //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)
78
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
readme  
李卓原 已提交
79 80 81 82 83 84 85
    print('Device width:${ScreenUtil.screenWidth}'); //Device width
    print('Device height:${ScreenUtil.screenHeight}'); //Device height
    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('The width is enlarged relative to the design draft:${ScreenUtil().scaleWidth}'); //The width is enlarged relative to the design draft
    print('Height relative to the magnification of the design draft:${ScreenUtil().scaleHeight}'); //Height relative to the magnification of the design draft
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.red,
                  child: Text(
readme  
李卓原 已提交
101
                    'My width:${ScreenUtil().setWidth(375)}dp',
102 103 104 105 106 107 108
                    style: TextStyle(color: Colors.white),
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(375),
                  height: ScreenUtil().setHeight(200),
                  color: Colors.blue,
readme  
李卓原 已提交
109
                  child: Text('My width:${ScreenUtil().setWidth(375)}dp',
110 111 112 113
                      style: TextStyle(color: Colors.white)),
                ),
              ],
            ),
readme  
李卓原 已提交
114 115 116 117 118 119 120
            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'),
            Text('The width is enlarged relative to the design draft:${ScreenUtil().scaleWidth}'),
            Text('Height relative to the magnification of the design draft:${ScreenUtil().scaleHeight}'),
121 122 123 124 125 126 127
          ],
        ),
      ),
    );
  }
```

readme  
李卓原 已提交
128
### example:
129 130 131

[example demo](/example)
 
readme  
李卓原 已提交
132
effect:
133

readme  
李卓原 已提交
134
![效果](effect.png)
135