width: ScreenUtil().setWidth(375),
height: ScreenUtil().setHeight(200),
),
适配字体:
ScreenUtil().setSp(28) //传入字体大小,根据系统的“字体大小”辅助选项来进行缩放
ScreenUtil().setSp(28,false) //传入字体大小,不会根据系统的“字体大小”辅助选项来进行缩放
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)
)
),
其他相关api:
ScreenUtil.pixelRatio //设备的像素密度
ScreenUtil.screenWidth //设备宽度
ScreenUtil.screenHeight //设备高度
ScreenUtil.bottomBarHeight //底部安全区距离,适用于全面屏下面有按键的
ScreenUtil.statusBarHeight //状态栏高度 刘海屏会更高 单位px
ScreenUtil.textScaleFactory //系统字体缩放比例
ScreenUtil().scaleWidth // 实际宽度的dp与设计稿px的比例
ScreenUtil().scaleHeight // 实际高度的dp与设计稿px的比例
//导入
import ‘package:flutter_screenutil/flutter_screenutil.dart’;
…
@override
Widget build(BuildContext context) {
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width:
750, height: 1334)…init(context);
print(‘设备宽度: S c r e e n U t i l . s c r e e n W i d t h ′ ) ; / / D e v i c e w i d t h p r i n t ( ′ 设 备 高 度 : {ScreenUtil.screenWidth}'); //Device width print('设备高度: ScreenUtil.screenWidth′);//Devicewidthprint(′设备高度:{ScreenUtil.screenHeight}’); //Device height
print(‘设备的像素密度: S c r e e n U t i l . p i x e l R a t i o ′ ) ; / / D e v i c e p i x e l d e n s i t y p r i n t ( ′ 底 部 安 全 区 距 离 : {ScreenUtil.pixelRatio}'); //Device pixel density print( '底部安全区距离: ScreenUtil.pixelRatio′);//Devicepixeldensityprint(′底部安全区距离:{ScreenUtil.bottomBarHeight}’); //Bottom safe zone distance,suitable for buttons with full screen
print(
‘状态栏高度:${ScreenUtil.statusBarHeight}px’); //Status bar height , Notch will be higher Unit px
print(‘实际宽度的dp与设计稿px的比例: S c r e e n U t i l ( ) . s c a l e W i d t h ′ ) ; p r i n t ( ′ 实 际 高 度 的 d p 与 设 计 稿 p x 的 比 例 : {ScreenUtil().scaleWidth}'); print('实际高度的dp与设计稿px的比例: ScreenUtil().scaleWidth′);p