From 108ef8dedaefb202224da20696ea1e71df4fee59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=8D=93=E5=8E=9F?= Date: Thu, 16 Jul 2020 20:10:44 +0800 Subject: [PATCH] 2.3 --- CHANGELOG.md | 3 ++ README.md | 12 ++--- README_CN.md | 12 ++--- README_PT.md | 12 ++--- example/android/gradle.properties | 2 + example/android/gradlew | 0 .../ios/Flutter/flutter_export_environment.sh | 11 ++-- example/lib/main.dart | 9 ++-- example/lib/main_zh.dart | 4 +- lib/screenutil.dart | 50 +++++++++---------- pubspec.yaml | 2 +- 11 files changed, 61 insertions(+), 56 deletions(-) mode change 100644 => 100755 example/android/gradlew mode change 100644 => 100755 example/ios/Flutter/flutter_export_environment.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 773647e..baa2ff1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ * @Description: Update log --> +# 2.3.0 +- We still need context to initialize, sorry. + # 2.2.0 - add 'wp','hp'. Get the height/width of the screen proportionally - For example: 0.5.wp : Half the width of the screen. diff --git a/README.md b/README.md index a50dccb..a7fee70 100644 --- a/README.md +++ b/README.md @@ -49,16 +49,16 @@ Be sure to set the page in the MaterialApp's home/initialRoute(ie the entry file //fill in the screen size of the device in the design //default value : width : 1080px , height:1920px , allowFontScaling:false -ScreenUtil.init(context); //flutter_screenuitl < 1.2 -ScreenUtil.init(); //flutter_screenuitl >= 1.2 +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) -ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2 -ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2 +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 -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 +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~~ ``` diff --git a/README_CN.md b/README_CN.md index a639e66..b77741e 100644 --- a/README_CN.md +++ b/README_CN.md @@ -53,16 +53,16 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; //填入设计稿中设备的屏幕尺寸 //默认 width : 1080px , height:1920px , allowFontScaling:false -ScreenUtil.init(context); //flutter_screenuitl < 1.2 -ScreenUtil.init(); //flutter_screenuitl >= 1.2 +ScreenUtil.init(context); ~~//flutter_screenuitl < 1.2~~ +~~ScreenUtil.init(); //flutter_screenuitl >= 1.2~~ //假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) -ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2 -ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2 +ScreenUtil.init(context, width: 750, height: 1334); ~~//flutter_screenuitl < 1.2~~ +~~ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2~~ //设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false -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 +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~~ ``` diff --git a/README_PT.md b/README_PT.md index ff10357..d643356 100644 --- a/README_PT.md +++ b/README_PT.md @@ -49,16 +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 //Valor padrão: width : 1080px , height:1920px , allowFontScaling:false -ScreenUtil.init(context); //flutter_screenuitl < 1.2 -ScreenUtil.init(); //flutter_screenuitl >= 1.2 +ScreenUtil.init(context); ~~//flutter_screenuitl < 1.2~~ +~~ScreenUtil.init(); //flutter_screenuitl >= 1.2~~ //Se o design é baseado no iPhone6 ​​(iPhone6 ​​750*1334) -ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2 -ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2 +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 -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 +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~~ ``` diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 7be3d8b..a5965ab 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,2 +1,4 @@ org.gradle.jvmargs=-Xmx1536M android.enableR8=true +android.useAndroidX=true +android.enableJetifier=true \ No newline at end of file diff --git a/example/android/gradlew b/example/android/gradlew old mode 100644 new mode 100755 diff --git a/example/ios/Flutter/flutter_export_environment.sh b/example/ios/Flutter/flutter_export_environment.sh old mode 100644 new mode 100755 index d1dce9b..3777c13 --- a/example/ios/Flutter/flutter_export_environment.sh +++ b/example/ios/Flutter/flutter_export_environment.sh @@ -1,11 +1,10 @@ #!/bin/sh # This is a generated file; do not edit or check into version control. -export "FLUTTER_ROOT=D:\Develop\flutter" -export "FLUTTER_APPLICATION_PATH=D:\Develop\Project\flutter_screenutil\example" -export "FLUTTER_TARGET=lib\main.dart" +export "FLUTTER_ROOT=/Users/lizhuoyuan/Development/flutter" +export "FLUTTER_APPLICATION_PATH=/Users/lizhuoyuan/Development/Project/flutter_screenutil/example" +export "FLUTTER_TARGET=lib/main.dart" export "FLUTTER_BUILD_DIR=build" -export "SYMROOT=${SOURCE_ROOT}/../build\ios" -export "OTHER_LDFLAGS=$(inherited) -framework Flutter" -export "FLUTTER_FRAMEWORK_DIR=D:\Develop\flutter\bin\cache\artifacts\engine\ios" +export "SYMROOT=${SOURCE_ROOT}/../build/ios" +export "FLUTTER_FRAMEWORK_DIR=/Users/lizhuoyuan/Development/flutter/bin/cache/artifacts/engine/ios" export "FLUTTER_BUILD_NAME=1.0.0" export "FLUTTER_BUILD_NUMBER=1" diff --git a/example/lib/main.dart b/example/lib/main.dart index 7a3fa97..772bb02 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -26,7 +28,7 @@ class _MyHomePageState extends State { @override 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) - ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false); + ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false); return ExampleWidget(title: 'FlutterScreenUtil Demo'); } @@ -44,7 +46,7 @@ class ExampleWidget extends StatefulWidget { class _ExampleWidgetState extends State { @override Widget build(BuildContext context) { - printScreenInformation(); + //printScreenInformation(); return Scaffold( appBar: AppBar( title: Text(widget.title), @@ -123,7 +125,8 @@ class _ExampleWidgetState extends State { floatingActionButton: FloatingActionButton( child: Icon(Icons.title), onPressed: () { - ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false); + ScreenUtil.init(context, + width: 1500, height: 1334, allowFontScaling: false); setState(() {}); }, ), diff --git a/example/lib/main_zh.dart b/example/lib/main_zh.dart index ce31bbc..6dfee6f 100644 --- a/example/lib/main_zh.dart +++ b/example/lib/main_zh.dart @@ -31,7 +31,7 @@ class _MyHomePageState extends State { Widget build(BuildContext context) { //设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334) - ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false); + ScreenUtil.init(context,width: 750, height: 1334, allowFontScaling: false); return ExampleWidget(title: 'FlutterScreenUtil示例'); } @@ -126,7 +126,7 @@ class _ExampleWidgetState extends State { floatingActionButton: FloatingActionButton( child: Icon(Icons.title), onPressed: () { - ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false); + ScreenUtil.init(context,width: 1500, height: 1334, allowFontScaling: false); setState(() {}); }, ), diff --git a/lib/screenutil.dart b/lib/screenutil.dart index 3ac8bde..1f855a2 100644 --- a/lib/screenutil.dart +++ b/lib/screenutil.dart @@ -3,7 +3,7 @@ * email: zhuoyuan93@gmail.com */ -import 'dart:ui'; +import 'package:flutter/material.dart'; class ScreenUtil { static ScreenUtil _instance; @@ -19,7 +19,7 @@ class ScreenUtil { /// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false. bool allowFontScaling; - static double _screenWidth; + static double _screenWidth; static double _screenHeight; static double _pixelRatio; static double _statusBarHeight; @@ -32,22 +32,24 @@ class ScreenUtil { return _instance; } - static void init( + static void init(BuildContext context, {num width = defaultWidth, - num height = defaultHeight, - bool allowFontScaling = false}) { + num height = defaultHeight, + bool allowFontScaling = false}) { if (_instance == null) { _instance = ScreenUtil._(); } _instance.uiWidthPx = width; _instance.uiHeightPx = height; _instance.allowFontScaling = allowFontScaling; - _pixelRatio = window.devicePixelRatio; - _screenWidth = window.physicalSize.width; - _screenHeight = window.physicalSize.height; - _statusBarHeight = window.padding.top; - _bottomBarHeight = window.padding.bottom; - _textScaleFactor = window.textScaleFactor; + + MediaQueryData mediaQuery = MediaQuery.of(context); + _pixelRatio = mediaQuery.devicePixelRatio; + _screenWidth = mediaQuery.size.width; + _screenHeight = mediaQuery.size.height; + _statusBarHeight = mediaQuery.padding.top; + _bottomBarHeight = mediaQuery.padding.bottom; + _textScaleFactor = mediaQuery.textScaleFactor; } /// 每个逻辑像素的字体像素数,字体的缩放比例 @@ -60,27 +62,23 @@ class ScreenUtil { /// 当前设备宽度 dp /// The horizontal extent of this size. - static double get screenWidth => _screenWidth / _pixelRatio; + static double get screenWidth => _screenWidth; ///当前设备高度 dp ///The vertical extent of this size. dp - static double get screenHeight => _screenHeight / _pixelRatio; + static double get screenHeight => _screenHeight; /// 当前设备宽度 px /// The vertical extent of this size. px - static double get screenWidthPx => _screenWidth; + static double get screenWidthPx => _screenWidth * _pixelRatio; /// 当前设备高度 px /// The vertical extent of this size. px - static double get screenHeightPx => _screenHeight; - - /// 状态栏高度 dp 刘海屏会更高 - /// The offset from the top - static double get statusBarHeight => _statusBarHeight / _pixelRatio; + static double get screenHeightPx => _screenHeight * _pixelRatio; /// 状态栏高度 dp 刘海屏会更高 /// The offset from the top - static double get statusBarHeightPx => _statusBarHeight; + static double get statusBarHeight => _statusBarHeight; /// 底部安全区距离 dp /// The offset from the bottom. @@ -88,9 +86,9 @@ class ScreenUtil { /// 实际的dp与UI设计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; @@ -119,9 +117,9 @@ class ScreenUtil { num setSp(num fontSize, {bool allowFontScalingSelf}) => allowFontScalingSelf == null ? (allowFontScaling - ? (fontSize * scaleText) - : ((fontSize * scaleText) / _textScaleFactor)) + ? (fontSize * scaleText) + : ((fontSize * scaleText) / _textScaleFactor)) : (allowFontScalingSelf - ? (fontSize * scaleText) - : ((fontSize * scaleText) / _textScaleFactor)); + ? (fontSize * scaleText) + : ((fontSize * scaleText) / _textScaleFactor)); } diff --git a/pubspec.yaml b/pubspec.yaml index 7908ff1..e7a4f7c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_screenutil description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models -version: 2.2.0 +version: 2.3.0 homepage: https://github.com/OpenFlutter/flutter_screenutil environment: