当前位置:   article > 正文

flutter 基础之屏幕适配_flutter scaffold 撑满屏幕

flutter scaffold 撑满屏幕

随着苹果,Google等大厂不断研发新的设备机型,Android和iOS的设备种类越来越繁多了,有普通的屏幕16:9的,也有最近几年出来的全面屏,水滴屏,刘海屏等,Android的全面屏基本上是18:9的比例,iPhone X的尺寸是19.5:9。按照这个结论,大致上是有三种尺寸的图的。所以得让设计师一个广告图,切三个尺寸。

根据现有的屏幕比例,分成了大致的三类:基础版 1080*1920(通用16:9)、Android 全面屏 1080*2160(比例18:9)、iPhone X 1125*2436(19.5:9)让设计师一张图切出这几个尺寸即可。

客户端调用api,传递高宽比的值,服务端根据高宽比返回对应的图片。这样图片加载到页面上,经过缩放拉拉伸等等,效果上也还不错。(需要服务端配合)

Android获取屏幕高宽比的代码如下:

  1. DecimalFormat decimalFormat = new DecimalFormat(“.0”);
  2. String scale = decimalFormat.format(1.0 * DisplayUtils.getWindowHeight() / DisplayUtils.getWindowWidth());

其他的一些思路:

实际上如果团队约定明确的话,一种尺寸也是可行的,这种的话,需要让ui设计师,以及运营明白,
做出来的图片(适用于广告营销、品牌推广类型的),靠近左右两边的位置不要放置重要信息,图片不要过于复杂等,以免被裁剪影响阅读和效果。有这个意识的话,一张图片也可行,这样就不存在拉伸形变的问题。

flutter 屏幕适配方案,让你的UI在不同尺寸的屏幕上都能显示合理的布局!

安装之前请查看最新版本

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. # 添加依赖
  5. flutter_screenutil: ^0.5.0

在每个使用的地方导入包:

import 'package:flutter_screenutil/flutter_screenutil.dart'; 

属性

属性类型默认参数值x详细描述
widthint1080px设计稿中设备的宽度,单位px
heightint1920px设计稿中设备的高度,单位px
allowFontScalingboolfalse设置字体大小是否根据系统的“字体大小”辅助选项来进行缩放

初始化并设置适配尺寸及字体大小是否根据系统的“字体大小”辅助选项来进行缩放

在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px) 一定在MaterialApp的home中的页面设置(即入口文件,只需设置一次),以保证在每次使用之前设置好了适配尺寸:

  1. //填入设计稿中设备的屏幕尺寸
  2. //默认 width : 1080px , height:1920px , allowFontScaling:false
  3. ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
  4. //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
  5. ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
  6. //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false : 字体随着系统的“字体大小”辅助选项来进行缩放
  7. ScreenUtil.instance = ScreenUtil(width: 750, height: 1334, allowFontScaling: true)..init(context);

根据屏幕宽度适配 width: ScreenUtil.getInstance().setWidth(540),

根据屏幕高度适配 height: ScreenUtil.getInstance().setHeight(200),

也可以使用 ScreenUtil() 替代 ScreenUtil.getInstance(), 例如:ScreenUtil().setHeight(200)

提示:

高度也根据setWidth来做适配可以保证不变形(当你想要一个正方形的时候)

setHeight方法主要是在高度上进行适配, 在你想控制UI上一屏的高度与实际中显示一样时使用.

示例:

  1. //长方形:
  2. Container(
  3. width: ScreenUtil.getInstance().setWidth(375),
  4. height: ScreenUtil.getInstance().setHeight(200),
  5. ),
  6. //如果你想显示一个正方形:
  7. Container(
  8. width: ScreenUtil.getInstance().setWidth(300),
  9. height: ScreenUtil.getInstance().setWidth(300),
  10. ),

