現在的手機品牌和型號越來越多,導致我們平時寫布局的時候會在個不同的移動設備上顯示的效果不同,
比如我們的設計稿一個View的大小是300px,如果直接寫300px,可能在當前設備顯示正常,但到了其他設備可能就會偏小或者偏大,這就需要我們對屏幕進行適配。
安卓原生的話有自己的適配規則,可以根據不同的尺寸建立不同的文件夾,系統會根據當前的設備尺寸取對應的大小的布局。而flutter本身並沒有適配規則,而原生的又比較繁瑣,這就需要我們自己去對屏幕進行適配。
點擊直達github地址 如果有幫助,請給我個star
flutter_ScreenUtil
flutter 屏幕適配方案
github: github.com/OpenFlutter…
csdn博客 工具 介紹: blog.csdn.net/u011272795/…
使用方法:
安裝依賴:
安裝之前請查看最新版本
dependencies: flutter: sdk: flutter # 添加依賴 flutter_screenutil: ^0.3.0 復制代碼
在每個使用的地方導入包:
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來做適配可以保證不變形 例如: Container( width: ScreenUtil().setWidth(375), height: ScreenUtil().setHeight(200), ), 復制代碼
適配字體:
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 //寬度相對於設計稿放大的倍數 ScreenUtil().scaleHeight //高度相對於設計稿放大的倍數 復制代碼
//導入 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( '寬度相對於設計稿放大的倍數:${ScreenUtil().scaleWidth}'); //The width is enlarged relative to the design draft print( '高度相對於設計稿放大的倍數:${ScreenUtil().scaleHeight}'); //The height is enlarged relative to the design draft 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( 'My width:${ScreenUtil().setWidth(375)}dp', style: TextStyle( color: Colors.white, fontSize: ScreenUtil().setSp(28, false)), ), ), Container( width: ScreenUtil().setWidth(375), height: ScreenUtil().setHeight(200), color