Flutter移动端屏幕适配⽅案和制作
flutter_screenutil插件
flutter 屏幕适配⽅案,让你的UI在不同尺⼨的屏幕上都能显⽰合理的布局!
注意:此插件仍处于开发阶段,某些API可能尚未推出。
安装依赖:
安装之前请查看最新版本
# 添加依赖
flutter_screenutil: ^0.5.3
在每个使⽤的地⽅导⼊包:
import 'package:flutter_screenutil/flutter_screenutil.dart';
属性
属性类型默认值描述
width double1080px设计稿中设备的宽度,单位px
height double1920px设计稿中设备的⾼度,单位px
allowFontScaling bool false设置字体⼤⼩是否根据系统的“字体⼤⼩”辅助选项来进⾏
缩放
初始化并设置适配尺⼨及字体⼤⼩是否根据系统的“字体⼤⼩”辅助选项来进⾏缩放
在使⽤之前请设置好设计稿的宽度和⾼度,传⼊设计稿的宽度和⾼度(单位px) ⼀定在MaterialApp的home中的页⾯设置(即⼊⼝⽂件,只需设置⼀次),以保证在每次使⽤之前设置好了适配尺⼨:
//填⼊设计稿中设备的屏幕尺⼨
//默认 width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.instance = Instance()..init(context);
//假如设计稿是按iPhone6的尺⼨设计的(iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
//设置字体⼤⼩根据系统的“字体⼤⼩”辅助选项来进⾏缩放,默认为false
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334, allowFontScaling: true)..init(context);
使⽤:
适配尺⼨:
传⼊设计稿的px尺⼨:
根据屏幕宽度适配width: Instance().setWidth(540),
根据屏幕⾼度适配height: Instance().setHeight(200),
也可以使⽤ScreenUtil()替代Instance(), 例如:ScreenUtil().setHeight(200)
注意
⾼度也根据setWidth来做适配可以保证不变形(当你想要⼀个正⽅形的时候)
setHeight⽅法主要是在⾼度上进⾏适配, 在你想控制UI上⼀屏的⾼度与实际中显⽰⼀样时使⽤.
//初始化设计尺⼨
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
print('设备的像素密度:${ScreenUtil.pixelRatio}');
print('设备的⾼:${ScreenUtil.screenHeight}');
print('设备的宽:${ScreenUtil.screenWidth}');
适配字体:
传⼊设计稿的px尺⼨:
//传⼊字体⼤⼩,默认不根据系统的“字体⼤⼩”辅助选项来进⾏缩放(可在初始化ScreenUtil时设置allowFontScaling)
/
/传⼊字体⼤⼩,根据系统的“字体⼤⼩”辅助选项来进⾏缩放(如果某个地⽅不遵循全局的allowFontScaling设置)    ScreenUtil(allowFontScaling: true).setSp(28)
//for example:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('我的⽂字⼤⼩在设计稿上是25px,不会随着系统的⽂字缩放⽐例变化',
style: TextStyle(
color: Colors.black, fontSize: Instance().setSp(24))),
Text('我的⽂字⼤⼩在设计稿上是25px,会随着系统的⽂字缩放⽐例变化',
style: TextStyle(
color: Colors.black, fontSize: ScreenUtil(allowFontScaling: true).setSp(24))),
],
)
其他相关api:
ScreenUtil.pixelRatio      //设备的像素密度
ScreenUtil.screenWidth      //设备宽度
ScreenUtil.screenHeight    //设备⾼度
ScreenUtil.bottomBarHeight  //底部安全区距离,适⽤于全⾯屏下⾯有按键的
ScreenUtil.statusBarHeight  //状态栏⾼度刘海屏会更⾼单位px
完整demo⽰例代码:
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter_ScreenUtil',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'FlutterScreenUtil Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
//设置适配尺⼨ (填⼊设计稿中设备的屏幕尺⼨) 假如设计稿是按iPhone6的尺⼨设计的(iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
print('设备宽度:${ScreenUtil.screenWidth}'); //Device width
print('设备⾼度:${ScreenUtil.screenHeight}'); //Device height
print('设备的像素密度:${ScreenUtil.pixelRatio}'); //Device pixel density
print(
'底部安全区距离:${ScreenUtil.bottomBarHeight}dp'); //Bottom safe zone distance,suitable for buttons with full screen    print(
'状态栏⾼度:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px
print('实际宽度的dp与设计稿px的⽐例:${Instance().scaleWidth}');
print('实际⾼度的dp与设计稿px的⽐例:${Instance().scaleHeight}');
print(
'宽度和字体相对于设计稿放⼤的⽐例:${Instance().scaleWidth * ScreenUtil.pixelRatio}');
print(
'⾼度相对于设计稿放⼤的⽐例:${Instance().scaleHeight * ScreenUtil.pixelRatio}');
print('系统的字体缩放⽐例:${ScaleFactory}');
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(
'Instance().width:${Instance().width}');
print('ScreenUtil().width:${ScreenUtil().width}');
},
child: Icon(Icons.accessible_forward),
),
body: new Center(
child: Column(
crossAxisAlignment: ,
children: <Widget>[
Row(
children: <Widget>[
Container(
width: Instance().setWidth(375),
height: Instance().setHeight(200),
color: d,
child: Text(
'我的宽度:${Instance().setWidth(375)}dp',
flutter开发app
style: TextStyle(
color: Colors.white,
fontSize: Instance().setSp(24),
),
),
),
Container(
width: Instance().setWidth(375),
height: Instance().setHeight(200),
color: Colors.blue,
child:
Text('我的宽度:${Instance().setWidth(375)}dp',
style: TextStyle(
color: Colors.white,
fontSize: Instance().setSp(24),
)),
),
],
),
Text('设备宽度:${ScreenUtil.screenWidth}px'),
Text('设备⾼度:${ScreenUtil.screenHeight}px'),
Text('设备宽度:${ScreenUtil.screenWidthDp}dp'),
Text('设备⾼度:${ScreenUtil.screenHeightDp}dp'),
Text('设计稿宽度:${Instance().width}'),
Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'),
Text('状态栏⾼度:${ScreenUtil.statusBarHeight}dp'),
Text(
'实际宽度的dp与设计稿px的⽐例:${Instance().scaleWidth}',
textAlign: ,
),
Text(
'实际⾼度的dp与设计稿px的⽐例:${Instance().scaleHeight}',              textAlign: ,
),
SizedBox(
height: Instance().setHeight(100),
),
Text('系统的字体缩放⽐例:${ScaleFactory}'),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('我的⽂字⼤⼩在设计稿上是24px,不会随着系统的⽂字缩放⽐例变化',                    style: TextStyle(
color: Colors.black,
fontSize: Instance().setSp(24))),
Text('我的⽂字⼤⼩在设计稿上是24px,会随着系统的⽂字缩放⽐例变化',                    style: TextStyle(
color: Colors.black,
fontSize:
ScreenUtil(allowFontScaling: true).setSp(24))),
],
)
],
),
),
);
}
}
总结:这次学习了使⽤flutter_ScreenUtil来适配Flutter的APP应⽤,需要注意的是这个插件再不断升级中,所以使⽤的时候要使⽤最新版。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。