@override
Widget build(BuildContext context) {
final double shortesSide = MediaQuery.of(context).size.shortestSide;
final bool useMobileLayout = shortesSide <= 600.0; //use this for mobile
final Orientation orientation = MediaQuery.of(context).orientation;
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color.fromRGBO(246, 246, 246, 1.0),
appBar: AppBar(
backgroundColor: Color.fromRGBO(121, 85, 72, 1.0),
centerTitle: true,
title: Text(...),
leading: IconButton(
onPressed: () {
Navigator.pushReplacementNamed(context, 'Menu');
},
icon: Icon(
Icons.arrow_back,
color: Colors.white,
),
)),
body: useMobileLayout
? _buildPhoneView(orientation: orientation)
: _buildTabletView(orientation: orientation),
);
}
//phone
Container _buildPhoneView({@required Orientation orientation}) {...}
//tablet
Container _buildTabletView({@required Orientation orientation}) {...}
诸如600以下的平板电脑的小型手机是否有突破口?我是否需要构建第三个布局,还是可以仅根据屏幕大小校正文本和小部件。谢谢
答案 0 :(得分:1)
这取决于您要构建的布局的复杂性。例如,对于复杂的布局,当屏幕尺寸变小时,小部件可能会覆盖其他小部件,或者当它们没有空间时可能会出现像素溢出。即使Flutter在不同的屏幕上都能很好地缩放,有时候这还不够。
我正在使用LayoutBuilder小部件,并根据其框约束,返回适合其当前约束的屏幕布局。
*请注意,LayoutBuilder小部件从其父级获取约束,因此请确保将其作为顶部小部件放置。
def find(my_list, value):
index = 0
k = -1
for element in my_list:
if element != value :
index += 1
if element == value:
k = index
return k
}
答案 1 :(得分:1)
您可以使用此插件flutter_screenutil。 这是一个用于调整屏幕和字体大小的flutter插件。让您的UI在不同的屏幕大小上显示合理的布局!
根据系统的“字体大小”可访问性选项初始化并设置合适的大小和字体大小以进行缩放 请在使用前设置设计图稿的宽度和高度,以及设计图稿的宽度和高度(单位px)。确保将页面设置在MaterialApp的主页中(即,输入文件,只需设置一次),以确保在每次使用之前设置合适的尺寸:
//fill in the screen size of the device in the design
//default value : width : 1080px , height:1920px ,
allowFontScaling:false
ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
//If the design is based on the size of the iPhone6 (iPhone6 750*1334)
ScreenUtil.instance = ScreenUtil(width: 750, height:
1334)..init(context);
//If you wang to set the font size is scaled according to the system's
"font size" assist option
ScreenUtil.instance = ScreenUtil(width: 750, height: 1334,
allowFontScaling: true)..init(context);
使用:# 适应屏幕尺寸: 传递设计草图的px大小:
适应屏幕宽度:ScreenUtil.getInstance()。setWidth(540),
适应屏幕高度:ScreenUtil.getInstance()。setHeight(200),
您还可以使用ScreenUtil()代替ScreenUtil.getInstance(),例如:ScreenUtil()。setHeight(200)
注意
高度也根据setWidth进行调整,以确保不变形(当您想要一个正方形时)
setHeight方法主要在高度上进行调整,您希望在显示相同的UIUsed时控制屏幕的高度和真实性。
//for example:
//rectangle
Container(
width: ScreenUtil.getInstance().setWidth(375),
height: ScreenUtil.getInstance().setHeight(200),
...
),
////If you want to display a square:
Container(
width: ScreenUtil.getInstance().setWidth(300),
height: ScreenUtil.getInstance().setWidth(300),
),
适配器字体:
//Incoming font size,the unit is pixel, fonts will not scale to
respect Text Size accessibility settings
//(AllowallowFontScaling when initializing ScreenUtil)
ScreenUtil.getInstance().setSp(28)
//Incoming font size,the unit is pixel,fonts will scale to respect Text
Size accessibility settings
//(If somewhere does not follow the global allowFontScaling setting)
ScreenUtil(allowFontScaling: true).setSp(28)
//for example:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'My font size is 24px on the design draft and will not change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: ScreenUtil.getInstance().setSp(24),
)),
Text(
'My font size is 24px on the design draft and will change with the system.',
style: TextStyle(
color: Colors.black,
fontSize: ScreenUtil(allowFontScaling: true).setSp(24),
)),
],
)
其他相关API:
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
ScreenUtil.textScaleFactory //System font scaling factor
ScreenUtil.getInstance().scaleWidth //Ratio of actual width dp to design draft px
ScreenUtil.getInstance().scaleHeight //Ratio of actual height dp to design draft px