Skip to content

Commit 108ef8d

Browse files
author
李卓原
committed
2.3
1 parent caed30e commit 108ef8d

File tree

11 files changed

+61
-56
lines changed

11 files changed

+61
-56
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* @Description: Update log
77
-->
88

9+
# 2.3.0
10+
- We still need context to initialize, sorry.
11+
912
# 2.2.0
1013
- add 'wp','hp'. Get the height/width of the screen proportionally
1114
- For example: 0.5.wp : Half the width of the screen.

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ Be sure to set the page in the MaterialApp's home/initialRoute(ie the entry file
4949
//fill in the screen size of the device in the design
5050
5151
//default value : width : 1080px , height:1920px , allowFontScaling:false
52-
ScreenUtil.init(context); //flutter_screenuitl < 1.2
53-
ScreenUtil.init(); //flutter_screenuitl >= 1.2
52+
ScreenUtil.init(context); ~~//flutter_screenuitl < 1.2~~
53+
~~ScreenUtil.init(); //flutter_screenuitl >= 1.2~~
5454
5555
//If the design is based on the size of the iPhone6 ​​(iPhone6 ​​750*1334)
56-
ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
57-
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
56+
ScreenUtil.init(context, width: 750, height: 1334); ~~//flutter_screenuitl < 1.2~~
57+
~~ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2~~
5858
5959
//If you want to set the font size is scaled according to the system's "font size" assist option
60-
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
61-
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
60+
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ~~//flutter_screenuitl < 1.2~~
61+
~~ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2~~
6262
6363
```
6464

README_CN.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
5353
//填入设计稿中设备的屏幕尺寸
5454
5555
//默认 width : 1080px , height:1920px , allowFontScaling:false
56-
ScreenUtil.init(context); //flutter_screenuitl < 1.2
57-
ScreenUtil.init(); //flutter_screenuitl >= 1.2
56+
ScreenUtil.init(context); ~~//flutter_screenuitl < 1.2~~
57+
~~ScreenUtil.init(); //flutter_screenuitl >= 1.2~~
5858
5959
//假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
60-
ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
61-
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
60+
ScreenUtil.init(context, width: 750, height: 1334); ~~//flutter_screenuitl < 1.2~~
61+
~~ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2~~
6262
6363
//设置字体大小根据系统的“字体大小”辅助选项来进行缩放,默认为false
64-
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
65-
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
64+
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ~~//flutter_screenuitl < 1.2~~
65+
~~ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2~~
6666
6767
```
6868