传入设计稿的px尺寸:

  1. //传入字体大小,默认不根据系统的“字体大小”辅助选项来进行缩放(可在初始化ScreenUtil时设置allowFontScaling)
  2. ScreenUtil.getInstance().setSp(28)
  3. //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放(如果某个地方不遵循全局的allowFontScaling设置)
  4. ScreenUtil(allowFontScaling: true).setSp(28)
  5. //for example:
  6. Column(
  7. crossAxisAlignment: CrossAxisAlignment.start,
  8. children: <Widget>[
  9. Text('我的文字大小在设计稿上是25px,不会随着系统的文字缩放比例变化',
  10. style: TextStyle(
  11. color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(24))),
  12. Text('我的文字大小在设计稿上是25px,会随着系统的文字缩放比例变化',
  13. style: TextStyle(
  14. color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
  15. ],
  16. )

其他相关api:

  1. ScreenUtil.pixelRatio //设备的像素密度
  2. ScreenUtil.screenWidth //设备宽度
  3. ScreenUtil.screenHeight //设备高度
  4. ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
  5. ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px
  6. ScreenUtil.textScaleFactory //系统字体缩放比例
  7. ScreenUtil.getInstance().scaleWidth // 实际宽度的dp与设计稿px的比例
  8. ScreenUtil.getInstance().scaleHeight // 实际高度的dp与设计稿px的比例
  1. //导入
  2. import 'package:flutter_screenutil/flutter_screenutil.dart';
  3. ...
  4. @override
  5. Widget build(BuildContext context) {
  6. //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
  7. ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
  8. print('设备宽度:${ScreenUtil.screenWidth}'); //Device width
  9. print('设备高度:${ScreenUtil.screenHeight}'); //Device height
  10. print('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
  11. print(
  12. '底部安全区距离:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
  13. print(
  14. '状态栏高度:${ScreenUtil.statusBarHeight}px'); //Status bar height , Notch will be higher Unit px
  15. print('实际宽度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleWidth}');
  16. print('实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}');
  17. print(
  18. '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}');
  19. print(
  20. '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}');
  21. print('系统的字体缩放比例:${ScreenUtil.textScaleFactory}');
  22. return new Scaffold(
  23. appBar: new AppBar(
  24. title: new Text(widget.title),
  25. ),
  26. body: new Center(
  27. child: Column(
  28. crossAxisAlignment: CrossAxisAlignment.center,
  29. children: <Widget>[
  30. Row(
  31. children: <Widget>[
  32. Container(
  33. width: ScreenUtil.getInstance().setWidth(375),
  34. height: ScreenUtil.getInstance().setHeight(200),
  35. color: Colors.red,
  36. child: Text(
  37. '我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
  38. style: TextStyle(
  39. color: Colors.white,
  40. fontSize: ScreenUtil.getInstance().setSp(12),
  41. ),
  42. ),
  43. ),
  44. Container(
  45. width: ScreenUtil.getInstance().setWidth(375),
  46. height: ScreenUtil.getInstance().setHeight(200),
  47. color: Colors.blue,
  48. child: Text('我的宽度:${ScreenUtil.getInstance().setWidth(375)}dp',
  49. style: TextStyle(
  50. color: Colors.white,
  51. fontSize: ScreenUtil.getInstance().setSp(12),
  52. )),
  53. ),
  54. ],
  55. ),
  56. Text('设备宽度:${ScreenUtil.screenWidth}px'),
  57. Text('设备高度:${ScreenUtil.screenHeight}px'),
  58. Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
  59. Text('底部安全区距离:${ScreenUtil.bottomBarHeight}px'),
  60. Text('状态栏高度:${ScreenUtil.statusBarHeight}px'),
  61. Text(
  62. '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
  63. textAlign: TextAlign.center,
  64. ),
  65. Text(
  66. '实际高度的dp与设计稿px的比例:${ScreenUtil.getInstance().scaleHeight}',
  67. textAlign: TextAlign.center,
  68. ),
  69. Text(
  70. '宽度和字体相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}',
  71. textAlign: TextAlign.center,
  72. ),
  73. Text(
  74. '高度相对于设计稿放大的比例:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}',
  75. textAlign: TextAlign.center,
  76. ),
  77. SizedBox(
  78. height: ScreenUtil.getInstance().setHeight(100),
  79. ),
  80. Text('系统的字体缩放比例:${ScreenUtil.textScaleFactory}'),
  81. Column(
  82. crossAxisAlignment: CrossAxisAlignment.start,
  83. children: <Widget>[
  84. Text('我的文字大小在设计稿上是25px,不会随着系统的文字缩放比例变化',
  85. style: TextStyle(
  86. color: Colors.black, fontSize: ScreenUtil.getInstance().setSp(24))),
  87. Text('我的文字大小在设计稿上是25px,会随着系统的文字缩放比例变化',
  88. style: TextStyle(
  89. color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
  90. ],
  91. )
  92. ],
  93. ),
  94. ),
  95. );
  96. }
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号