当前位置: 移动技术网 > 移动技术>移动开发>Android > flutter 屏幕尺寸适配和字体大小适配的实现

flutter 屏幕尺寸适配和字体大小适配的实现

2019年09月06日  | 移动技术网移动技术  | 我要评论

前言:

现在的手机品牌和型号越来越多,导致我们平时写布局的时候会在个不同的移动设备上显示的效果不同,

比如我们的设计稿一个view的大小是300px,如果直接写300px,可能在当前设备显示正常,但到了其他设备可能就会偏小或者偏大,这就需要我们对屏幕进行适配。

安卓原生的话有自己的适配规则,可以根据不同的尺寸建立不同的文件夹,系统会根据当前的设备尺寸取对应的大小的布局。而flutter本身并没有适配规则,而原生的又比较繁琐,这就需要我们自己去对屏幕进行适配。

点击直达github地址

如果有帮助,请给我个star

flutter_screenutil

flutter 屏幕适配方案

github: https://github.com/openflutter/flutter_screenutil  

使用方法:

安装依赖:

安装之前请查看最新版本

dependencies:
 flutter:
  sdk: flutter
 # 添加依赖
 flutter_screenutil: ^0.4.2

在每个使用的地方导入包:

import 'package:flutter_screenutil/flutter_screenutil.dart';

初始化设置尺寸

在使用之前请设置好设计稿的宽度和高度,传入设计稿的宽度和高度(单位px)

一定在materialapp的home中的页面设置(即入口文件,只需设置一次),以保证在每次使用之前设置好了适配尺寸:

//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 假如设计稿是按iphone6的尺寸设计的(iphone6 750*1334)
  screenutil.instance = screenutil(width: 750, height: 1334)..init(context);

使用:

适配尺寸:

传入设计稿的px尺寸:

根据屏幕宽度适配 width: screenutil().setwidth(540),

根据屏幕高度适配 height: screenutil().setheight(200),

注意

高度也根据setwidth来做适配可以保证不变形(当你想要一个正方形的时候)

setheight方法主要是在高度上进行适配, 在你想控制ui上一屏的高度与实际中显示一样时使用.

例如:

//长方形:
container(
      width: screenutil().setwidth(375),
      height: screenutil().setheight(200),
      ),
      
//如果你想显示一个正方形:
container(
      width: screenutil().setwidth(300),
      height: screenutil().setwidth(300),
      ),

适配字体:

   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('设备宽度:${screenutil.screenwidth}'); //device width
  print('设备高度:${screenutil.screenheight}'); //device height
  print('设备的像素密度:${screenutil.pixelratio}'); //device pixel density
  print(
    '底部安全区距离:${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的比例:${screenutil().scalewidth}');
  print('实际高度的dp与设计稿px的比例:${screenutil().scaleheight}');

  print(
    '宽度和字体相对于设计稿放大的比例:${screenutil().scalewidth * screenutil.pixelratio}'); 
  print(
    '高度相对于设计稿放大的比例:${screenutil().scaleheight * screenutil.pixelratio}'); 
  print('系统的字体缩放比例:${screenutil.textscalefactory}');

  return new scaffold(
   appbar: new appbar(
    title: new text(widget.title),
   ),
   body: new center(
    child: column(
     crossaxisalignment: crossaxisalignment.center,
     children: <widget>[
      row(
       children: <widget>[
        container(
         width: screenutil().setwidth(375),
         height: screenutil().setheight(200),
         color: colors.red,
         child: text(
          '我的宽度:${screenutil().setwidth(375)}dp',
          style: textstyle(
           color: colors.white,
           fontsize: screenutil().setsp(12, false),
          ),
         ),
        ),
        container(
         width: screenutil().setwidth(375),
         height: screenutil().setheight(200),
         color: colors.blue,
         child: text('我的宽度:${screenutil().setwidth(375)}dp',
           style: textstyle(
            color: colors.white,
            fontsize: screenutil().setsp(12, false),
           )),
        ),
       ],
      ),
      text('设备宽度:${screenutil.screenwidth}px'),
      text('设备高度:${screenutil.screenheight}px'),
      text('设备的像素密度:${screenutil.pixelratio}'),
      text('底部安全区距离:${screenutil.bottombarheight}px'),
      text('状态栏高度:${screenutil.statusbarheight}px'),
      text(
       '实际高度的dp与设计稿px的比例:${screenutil().scaleheight}',
       textalign: textalign.center,
      ),
      text(
       '实际高度的dp与设计稿px的比例:${screenutil().scaleheight}',
       textalign: textalign.center,
      ),
      text(
       '宽度和字体相对于设计稿放大的比例:${screenutil().scalewidth * screenutil.pixelratio}',
       textalign: textalign.center,
      ),
      text(
       '高度相对于设计稿放大的比例:${screenutil().scaleheight * screenutil.pixelratio}',
       textalign: textalign.center,
      ),
      sizedbox(
       height: screenutil().setheight(100),
      ),
      text('系统的字体缩放比例:${screenutil.textscalefactory}'),
      column(
       crossaxisalignment: crossaxisalignment.start,
       children: <widget>[
        text('我的文字大小在设计稿上是14px,不会随着系统的文字缩放比例变化',
          style: textstyle(
            color: colors.black,
            fontsize: screenutil().setsp(14, false))),
        text('我的文字大小在设计稿上是14px,会随着系统的文字缩放比例变化',
          style: textstyle(
            color: colors.black, fontsize: screenutil().setsp(14))),
       ],
      )
     ],
    ),
   ),
  );
 }

