Android 颤振中不同屏幕尺寸和dpi缩放 @覆盖 小部件构建(构建上下文){ final double shortesSide=MediaQuery.of(context).size.shortestSide; final bool useMobileLayout=shortesSide

Android 颤振中不同屏幕尺寸和dpi缩放 @覆盖 小部件构建(构建上下文){ final double shortesSide=MediaQuery.of(context).size.shortestSide; final bool useMobileLayout=shortesSide,android,flutter,size,screen,Android,Flutter,Size,Screen,这取决于您正在构建的布局的复杂性。例如,对于复杂的布局,当屏幕尺寸变小时,小部件可能会覆盖其他小部件,或者当它们没有空间时,可能会出现像素溢出。即使颤振在不同的屏幕上可以很好地缩放,有时这还不够 我所做的是使用小部件,并基于其框约束,返回符合其当前约束的屏幕布局 *请注意,LayoutBuilder小部件从其父部件获取其约束,因此请确保将其作为顶级小部件放置 @override Widget build(BuildContext context) { final double

这取决于您正在构建的布局的复杂性。例如,对于复杂的布局,当屏幕尺寸变小时,小部件可能会覆盖其他小部件,或者当它们没有空间时,可能会出现像素溢出。即使颤振在不同的屏幕上可以很好地缩放,有时这还不够

我所做的是使用小部件,并基于其框约束,返回符合其当前约束的屏幕布局

*请注意,LayoutBuilder小部件从其父部件获取其约束,因此请确保将其作为顶级小部件放置

  @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}) {...}

小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:布局生成器(
生成器:(上下文、约束){
如果(constraints.maxWidth<600){
返回SmallPage();
}否则{
返回BigPage();
}
},
),
);
}您可以使用此插件。
这是一个适应屏幕和字体大小的颤振插件。让你的用户界面在不同的屏幕大小上显示合理的布局

根据系统的“字体大小”可访问性选项初始化并设置合适的大小和字体大小# 请在使用前设置设计草图的宽度和高度,设计草图的宽度和高度(单位px)。确保在MaterialApp的主页中设置页面(即输入文件,只需设置一次),以确保在每次使用前设置合适的尺寸:

 Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: LayoutBuilder(
    builder: (context, constraints) {
      if (constraints.maxWidth < 600) {
        return SmallPage();
      } else {
        return BigPage();
      }
    },
  ),
);
使用:# 调整屏幕大小:# 通过设计草图的px尺寸:

适应屏幕宽度:ScreenUtil.getInstance().setWidth(540)

适应屏幕高度:ScreenUtil.getInstance().setHeight(200)

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

高度也根据setWidth进行调整,以确保不变形(当您想要正方形时)

setHeight方法主要适用于高度,您希望在显示屏幕时控制屏幕在UI上的高度和现状

//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);
适配器字体:

//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),
        ),

关于字体大小,您可以根据屏幕文本的大小调整大小(“文本”,style:TextStyle(fontSize:screenSize<300?12:16))。另一个选项是将文本包装在fittedBox中,并将文本的大小设置为零。fittedBox(child:Text(“文本”))。这将使文本与其可用空间一样大。null,请不要只是发布一些工具/库或插件作为答案。至少在答案中进行演示。这是一个用于调整屏幕和字体大小的颤振插件。让您的UI在不同的屏幕大小上显示合理的布局!
//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),
                )),
          ],
        )
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