README.md 2.2 KB
Newer Older
L
li_zy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
# flutter_ScreenUtil
flutter 屏幕适配方案

# 前言:

现在的手机品牌和型号越来越多,导致我们平时写布局的时候会在个不同的移动设备上显示的效果不同,

比如我们的设计稿一个View的大小是300px,如果直接写300px,可能在当前设备显示正常,但到了其他设备可能就会偏小或者偏大,这就需要我们对屏幕进行适配。

安卓原生的话有自己的适配规则,可以根据不同的尺寸建立不同的文件夹,系统会根据当前的设备尺寸取对应的大小的布局。而flutter本身并没有适配规则,而原生的又比较繁琐,这就需要我们自己去对屏幕进行适配。



使用方法:
```
import 'package:flutter_app/ScreenUtil.dart';  //导入

....
//传入设计稿的px尺寸
width: ScreenUtil().setWidth(540),
height: ScreenUtil().setHeight(200),

```

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

@override
  Widget build(BuildContext context) {
    print(ScreenUtil.screenWidth); //设备宽度
    print(ScreenUtil.screenHeight); //设备高度

    print(ScreenUtil.pixelRatio); //设备的像素密度

    print(ScreenUtil.StatusBarHeight); //状态栏高度 刘海屏会更高
    print(ScreenUtil.BottomBarHeight); //底部安全区距离

    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Container(
                width: ScreenUtil().setWidth(540),
                height: ScreenUtil().setHeight(200),
                color: Colors.red,
                child: Text(ScreenUtil().setWidth(540).toString()),
              ),
              Container(
                width: ScreenUtil().setWidth(540),
                height: ScreenUtil().setHeight(200),
                color: Colors.blue,
                child: Text(ScreenUtil().setWidth(540).toString()),
              ),
            ],
          ),
          Text(ScreenUtil.screenWidth.toString()),
          Text(ScreenUtil.screenHeight.toString()),
        ],
      ),
    );
  }
```
效果:
上面的205.xxx 是dp的单位,
下面的单位是px
L
li_zy 已提交
70
![效果](微信图片_20180921000611.png)
L
li_zy 已提交
71