README_PT.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ Certifique-se de definir as dimensões na paginal inicial do MaterialApp (ou sej
4949
//Preencha o tamanho da tela do dispositivo no protótipo de design
5050
5151
//Valor padrão: width : 1080px , height:1920px , allowFontScaling:false
52-
ScreenUtil.init(context); //flutter_screenuitl < 1.2
53-
ScreenUtil.init(); //flutter_screenuitl >= 1.2
52+
ScreenUtil.init(context); ~~//flutter_screenuitl < 1.2~~
53+
~~ScreenUtil.init(); //flutter_screenuitl >= 1.2~~
5454
5555
//Se o design é baseado no iPhone6 ​​(iPhone6 ​​750*1334)
56-
ScreenUtil.init(context, width: 750, height: 1334); //flutter_screenuitl < 1.2
57-
ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2
56+
ScreenUtil.init(context, width: 750, height: 1334); ~~//flutter_screenuitl < 1.2~~
57+
~~ScreenUtil.init(width: 750, height: 1334); //flutter_screenuitl >= 1.2~~
5858
5959
//Se você quer definir que o tamanho da fonte seja ajustado de acordo com a opção "tamanho da fonte" na acessibilidade do sistema
60-
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl < 1.2
61-
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2
60+
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: true); ~~//flutter_screenuitl < 1.2~~
61+
~~ScreenUtil.init(width: 750, height: 1334, allowFontScaling: true); //flutter_screenuitl >= 1.2~~
6262
6363
```
6464

example/android/gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
org.gradle.jvmargs=-Xmx1536M
22
android.enableR8=true
3+
android.useAndroidX=true
4+
android.enableJetifier=true

example/android/gradlew

100644100755
File mode changed.

example/ios/Flutter/flutter_export_environment.sh

100644100755
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/bin/sh
22
# This is a generated file; do not edit or check into version control.
3-
export "FLUTTER_ROOT=D:\Develop\flutter"
4-
export "FLUTTER_APPLICATION_PATH=D:\Develop\Project\flutter_screenutil\example"
5-
export "FLUTTER_TARGET=lib\main.dart"
3+
export "FLUTTER_ROOT=/Users/lizhuoyuan/Development/flutter"
4+
export "FLUTTER_APPLICATION_PATH=/Users/lizhuoyuan/Development/Project/flutter_screenutil/example"
5+
export "FLUTTER_TARGET=lib/main.dart"
66
export "FLUTTER_BUILD_DIR=build"
7-
export "SYMROOT=${SOURCE_ROOT}/../build\ios"
8-
export "OTHER_LDFLAGS=$(inherited) -framework Flutter"
9-
export "FLUTTER_FRAMEWORK_DIR=D:\Develop\flutter\bin\cache\artifacts\engine\ios"
7+
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
8+
export "FLUTTER_FRAMEWORK_DIR=/Users/lizhuoyuan/Development/flutter/bin/cache/artifacts/engine/ios"
109
export "FLUTTER_BUILD_NAME=1.0.0"
1110
export "FLUTTER_BUILD_NUMBER=1"

example/lib/main.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:ui';
2+
13
import 'package:flutter/material.dart';
24
import 'package:flutter_screenutil/flutter_screenutil.dart';
35

@@ -26,7 +28,7 @@ class _MyHomePageState extends State<MyHomePage> {
2628
@override
2729
Widget build(BuildContext context) {
2830
//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)
29-
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
31+
ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
3032

3133
return ExampleWidget(title: 'FlutterScreenUtil Demo');
3234
}
@@ -44,7 +46,7 @@ class ExampleWidget extends StatefulWidget {
4446
class _ExampleWidgetState extends State<ExampleWidget> {
4547
@override
4648
Widget build(BuildContext context) {
47-
printScreenInformation();
49+
//printScreenInformation();
4850
return Scaffold(
4951
appBar: AppBar(
5052
title: Text(widget.title),
@@ -123,7 +125,8 @@ class _ExampleWidgetState extends State<ExampleWidget> {
123125
floatingActionButton: FloatingActionButton(
124126
child: Icon(Icons.title),
125127
onPressed: () {
126-
ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
128+
ScreenUtil.init(context,
129+
width: 1500, height: 1334, allowFontScaling: false);
127130
setState(() {});
128131
},
129132
),

example/lib/main_zh.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class _MyHomePageState extends State<MyHomePage> {
3131
Widget build(BuildContext context) {
3232
//设置适配尺寸 (填入设计稿中设备的屏幕尺寸) 此处假如设计稿是按iPhone6的尺寸设计的(iPhone6 750*1334)
3333

34-
ScreenUtil.init(width: 750, height: 1334, allowFontScaling: false);
34+
ScreenUtil.init(context,width: 750, height: 1334, allowFontScaling: false);
3535

3636
return ExampleWidget(title: 'FlutterScreenUtil示例');
3737
}
@@ -126,7 +126,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
126126
floatingActionButton: FloatingActionButton(
127127
child: Icon(Icons.title),
128128
onPressed: () {
129-
ScreenUtil.init(width: 1500, height: 1334, allowFontScaling: false);
129+
ScreenUtil.init(context,width: 1500, height: 1334, allowFontScaling: false);
130130
setState(() {});
131131
},
132132
),

lib/screenutil.dart

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
*/
55

6-
import 'dart:ui';
6+
import 'package:flutter/material.dart';
77

88
class ScreenUtil {
99
static ScreenUtil _instance;
@@ -19,7 +19,7 @@ class ScreenUtil {
1919
/// allowFontScaling Specifies whether fonts should scale to respect Text Size accessibility settings. The default is false.
2020
bool allowFontScaling;
2121

22-
static double _screenWidth;
22+
static double _screenWidth;
2323
static double _screenHeight;
2424
static double _pixelRatio;
2525
static double _statusBarHeight;
@@ -32,22 +32,24 @@ class ScreenUtil {
3232
return _instance;
3333
}
3434

35-
static void init(
35+
static void init(BuildContext context,
3636
{num width = defaultWidth,
37-
num height = defaultHeight,
38-
bool allowFontScaling = false}) {
37+
num height = defaultHeight,
38+
bool allowFontScaling = false}) {
3939
if (_instance == null) {
4040
_instance = ScreenUtil._();
4141
}
4242
_instance.uiWidthPx = width;
4343
_instance.uiHeightPx = height;
4444
_instance.allowFontScaling = allowFontScaling;
45-
_pixelRatio = window.devicePixelRatio;
46-
_screenWidth = window.physicalSize.width;
47-
_screenHeight = window.physicalSize.height;
48-
_statusBarHeight = window.padding.top;
49-
_bottomBarHeight = window.padding.bottom;
50-
_textScaleFactor = window.textScaleFactor;
45+
46+
MediaQueryData mediaQuery = MediaQuery.of(context);
47+
_pixelRatio = mediaQuery.devicePixelRatio;
48+
_screenWidth = mediaQuery.size.width;
49+
_screenHeight = mediaQuery.size.height;
50+
_statusBarHeight = mediaQuery.padding.top;
51+
_bottomBarHeight = mediaQuery.padding.bottom;
52+
_textScaleFactor = mediaQuery.textScaleFactor;
5153
}
5254

5355
/// 每个逻辑像素的字体像素数,字体的缩放比例
@@ -60,37 +62,33 @@ class ScreenUtil {
6062

6163
/// 当前设备宽度 dp
6264
/// The horizontal extent of this size.
63-
static double get screenWidth => _screenWidth / _pixelRatio;
65+
static double get screenWidth => _screenWidth;
6466

6567
///当前设备高度 dp
6668
///The vertical extent of this size. dp
67-
static double get screenHeight => _screenHeight / _pixelRatio;
69+
static double get screenHeight => _screenHeight;
6870

6971
/// 当前设备宽度 px
7072
/// The vertical extent of this size. px
71-
static double get screenWidthPx => _screenWidth;
73+
static double get screenWidthPx => _screenWidth * _pixelRatio;
7274

7375
/// 当前设备高度 px
7476
/// The vertical extent of this size. px
75-
static double get screenHeightPx => _screenHeight;
76-
77-
/// 状态栏高度 dp 刘海屏会更高
78-
/// The offset from the top
79-
static double get statusBarHeight => _statusBarHeight / _pixelRatio;
77+
static double get screenHeightPx => _screenHeight * _pixelRatio;
8078

8179
/// 状态栏高度 dp 刘海屏会更高
8280
/// The offset from the top
83-
static double get statusBarHeightPx => _statusBarHeight;
81+
static double get statusBarHeight => _statusBarHeight;
8482

8583
/// 底部安全区距离 dp
8684
/// The offset from the bottom.
8785
static double get bottomBarHeight => _bottomBarHeight;
8886

8987
/// 实际的dp与UI设计px的比例
9088
/// The ratio of the actual dp to the design draft px
91-
double get scaleWidth => screenWidth / uiWidthPx;
89+
double get scaleWidth => _screenWidth / uiWidthPx;
9290

93-
double get scaleHeight => screenHeight / uiHeightPx;
91+
double get scaleHeight => _screenHeight / uiHeightPx;
9492

9593
double get scaleText => scaleWidth;
9694

@@ -119,9 +117,9 @@ class ScreenUtil {
119117
num setSp(num fontSize, {bool allowFontScalingSelf}) =>
120118
allowFontScalingSelf == null
121119
? (allowFontScaling
122-
? (fontSize * scaleText)
123-
: ((fontSize * scaleText) / _textScaleFactor))
120+
? (fontSize * scaleText)
121+
: ((fontSize * scaleText) / _textScaleFactor))
124122
: (allowFontScalingSelf
125-
? (fontSize * scaleText)
126-
: ((fontSize * scaleText) / _textScaleFactor));
123+
? (fontSize * scaleText)
124+
: ((fontSize * scaleText) / _textScaleFactor));
127125
}

0 commit comments

Comments
 (0)