Showing
2 changed files
with
9 additions
and
5 deletions
| @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget { | @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget { | ||
| 29 | child: widget, | 29 | child: widget, |
| 30 | ); | 30 | ); |
| 31 | }, | 31 | }, |
| 32 | - home: HomePage(title: 'FlutterScreenUtil Demo'), | 32 | + home: const HomePage(title: 'FlutterScreenUtil Demo'), |
| 33 | ), | 33 | ), |
| 34 | ); | 34 | ); |
| 35 | } | 35 | } |
| @@ -63,19 +63,23 @@ class ScreenUtil { | @@ -63,19 +63,23 @@ class ScreenUtil { | ||
| 63 | 63 | ||
| 64 | /// 当前设备宽度 dp | 64 | /// 当前设备宽度 dp |
| 65 | /// The horizontal extent of this size. | 65 | /// The horizontal extent of this size. |
| 66 | - double get screenWidth => _screenWidth; | 66 | + double get screenWidth => |
| 67 | + context != null ? MediaQuery.of(context!).size.width : _screenWidth; | ||
| 67 | 68 | ||
| 68 | ///当前设备高度 dp | 69 | ///当前设备高度 dp |
| 69 | ///The vertical extent of this size. dp | 70 | ///The vertical extent of this size. dp |
| 70 | - double get screenHeight => _screenHeight; | 71 | + double get screenHeight => |
| 72 | + context != null ? MediaQuery.of(context!).size.height : _screenHeight; | ||
| 71 | 73 | ||
| 72 | /// 状态栏高度 dp 刘海屏会更高 | 74 | /// 状态栏高度 dp 刘海屏会更高 |
| 73 | /// The offset from the top, in dp | 75 | /// The offset from the top, in dp |
| 74 | - double get statusBarHeight => MediaQuery.of(context!).padding.top; | 76 | + double get statusBarHeight => |
| 77 | + context == null ? 0 : MediaQuery.of(context!).padding.top; | ||
| 75 | 78 | ||
| 76 | /// 底部安全区距离 dp | 79 | /// 底部安全区距离 dp |
| 77 | /// The offset from the bottom, in dp | 80 | /// The offset from the bottom, in dp |
| 78 | - double get bottomBarHeight => MediaQuery.of(context!).padding.bottom; | 81 | + double get bottomBarHeight => |
| 82 | + context == null ? 0 : MediaQuery.of(context!).padding.bottom; | ||
| 79 | 83 | ||
| 80 | /// 实际尺寸与UI设计的比例 | 84 | /// 实际尺寸与UI设计的比例 |
| 81 | /// The ratio of actual width to UI design | 85 | /// The ratio of actual width to UI design |
-
Please register or login to post a comment