李卓原

fix #310

# 5.0.4
- add setContext() , the first initialization method requires calling
- fix # 310
# 5.0.3
- init method add "context" param
... ...
... ... @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
... ... @@ -22,6 +21,7 @@ class MyApp extends StatelessWidget {
textTheme: TextTheme(button: TextStyle(fontSize: 45.sp)),
),
builder: (context, widget) {
ScreenUtil.setContext(context);
return MediaQuery(
//Setting font does not change with system font size
data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
... ...
... ... @@ -15,13 +15,10 @@ class ScreenUtil {
///屏幕方向
late Orientation _orientation;
late double _pixelRatio;
late double _textScaleFactor;
late double _screenWidth;
late double _screenHeight;
late double _statusBarHeight;
late double _bottomBarHeight;
late bool _minTextAdapt;
late BuildContext? context;
ScreenUtil._();
... ... @@ -29,6 +26,10 @@ class ScreenUtil {
return _instance;
}
static void setContext(BuildContext context) {
_instance.context = context;
}
static void init(
BoxConstraints constraints, {
BuildContext? context,
... ... @@ -45,17 +46,7 @@ class ScreenUtil {
.._screenHeight = splitScreenMode
? max(constraints.maxHeight, 700)
: constraints.maxHeight;
var windowData;
if (context != null) {
windowData = MediaQuery.of(context);
} else {
windowData = WidgetsBinding.instance?.window ?? ui.window;
}
_instance._pixelRatio = windowData.devicePixelRatio;
_instance._statusBarHeight = windowData.padding.top;
_instance._bottomBarHeight = windowData.padding.bottom;
_instance._textScaleFactor = windowData.textScaleFactor;
if (context != null) setContext(context);
}
///获取屏幕方向
... ... @@ -64,11 +55,11 @@ class ScreenUtil {
/// 每个逻辑像素的字体像素数,字体的缩放比例
/// The number of font pixels for each logical pixel.
double get textScaleFactor => _textScaleFactor;
double get textScaleFactor => MediaQuery.of(context!).textScaleFactor;
/// 设备的像素密度
/// The size of the media in logical pixels (e.g, the size of the screen).
double get pixelRatio => _pixelRatio;
double? get pixelRatio => MediaQuery.of(context!).devicePixelRatio;
/// 当前设备宽度 dp
/// The horizontal extent of this size.
... ... @@ -80,11 +71,11 @@ class ScreenUtil {
/// 状态栏高度 dp 刘海屏会更高
/// The offset from the top, in dp
double get statusBarHeight => _statusBarHeight / _pixelRatio;
double get statusBarHeight => MediaQuery.of(context!).padding.top;
/// 底部安全区距离 dp
/// The offset from the bottom, in dp
double get bottomBarHeight => _bottomBarHeight / _pixelRatio;
double get bottomBarHeight => MediaQuery.of(context!).padding.bottom;
/// 实际尺寸与UI设计的比例
/// The ratio of actual width to UI design
... ...
... ... @@ -19,24 +19,21 @@ class ScreenUtilInit extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MediaQuery(
data: MediaQueryData(),
child: LayoutBuilder(builder: (_, BoxConstraints constraints) {
if (constraints.maxWidth != 0) {
final Orientation orientation =
constraints.maxWidth > constraints.maxHeight
? Orientation.landscape
: Orientation.portrait;
ScreenUtil.init(constraints,
context: _,
orientation: orientation,
designSize: designSize,
splitScreenMode: splitScreenMode,
minTextAdapt: minTextAdapt);
return builder();
}
return Container();
}),
);
return LayoutBuilder(builder: (_, BoxConstraints constraints) {
if (constraints.maxWidth != 0) {
final Orientation orientation =
constraints.maxWidth > constraints.maxHeight
? Orientation.landscape
: Orientation.portrait;
ScreenUtil.init(constraints,
context: _,
orientation: orientation,
designSize: designSize,
splitScreenMode: splitScreenMode,
minTextAdapt: minTextAdapt);
return builder();
}
return Container();
});
}
}
... ...
name: flutter_screenutil
description: A flutter plugin for adapting screen and font size.Guaranteed to look good on different models
version: 5.0.3
version: 5.0.4
homepage: https://github.com/OpenFlutter/flutter_screenutil
publish_to: https://pub.dev
environment:
sdk: ">=2.12.0 <3.0.0"
... ...