使用示例:

example demo

适配原理

说一下适配方案, 比如我们设计师设计的ui是根据iphone6来做的,我们知道 iphone6的分辨率是750*1334(px),

又或者是根据hdpi的设备来设计的ui,我们知道hdpi的 android设备是 (240 dpi),像素密度是1.5,即hdpi设备的分辨率宽度是320px, 总之,无论设计稿的单位是px,或者是dp,我们都能够转换成px.

那么我们如果根据px来适配,ios和 android 就都可以兼容了.

假设,我们的设计稿手机是10801920 px.

设计稿上有一个540960 的组件, 即宽度和宽度是手机的一半. 如果我们直接写的时候组件的尺寸这么定义,在其他尺寸的设备上未必是一半,或多,或少. 但是我们可以按比例来看,即我们要实现的宽度是实际设备的一半.

那么假设我们设备的宽度是devicewidth和deviceheight , 我们要写的组件大小为: 宽:(540/1080)*devicewidth,高度: (960/1920)*deviceheight.

通过这个公式我们可以发现,我们要写的组件宽度就是设计稿上的尺寸width*(devicewdith/原型设备宽度).那么每次我们写ui的时候,只要直接哪来设计稿的尺寸*(devicewdith/设备原型)宽度即可.

原理就是先获取,实际设备与原型设备的尺寸比例.

首先flutter获取设备的尺寸的代码是:

以下数据为我的手机数据:

import 'dart:ui';
//因为window是dart:ui中提供的,所以需要引入这个包.
window.physicalsize //size(1080.0, 1794.0) 单位px
width = window.physicalsize.width //宽度
height = window.physicalsize.height //高度

//使用这个方法则无需引入包
mediaquery.of(context).size  //size(411.4, 683.4)  单位:dp
widhtdp = mediaquery.of(context).size.width  //宽度 411.4
heightdp = mediaquery.of(context).size.height //高度 683.4

设计稿单位是px,且尺寸为1080*1920 px 时:

scalewidth = width / 1080;
scaleheight = height / 1920;

那么我们要写尺寸为500100控件的宽度就是 500scalewidth .100*scaleheigh ,注意这时单位是px,flutter中默认组件尺寸单位都是dp,我们还要进行px->dp的操作.除以像素密度就好了.
flutter获取像素密度的方法:

mediaquery.of(context).devicepixelratio
window.physicalsize   

上面两种方法得到的是一样的结果,但是window对象来自dart:ui,所以我们引入这个包:

import ‘dart:ui';

设计稿单位是dp,且尺寸为360*640 dp 时:

scalewidth = widhtdp / 360;
scaleheight = heightdp / 640;

那么我们要写尺寸为500*100控件的宽度就是 500*scalewidth .100*scaleheigh

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网