李卓原

fix #310

  1 +# 5.0.4
  2 +- add setContext() , the first initialization method requires calling
  3 +- fix # 310
  4 +
1 # 5.0.3 5 # 5.0.3
2 6
3 - init method add "context" param 7 - init method add "context" param
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
2 import 'package:flutter_screenutil/flutter_screenutil.dart'; 2 import 'package:flutter_screenutil/flutter_screenutil.dart';
3 3
4 void main() { 4 void main() {
5 - WidgetsFlutterBinding.ensureInitialized();  
6 runApp(MyApp()); 5 runApp(MyApp());
7 } 6 }
8 7
@@ -22,6 +21,7 @@ class MyApp extends StatelessWidget { @@ -22,6 +21,7 @@ class MyApp extends StatelessWidget {
22 textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)), 21 textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
23 ), 22 ),
24 builder: (context, widget) { 23 builder: (context, widget) {
  24 + ScreenUtil.setContext(context);
25 return MediaQuery( 25 return MediaQuery(
26 //Setting font does not change with system font size 26 //Setting font does not change with system font size
27 data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0), 27 data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
@@ -15,13 +15,10 @@ class ScreenUtil { @@ -15,13 +15,10 @@ class ScreenUtil {
15 ///屏幕方向 15 ///屏幕方向
16 late Orientation _orientation; 16 late Orientation _orientation;
17 17
18 - late double _pixelRatio;  
19 - late double _textScaleFactor;  
20 late double _screenWidth; 18 late double _screenWidth;
21 late double _screenHeight; 19 late double _screenHeight;
22 - late double _statusBarHeight;  
23 - late double _bottomBarHeight;  
24 late bool _minTextAdapt; 20 late bool _minTextAdapt;
  21 + late BuildContext? context;
25 22
26 ScreenUtil._(); 23 ScreenUtil._();
27 24
@@ -29,6 +26,10 @@ class ScreenUtil { @@ -29,6 +26,10 @@ class ScreenUtil {
29 return _instance; 26 return _instance;
30 } 27 }
31 28
  29 + static void setContext(BuildContext context) {
  30 + _instance.context = context;
  31 + }
  32 +
32 static void init( 33 static void init(
33 BoxConstraints constraints, { 34 BoxConstraints constraints, {
34 BuildContext? context, 35 BuildContext? context,
@@ -45,17 +46,7 @@ class ScreenUtil { @@ -45,17 +46,7 @@ class ScreenUtil {
45 .._screenHeight = splitScreenMode 46 .._screenHeight = splitScreenMode
46 ? max(constraints.maxHeight, 700) 47 ? max(constraints.maxHeight, 700)
47 : constraints.maxHeight; 48 : constraints.maxHeight;
48 -  
49 - var windowData;  
50 - if (context != null) {  
51 - windowData = MediaQuery.of(context);  
52 - } else {  
53 - windowData = WidgetsBinding.instance?.window ?? ui.window;  
54 - }  
55 - _instance._pixelRatio = windowData.devicePixelRatio;  
56 - _instance._statusBarHeight = windowData.padding.top;  
57 - _instance._bottomBarHeight = windowData.padding.bottom;  
58 - _instance._textScaleFactor = windowData.textScaleFactor; 49 + if (context != null) setContext(context);
59 } 50 }
60 51
61 ///获取屏幕方向 52 ///获取屏幕方向
@@ -64,11 +55,11 @@ class ScreenUtil { @@ -64,11 +55,11 @@ class ScreenUtil {
64 55
65 /// 每个逻辑像素的字体像素数,字体的缩放比例 56 /// 每个逻辑像素的字体像素数,字体的缩放比例
66 /// The number of font pixels for each logical pixel. 57 /// The number of font pixels for each logical pixel.
67 - double get textScaleFactor => _textScaleFactor; 58 + double get textScaleFactor => MediaQuery.of(context!).textScaleFactor;
68 59
69 /// 设备的像素密度 60 /// 设备的像素密度
70 /// The size of the media in logical pixels (e.g, the size of the screen). 61 /// The size of the media in logical pixels (e.g, the size of the screen).
71 - double get pixelRatio => _pixelRatio; 62 + double? get pixelRatio => MediaQuery.of(context!).devicePixelRatio;
72 63
73 /// 当前设备宽度 dp 64 /// 当前设备宽度 dp
74 /// The horizontal extent of this size. 65 /// The horizontal extent of this size.
@@ -80,11 +71,11 @@ class ScreenUtil { @@ -80,11 +71,11 @@ class ScreenUtil {
80 71
81 /// 状态栏高度 dp 刘海屏会更高 72 /// 状态栏高度 dp 刘海屏会更高
82 /// The offset from the top, in dp 73 /// The offset from the top, in dp
83 - double get statusBarHeight => _statusBarHeight / _pixelRatio; 74 + double get statusBarHeight => MediaQuery.of(context!).padding.top;
84 75
85 /// 底部安全区距离 dp 76 /// 底部安全区距离 dp
86 /// The offset from the bottom, in dp 77 /// The offset from the bottom, in dp
87 - double get bottomBarHeight => _bottomBarHeight / _pixelRatio; 78 + double get bottomBarHeight => MediaQuery.of(context!).padding.bottom;
88 79
89 /// 实际尺寸与UI设计的比例 80 /// 实际尺寸与UI设计的比例
90 /// The ratio of actual width to UI design 81 /// The ratio of actual width to UI design
@@ -19,24 +19,21 @@ class ScreenUtilInit extends StatelessWidget { @@ -19,24 +19,21 @@ class ScreenUtilInit extends StatelessWidget {
19 19
20 @override 20 @override
21 Widget build(BuildContext context) { 21 Widget build(BuildContext context) {
22 - return MediaQuery(  
23 - data: MediaQueryData(),  
24 - child: LayoutBuilder(builder: (_, BoxConstraints constraints) {  
25 - if (constraints.maxWidth != 0) {  
26 - final Orientation orientation =  
27 - constraints.maxWidth > constraints.maxHeight  
28 - ? Orientation.landscape  
29 - : Orientation.portrait;  
30 - ScreenUtil.init(constraints,  
31 - context: _,  
32 - orientation: orientation,  
33 - designSize: designSize,  
34 - splitScreenMode: splitScreenMode,  
35 - minTextAdapt: minTextAdapt);  
36 - return builder();  
37 - }  
38 - return Container();  
39 - }),  
40 - ); 22 + return LayoutBuilder(builder: (_, BoxConstraints constraints) {
  23 + if (constraints.maxWidth != 0) {
  24 + final Orientation orientation =
  25 + constraints.maxWidth > constraints.maxHeight
  26 + ? Orientation.landscape
  27 + : Orientation.portrait;
  28 + ScreenUtil.init(constraints,
  29 + context: _,
  30 + orientation: orientation,
  31 + designSize: designSize,
  32 + splitScreenMode: splitScreenMode,
  33 + minTextAdapt: minTextAdapt);
  34 + return builder();
  35 + }
  36 + return Container();
  37 + });
41 } 38 }
42 } 39 }
1 name: flutter_screenutil 1 name: flutter_screenutil
2 description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models 2 description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
3 -version: 5.0.3 3 +version: 5.0.4
4 homepage: https://github.com/OpenFlutter/flutter_screenutil 4 homepage: https://github.com/OpenFlutter/flutter_screenutil
  5 +publish_to: https://pub.dev
5 6
6 environment: 7 environment:
7 sdk: ">=2.12.0 <3.0.0" 8 sdk: ">=2.12.0 <3.0.0"