提交 cff6d047 编写于 作者: L LiZhuoyuan

2.0.0

上级 77699ad6
...@@ -2,10 +2,14 @@ ...@@ -2,10 +2,14 @@
* @Author: zhuoyuan93@gmail.com * @Author: zhuoyuan93@gmail.com
* @Date: 2018-10-16 19:43:03 * @Date: 2018-10-16 19:43:03
* @LastEditors: zhuoyuan93@gmail.com * @LastEditors: zhuoyuan93@gmail.com
* @LastEditTime: 2020年1月14日 12:11:02 * @LastEditTime: 2020年6月20日 11:20:02
* @Description: Update log * @Description: Update log
--> -->
#2.0.0
- Use `MediaQueryData.fromWindow(window)` instead of `MediaQuery.of(context)`, no context parameter required
- API changes, please note
#1.1.0 #1.1.0
- support ExtensionMethod Dart-SDK-2.6.0 - support ExtensionMethod Dart-SDK-2.6.0
- you can use 'width: 50.w' instead of 'width: ScreenUtil().setWidth(50)' - you can use 'width: 50.w' instead of 'width: ScreenUtil().setWidth(50)'
......
...@@ -18,12 +18,13 @@ github: https://github.com/OpenFlutter/flutter_screenutil ...@@ -18,12 +18,13 @@ github: https://github.com/OpenFlutter/flutter_screenutil
### Add dependency: ### Add dependency:
Please check the latest version before installation. Please check the latest version before installation.
If there is any problem with the new version, please use the previous version
``` ```
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
# add flutter_screenutil # add flutter_screenutil
flutter_screenutil: ^1.0.2 flutter_screenutil: ^2.0.0
``` ```
### Add the following imports to your Dart code: ### Add the following imports to your Dart code:
...@@ -48,13 +49,16 @@ Be sure to set the page in the MaterialApp's home(ie the entry file, just set it ...@@ -48,13 +49,16 @@ Be sure to set the page in the MaterialApp's home(ie the entry file, just set it
//fill in the screen size of the device in the design //fill in the screen size of the device in the design
//default value : width : 1080px , height:1920px , allowFontScaling:false //default value : width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.init(context); ScreenUtil.init(context); //flutter_screenuitl < 1.2
ScreenUtil.init(); //flutter_screenuitl >= 1.2
//If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334) //If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(context, width: 750, height: 1334); ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
//If you want to set the font size is scaled according to the system's "font size" assist option //If you want to set the font size is scaled according to the system's "font size" assist option
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
``` ```
...@@ -189,7 +193,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -189,7 +193,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
//Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334) //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
return ExampleWidget(title: 'FlutterScreenUtil Demo'); return ExampleWidget(title: 'FlutterScreenUtil Demo');
} }
...@@ -244,10 +248,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -244,10 +248,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
), ),
], ],
), ),
Text('Device width:${ScreenUtil.screenWidth}px'), Text('Device width:${ScreenUtil.screenWidth}dp'),
Text('Device height:${ScreenUtil.screenHeight}px'), Text('Device height:${ScreenUtil.screenHeight}dp'),
Text('Device width:${ScreenUtil.screenWidthDp}dp'), Text('Device width:${ScreenUtil.screenWidthPx}px'),
Text('Device height:${ScreenUtil.screenHeightDp}dp'), Text('Device height:${ScreenUtil.screenHeightPx}px'),
Text('Device pixel density:${ScreenUtil.pixelRatio}'), Text('Device pixel density:${ScreenUtil.pixelRatio}'),
Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}dp'), Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}dp'),
Text('Status bar height:${ScreenUtil.statusBarHeight}dp'), Text('Status bar height:${ScreenUtil.statusBarHeight}dp'),
...@@ -286,8 +290,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -286,8 +290,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: Icon(Icons.title), child: Icon(Icons.title),
onPressed: () { onPressed: () {
ScreenUtil.init(context, ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
width: 1500, height: 1334, allowFontScaling: false);
setState(() {}); setState(() {});
}, },
), ),
...@@ -302,7 +305,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -302,7 +305,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
print( print(
'Bottom safe zone distance dp:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen 'Bottom safe zone distance dp:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
print( print(
'Status bar height px:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px 'Status bar height dp:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit dp
print( print(
'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}'); 'Ratio of actual width dp to design draft px:${ScreenUtil().scaleWidth}');
print( print(
......
...@@ -23,12 +23,13 @@ csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477 ...@@ -23,12 +23,13 @@ csdn博客工具介绍:https://blog.csdn.net/u011272795/article/details/82795477
### 安装依赖: ### 安装依赖:
安装之前请查看最新版本 安装之前请查看最新版本
新版本如有问题请使用上一版
``` ```
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
# 添加依赖 # 添加依赖
flutter_screenutil: ^1.0.2 flutter_screenutil: ^2.0.0
``` ```
### 在每个使用的地方导入包: ### 在每个使用的地方导入包:
...@@ -52,13 +53,16 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; ...@@ -52,13 +53,16 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
//填入设计稿中设备的屏幕尺寸 //填入设计稿中设备的屏幕尺寸
//默认 width : 1080px , height:1920px , allowFontScaling:false //默认 width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.init(context); ScreenUtil.init(context); //flutter_screenuitl < 1.2
ScreenUtil.init(); //flutter_screenuitl >= 1.2
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.init(context, width: 750, height: 1334); ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
``` ```
...@@ -198,9 +202,9 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -198,9 +202,9 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
ScreenUtil.init(context); ScreenUtil.init();
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
return ExampleWidget(title: 'FlutterScreenUtil示例'); return ExampleWidget(title: 'FlutterScreenUtil示例');
} }
...@@ -255,10 +259,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -255,10 +259,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
), ),
], ],
), ),
Text('设备宽度:${ScreenUtil.screenWidth}px'), Text('设备宽度:${ScreenUtil.screenWidth}dp'),
Text('设备高度:${ScreenUtil.screenHeight}px'), Text('设备高度:${ScreenUtil.screenHeight}dp'),
Text('设备宽度:${ScreenUtil.screenWidthDp}dp'), Text('设备宽度:${ScreenUtil.screenWidthPx}px'),
Text('设备高度:${ScreenUtil.screenHeightDp}dp'), Text('设备高度:${ScreenUtil.screenHeightPx}px'),
Text('设备的像素密度:${ScreenUtil.pixelRatio}'), Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'), Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'),
Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'), Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'),
...@@ -295,8 +299,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -295,8 +299,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: Icon(Icons.title), child: Icon(Icons.title),
onPressed: () { onPressed: () {
ScreenUtil.init(context, ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
width: 1500, height: 1334, allowFontScaling: false);
setState(() {}); setState(() {});
}, },
), ),
...@@ -310,11 +313,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -310,11 +313,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
print( print(
'底部安全区距离:${ScreenUtil.bottomBarHeight}dp'); //Bottom safe zone distance,suitable for buttons with full screen '底部安全区距离:${ScreenUtil.bottomBarHeight}dp'); //Bottom safe zone distance,suitable for buttons with full screen
print( print(
'状态栏高度:${ScreenUtil.statusBarHeight}dp'); //Status bar height , Notch will be higher Unit px '状态栏高度:${ScreenUtil.statusBarHeight}dp'); //状态栏高度 , 刘海屏会更高,单位dp
print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}'); print('实际宽度的dp与设计稿px的比例:${ScreenUtil().scaleWidth}');
print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}'); print('实际高度的dp与设计稿px的比例:${ScreenUtil().scaleHeight}');
print( print(
'宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}'); '宽度和字体相对于设计稿放大的比例:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}'); print('高度相对于设计稿放大的比例:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
......
...@@ -18,12 +18,13 @@ github: https://github.com/OpenFlutter/flutter_screenutil ...@@ -18,12 +18,13 @@ github: https://github.com/OpenFlutter/flutter_screenutil
### Adicionando a dependência: ### Adicionando a dependência:
Por favor, verifique a última versão antes da instalação. Por favor, verifique a última versão antes da instalação.
Se houver algum problema com a nova versão, use a versão anterior
``` ```
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
# add flutter_screenutil # add flutter_screenutil
flutter_screenutil: ^1.0.2 flutter_screenutil: ^2.0.0
``` ```
### Adicione o seguinte import em seu código Dart: ### Adicione o seguinte import em seu código Dart:
...@@ -48,13 +49,16 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej ...@@ -48,13 +49,16 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej
//Preencha o tamanho da tela do dispositivo no protótipo de design //Preencha o tamanho da tela do dispositivo no protótipo de design
//Valor padrão: width : 1080px , height:1920px , allowFontScaling:false //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false
ScreenUtil.init(context); ScreenUtil.init(context); //flutter_screenuitl < 1.2
ScreenUtil.init(); //flutter_screenuitl >= 1.2
//Se o design é baseado no iPhone6 ​​(iPhone6 ​​750*1334) //Se o design é baseado no iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(context, width: 750, height: 1334); ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
//Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema //Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
``` ```
...@@ -182,7 +186,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; ...@@ -182,7 +186,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
///Define o tamanho de ajuste (preenche o tamanho da tela do dispositivo no design). Se o design é baseado no tamanho do iPhone6 (iPhone6 ​​750*1334) ///Define o tamanho de ajuste (preenche o tamanho da tela do dispositivo no design). Se o design é baseado no tamanho do iPhone6 (iPhone6 ​​750*1334)
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
print('Largura do dispositivo:${ScreenUtil.screenWidth}'); //Largura do dispositivo print('Largura do dispositivo:${ScreenUtil.screenWidth}'); //Largura do dispositivo
print('Altura do dispositivo:${ScreenUtil.screenHeight}'); //Altura do dispositivo print('Altura do dispositivo:${ScreenUtil.screenHeight}'); //Altura do dispositivo
...@@ -235,11 +239,11 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; ...@@ -235,11 +239,11 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
), ),
], ],
), ),
Text('Largura do dispositivo:${ScreenUtil.screenWidth}px'), Text('Largura do dispositivo:${ScreenUtil.screenWidth}dp'),
Text('Altura do dispositivo:${ScreenUtil.screenHeight}px'), Text('Altura do dispositivo:${ScreenUtil.screenHeight}dp'),
Text('Densidade de pixels do dispositivo:${ScreenUtil.pixelRatio}'), Text('Densidade de pixels do dispositivo:${ScreenUtil.pixelRatio}'),
Text('Distância segura do rodapé:${ScreenUtil.bottomBarHeight}px'), Text('Distância segura do rodapé:${ScreenUtil.bottomBarHeight}dp'),
Text('Altura da status bar:${ScreenUtil.statusBarHeight}px'), Text('Altura da status bar:${ScreenUtil.statusBarHeight}dp'),
Text( Text(
'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}', 'Razão entre a largura atual e a largura do protótipo de design em pixels:${ScreenUtil().scaleWidth}',
textAlign: TextAlign.center, textAlign: TextAlign.center,
......
...@@ -2,6 +2,7 @@ buildscript { ...@@ -2,6 +2,7 @@ buildscript {
repositories { repositories {
google() google()
jcenter() jcenter()
maven { url "http://download.flutter.io" }
} }
dependencies { dependencies {
...@@ -13,6 +14,7 @@ allprojects { ...@@ -13,6 +14,7 @@ allprojects {
repositories { repositories {
google() google()
jcenter() jcenter()
maven { url "http://download.flutter.io" }
} }
} }
......
#!/bin/sh #!/bin/sh
# This is a generated file; do not edit or check into version control. # This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=D:\flutter" export "FLUTTER_ROOT=D:\Develop\flutter"
export "FLUTTER_APPLICATION_PATH=D:\Project\flutter_ScreenUtil\example" export "FLUTTER_APPLICATION_PATH=D:\Develop\Project\flutter_screenutil\example"
export "FLUTTER_TARGET=lib\main.dart" export "FLUTTER_TARGET=lib\main.dart"
export "FLUTTER_BUILD_DIR=build" export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build\ios" export "SYMROOT=${SOURCE_ROOT}/../build\ios"
export "FLUTTER_FRAMEWORK_DIR=D:\flutter\bin\cache\artifacts\engine\ios" export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
export "FLUTTER_FRAMEWORK_DIR=D:\Develop\flutter\bin\cache\artifacts\engine\ios"
export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1" export "FLUTTER_BUILD_NUMBER=1"
...@@ -26,7 +26,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -26,7 +26,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
//Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334) //Set the fit size (fill in the screen size of the device in the design) If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
return ExampleWidget(title: 'FlutterScreenUtil Demo'); return ExampleWidget(title: 'FlutterScreenUtil Demo');
} }
...@@ -81,10 +81,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -81,10 +81,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
), ),
], ],
), ),
Text('Device width:${ScreenUtil.screenWidth}px'), Text('Device width:${ScreenUtil.screenWidthPx}px'),
Text('Device height:${ScreenUtil.screenHeight}px'), Text('Device height:${ScreenUtil.screenHeightPx}px'),
Text('Device width:${ScreenUtil.screenWidthDp}dp'), Text('Device width:${ScreenUtil.screenWidth}dp'),
Text('Device height:${ScreenUtil.screenHeightDp}dp'), Text('Device height:${ScreenUtil.screenHeight}dp'),
Text('Device pixel density:${ScreenUtil.pixelRatio}'), Text('Device pixel density:${ScreenUtil.pixelRatio}'),
Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}dp'), Text('Bottom safe zone distance:${ScreenUtil.bottomBarHeight}dp'),
Text('Status bar height:${ScreenUtil.statusBarHeight}dp'), Text('Status bar height:${ScreenUtil.statusBarHeight}dp'),
...@@ -123,8 +123,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -123,8 +123,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: Icon(Icons.title), child: Icon(Icons.title),
onPressed: () { onPressed: () {
ScreenUtil.init(context, ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
width: 1500, height: 1334, allowFontScaling: false);
setState(() {}); setState(() {});
}, },
), ),
...@@ -132,8 +131,8 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -132,8 +131,8 @@ class _ExampleWidgetState extends State<ExampleWidget> {
} }
void printScreenInformation() { void printScreenInformation() {
print('Device width px:${ScreenUtil.screenWidth}'); //Device width print('Device width dp:${ScreenUtil.screenWidth}'); //Device width
print('Device height px:${ScreenUtil.screenHeight}'); //Device height print('Device height dp:${ScreenUtil.screenHeight}'); //Device height
print( print(
'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density 'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density
print( print(
...@@ -148,5 +147,6 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -148,5 +147,6 @@ class _ExampleWidgetState extends State<ExampleWidget> {
'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}'); 'The ratio of font and width to the size of the design:${ScreenUtil().scaleWidth * ScreenUtil.pixelRatio}');
print( print(
'The ratio of height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}'); 'The ratio of height width to the size of the design:${ScreenUtil().scaleHeight * ScreenUtil.pixelRatio}');
print('System font scaling:${ScreenUtil.textScaleFactor}');
} }
} }
...@@ -31,7 +31,7 @@ class _MyHomePageState extends State<MyHomePage> { ...@@ -31,7 +31,7 @@ class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
return ExampleWidget(title: 'FlutterScreenUtil示例'); return ExampleWidget(title: 'FlutterScreenUtil示例');
} }
...@@ -86,10 +86,10 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -86,10 +86,10 @@ class _ExampleWidgetState extends State<ExampleWidget> {
), ),
], ],
), ),
Text('设备宽度:${ScreenUtil.screenWidth}px'), Text('设备宽度:${ScreenUtil.screenWidthPx}px'),
Text('设备高度:${ScreenUtil.screenHeight}px'), Text('设备高度:${ScreenUtil.screenHeightPx}px'),
Text('设备宽度:${ScreenUtil.screenWidthDp}dp'), Text('设备宽度:${ScreenUtil.screenWidth}dp'),
Text('设备高度:${ScreenUtil.screenHeightDp}dp'), Text('设备高度:${ScreenUtil.screenHeight}dp'),
Text('设备的像素密度:${ScreenUtil.pixelRatio}'), Text('设备的像素密度:${ScreenUtil.pixelRatio}'),
Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'), Text('底部安全区距离:${ScreenUtil.bottomBarHeight}dp'),
Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'), Text('状态栏高度:${ScreenUtil.statusBarHeight}dp'),
...@@ -126,8 +126,7 @@ class _ExampleWidgetState extends State<ExampleWidget> { ...@@ -126,8 +126,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: Icon(Icons.title), child: Icon(Icons.title),
onPressed: () { onPressed: () {
ScreenUtil.init(context, ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
width: 1500, height: 1334, allowFontScaling: false);
setState(() {}); setState(() {});
}, },
), ),
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* email: zhuoyuan93@gmail.com * email: zhuoyuan93@gmail.com
*/ */
import 'package:flutter/material.dart'; import 'dart:ui';
class ScreenUtil { class ScreenUtil {
static ScreenUtil _instance; static ScreenUtil _instance;
...@@ -19,7 +19,6 @@ class ScreenUtil { ...@@ -19,7 +19,6 @@ class ScreenUtil {
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
bool allowFontScaling; bool allowFontScaling;
static MediaQueryData _mediaQueryData;
static double _screenWidth; static double _screenWidth;
static double _screenHeight; static double _screenHeight;
static double _pixelRatio; static double _pixelRatio;
...@@ -33,7 +32,7 @@ class ScreenUtil { ...@@ -33,7 +32,7 @@ class ScreenUtil {
return _instance; return _instance;
} }
static void init(BuildContext context, static void init(
{num width = defaultWidth, {num width = defaultWidth,
num height = defaultHeight, num height = defaultHeight,
bool allowFontScaling = false}) { bool allowFontScaling = false}) {
...@@ -43,19 +42,14 @@ class ScreenUtil { ...@@ -43,19 +42,14 @@ class ScreenUtil {
_instance.uiWidthPx = width; _instance.uiWidthPx = width;
_instance.uiHeightPx = height; _instance.uiHeightPx = height;
_instance.allowFontScaling = allowFontScaling; _instance.allowFontScaling = allowFontScaling;
_pixelRatio = window.devicePixelRatio;
MediaQueryData mediaQuery = MediaQuery.of(context); _screenWidth = window.physicalSize.width;
_mediaQueryData = mediaQuery; _screenHeight = window.physicalSize.height;
_pixelRatio = mediaQuery.devicePixelRatio; _statusBarHeight = window.padding.top;
_screenWidth = mediaQuery.size.width; _bottomBarHeight = window.padding.bottom;
_screenHeight = mediaQuery.size.height; _textScaleFactor = window.textScaleFactor;
_statusBarHeight = mediaQuery.padding.top;
_bottomBarHeight = _mediaQueryData.padding.bottom;
_textScaleFactor = mediaQuery.textScaleFactor;
} }
static MediaQueryData get mediaQueryData => _mediaQueryData;
/// 每个逻辑像素的字体像素数,字体的缩放比例 /// 每个逻辑像素的字体像素数,字体的缩放比例
/// The number of font pixels for each logical pixel. /// The number of font pixels for each logical pixel.
static double get textScaleFactor => _textScaleFactor; static double get textScaleFactor => _textScaleFactor;
...@@ -66,23 +60,27 @@ class ScreenUtil { ...@@ -66,23 +60,27 @@ class ScreenUtil {
/// 当前设备宽度 dp /// 当前设备宽度 dp
/// The horizontal extent of this size. /// The horizontal extent of this size.
static double get screenWidthDp => _screenWidth; static double get screenWidth => _screenWidth / _pixelRatio;
///当前设备高度 dp ///当前设备高度 dp
///The vertical extent of this size. dp ///The vertical extent of this size. dp
static double get screenHeightDp => _screenHeight; static double get screenHeight => _screenHeight / _pixelRatio;
/// 当前设备宽度 px /// 当前设备宽度 px
/// The vertical extent of this size. px /// The vertical extent of this size. px
static double get screenWidth => _screenWidth * _pixelRatio; static double get screenWidthPx => _screenWidth;
/// 当前设备高度 px /// 当前设备高度 px
/// The vertical extent of this size. px /// The vertical extent of this size. px
static double get screenHeight => _screenHeight * _pixelRatio; static double get screenHeightPx => _screenHeight;
/// 状态栏高度 dp 刘海屏会更高
/// The offset from the top
static double get statusBarHeight => _statusBarHeight / _pixelRatio;
/// 状态栏高度 dp 刘海屏会更高 /// 状态栏高度 dp 刘海屏会更高
/// The offset from the top /// The offset from the top
static double get statusBarHeight => _statusBarHeight; static double get statusBarHeightPx => _statusBarHeight;
/// 底部安全区距离 dp /// 底部安全区距离 dp
/// The offset from the bottom. /// The offset from the bottom.
...@@ -90,9 +88,9 @@ class ScreenUtil { ...@@ -90,9 +88,9 @@ class ScreenUtil {
/// 实际的dp与UI设计px的比例 /// 实际的dp与UI设计px的比例
/// The ratio of the actual dp to the design draft px /// The ratio of the actual dp to the design draft px
double get scaleWidth => _screenWidth / uiWidthPx; double get scaleWidth => screenWidth / uiWidthPx;
double get scaleHeight => _screenHeight / uiHeightPx; double get scaleHeight => screenHeight / uiHeightPx;
double get scaleText => scaleWidth; double get scaleText => scaleWidth;
......
name: flutter_screenutil name: flutter_screenutil
description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
version: 1.1.0 version: 2.0.0
homepage: https://github.com/OpenFlutter/flutter_screenutil homepage: https://github.com/OpenFlutter/flutter_screenutil
environment: environment:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册