简体   繁体   English

Flutter 中的不同屏幕尺寸和 dpi 缩放比例

[英]Different Screen size and dpi scaling in Flutter

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

Is there a breaking point for smaller phones like for tablets <600?小于 600 的平板电脑等小型手机是否存在断点? Do I need to build third layout or I can just correct text and widget based on screen size.我是否需要构建第三种布局,或者我可以根据屏幕尺寸更正文本和小部件。 Thank you谢谢

It depends of the complexity of the layout you're building. 这取决于您要构建的布局的复杂性。 For example with complex layouts, when the screen size gets smaller, widgets might cover other widgets or a pixel overflow could appear when they have no room. 例如,对于复杂的布局,当屏幕尺寸变小时,小部件可能会覆盖其他小部件,或者当它们没有空间时可能会出现像素溢出。 Even though Flutter scales well on different screens, sometimes that is not enough. 即使Flutter在不同的屏幕上都能很好地缩放,有时候这还不够。

What I do is using the LayoutBuilder Widget, and based on its box Constraints, I return a layout for a screen that fits its current constraints. 我正在使用LayoutBuilder窗口小部件,并根据其框“约束”返回适合其当前约束的屏幕布局。

*Note that the LayoutBuilder widget takes its constraints from its parent, so make sure you put it as a top widget. *请注意,LayoutBuilder小部件从其父级获取其约束,因此请确保将其作为顶部小部件放置。

 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();
      }
    },
  ),
);

} }

you can use this plugin flutter_screenutil . 您可以使用此插件flutter_screenutil It is a flutter plugin for adapting screen and font size.Let your UI display a reasonable layout on different screen sizes! 这是一个用于调整屏幕和字体大小的flutter插件。让您的UI在不同的屏幕大小上显示合理的布局!

Initialize and set the fit size and font size to scale according to the system's "font size" accessibility option # Please set the width and height of the design draft before use, the width and height of the design draft (unit px). 根据系统的“字体大小”辅助功能选项初始化并设置合适的尺寸和字体大小,以进行缩放。#请在使用前设置设计草图的宽度和高度,以及设计草图的宽度和高度(单位px)。 Be sure to set the page in the MaterialApp's home(ie the entry file, just set it once) to ensure that the fit size is set before each use: 确保在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);

Use: # Adapt screen size: # Pass the px size of the design draft: 使用:#调整屏幕尺寸:#传递设计草图的px尺寸:

Adapted to screen width: ScreenUtil.getInstance().setWidth(540), 适应屏幕宽度:ScreenUtil.getInstance()。setWidth(540),

Adapted to screen height: ScreenUtil.getInstance().setHeight(200), 适应屏幕高度:ScreenUtil.getInstance()。setHeight(200),

You can also use ScreenUtil() instead of ScreenUtil.getInstance(), for example:ScreenUtil().setHeight(200) 您也可以使用ScreenUtil()代替ScreenUtil.getInstance(),例如:ScreenUtil()。setHeight(200)

Note 注意

Height is also adapted according to setWidth to ensure no deformation (when you want a square) 高度也根据setWidth进行调整,以确保不变形(当您想要正方形时)

setHeight method is mainly adapted in height, you want to control the height and actuality of a screen on the UIUsed when the same is displayed. 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),
        ),

Adapter font: 适配器字体:

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

Other related apis: 其他相关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

I have created a small class to determine screen size.我创建了一个小的 class 来确定屏幕尺寸。 (Extra small, Small, Medium, Large, Extra Large) with short names (xs, sm, md, lg, xl) (特小号、小号、中号、大号、特大号)带有短名称(xs、sm、md、lg、xl)

Create new dart file name as scree_size_info.dart创建新的 dart 文件名为scree_size_info.dart

import 'dart:math';
import 'package:flutter/material.dart';

enum ScreenSize { xs, sm, md, lg, xl, unknown }

class ScreenInfo {
  static Size size(BuildContext c) => MediaQuery.of(c).size;

  static double diagonal(BuildContext c) {
    Size s = size(c);
    return sqrt((s.width * s.width) + (s.height * s.height));
  }

  static ScreenSize screenSize(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 0 && diagonalValue <= 599) {
      return ScreenSize.xs;
    } else if (diagonalValue >= 600 && diagonalValue <= 1023) {
      return ScreenSize.sm;
    } else if (diagonalValue >= 1023 && diagonalValue <= 1439) {
      return ScreenSize.md;
    } else if (diagonalValue >= 1440 && diagonalValue <= 1919) {
      return ScreenSize.lg;
    } else if (diagonalValue >= 1920) {
      return ScreenSize.xl;
    } else {
      return ScreenSize.unknown;
    }
  }

  static bool isExtraSmall(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 0 && diagonalValue <= 599) {
      return true;
    } else {
      return false;
    }
  }

  static bool isSmall(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 600 && diagonalValue <= 1023) {
      return true;
    } else {
      return false;
    }
  }

  static bool isMedium(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 1023 && diagonalValue <= 1439) {
      return true;
    } else {
      return false;
    }
  }

  static bool isLarge(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 1440 && diagonalValue <= 1919) {
      return true;
    } else {
      return false;
    }
  }

  static bool isExtraLarge(BuildContext context) {
    double diagonalValue = diagonal(context);
    if (diagonalValue >= 1920) {
      return true;
    } else {
      return false;
    }
  }
  
}

Usage:用法:

create function:创建 function:

double boxWidth(BuildContext ctx){
  if(ScreenInfo.screenSize(ctx) == ScreenSize.xl ||
      ScreenInfo.screenSize(ctx) == ScreenSize.md ||
      ScreenInfo.screenSize(ctx) == ScreenSize.lg) {
    return 100;
  } else {
    return 30;
  }
}

use width for container:使用容器的宽度:

Container(
   width: boxWidth(context),
   color: Colors.white,
   padding: const EdgeInsets.all(5),
)

I hope it will help you.我希望它能帮助你。 I have used this code in many flutter projects and it works perfectly.我已经在许多 flutter 项目中使用过这段代码,并且效果很好。 Cheers!!干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM