Comments (8)
希望大佬查看下原因,完善下。PS:ScreenUtil还是很好用的
from flutter_screenutil.
我也遇到类似的问题
运行官方这段代码
print('Device width:${ScreenUtil.screenWidth}'); //Device width
print('Device height:${ScreenUtil.screenHeight}'); //Device height
print(
'Device pixel density:${ScreenUtil.pixelRatio}'); //Device pixel density
print(
'Bottom safe zone distance:${ScreenUtil.bottomBarHeight}'); //Bottom safe zone distance,suitable for buttons with full screen
print(
'Status bar height:${ScreenUtil.statusBarHeight}px'); //Status bar height , Notch will be higher Unit px
print(
'Ratio of actual width dp to design draft px:${ScreenUtil.getInstance().scaleWidth}');
print(
'Ratio of actual height dp to design draft px:${ScreenUtil.getInstance().scaleHeight}');
print(
'The ratio of font and width to the size of the design:${ScreenUtil.getInstance().scaleWidth * ScreenUtil.pixelRatio}');
print(
'The ratio of height width to the size of the design:${ScreenUtil.getInstance().scaleHeight * ScreenUtil.pixelRatio}');
如果 flutter run ,结果:
I/flutter (13647): Device width:1080.0
I/flutter (13647): Device height:1919.9999999999998
I/flutter (13647): Device pixel density:2.75
I/flutter (13647): Bottom safe zone distance:0.0
I/flutter (13647): Status bar height:66.0px
I/flutter (13647): Ratio of actual width dp to design draft px:0.5236363636363637
I/flutter (13647): Ratio of actual height dp to design draft px:0.5233746762982144
I/flutter (13647): The ratio of font and width to the size of the design:1.4400000000000002
I/flutter (13647): The ratio of height width to the size of the design:1.4392803598200896
但是 flutter fun --profile / --release 结果:
I/flutter (12960): Device width:0.0
I/flutter (12960): Device height:0.0
I/flutter (12960): Device pixel density:1.0
I/flutter (12960): Bottom safe zone distance:0.0
I/flutter (12960): Status bar height:0.0px
I/flutter (12960): Ratio of actual width dp to design draft px:0.0
I/flutter (12960): Ratio of actual height dp to design draft px:0.0
I/flutter (12960): The ratio of font and width to the size of the design:0.0
I/flutter (12960): The ratio of height width to the size of the design:0.0
from flutter_screenutil.
已知
稍后看一下原因
from flutter_screenutil.
我试了一下打开了我的flutter_screenutil项目
在命令行切换到example目录下
然后运行了一下: flutter run --profile
time是打印的当前时间 , 可以看到是极短时间内执行了两次build方法
flutter run --release
结果也是一样的,
ui上面显示也都是正常的
后面我再看看
之所以打印两次 应该是因为第一次的时候, 系统还没有获取到屏幕参数 ,
后来获取到屏幕参数之后是又运行了一次
from flutter_screenutil.
存在同样的问题.
调试模式一切正常, 打包之后, 发现有个页面文字不显示(用 SU设置了字体大小), 把字体大小改为直接设置就正常了.
from flutter_screenutil.
比较奇怪的是, 我几乎每个页面都用到了SU, 但只有一个页面出问题..
from flutter_screenutil.
为了测试获取屏幕尺寸为0的问题,就进行下面的测试:
我的启动页面,在启动的时候获取不到屏幕的尺寸,跳转到第二个页面才可以获取到屏幕尺寸。(有些机型不会出现这个问题)
原因是打开启动页面时,ScreenUtil 初始化时,获取的屏幕尺寸都是0;在第二个页面,ScreenUtil再次调用初始化函数,这时就可以获取到屏幕的尺寸。
官方的解析是,需要监听页面的屏幕尺寸变化,
class MetricsReactor extends StatefulWidget {
const MetricsReactor({ Key key }) : super(key: key);
@OverRide
_MetricsReactorState createState() => _MetricsReactorState();
}
class _MetricsReactorState extends State with WidgetsBindingObserver {
@OverRide
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@OverRide
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
Size _lastSize;
@OverRide
void didChangeMetrics() {
setState(() {
_lastSize = WidgetsBinding.instance.window.physicalSize;
// 在这里可以进行屏幕尺寸的初始化
});
}
@OverRide
Widget build(BuildContext context) {
return Text('Current size: $_lastSize');
}
}
from flutter_screenutil.
@hzysunrain 我试过在didChangeMetrics中初始化ScreenUtil , 但是它的触发时间晚于build (在我尝试的几次)
哪怕我在Myapp中的didChangeMetrics都要在home的build方法之后才调用
比较理想的就是在home(一般作为启动页面)中初始化 , 但是不使用ScreenUtil(避免特殊情况拿不到屏幕数据), 在启动页的下一个页面应该就可以直接使用了
from flutter_screenutil.
Related Issues (20)
- How to display well on different devices with different ratio? HOT 2
- Landscape mode, font size increases causing overflow HOT 5
- BorderRadiusExtension and RadiusExtension named wrong HOT 2
- some text widgets in build mode is missing HOT 15
- i update to flutter 3.19.5 the package dosen't work any more HOT 7
- ScreenUtils is best Utils for Flutter developer. HOT 1
- 5.9.0 oppo colorOS 14.0 Android14列表item有时会空白 HOT 3
- flutter_screenutil 5.9.0, Android phones can run with the screen turned off and wiget will be lost HOT 2
- 关于我使用ScreenUtilInit这种方式初始化,builder会调用两次,导致跳转页面会多生成一个 HOT 7
- u
- 横竖屏切换布局错乱 HOT 13
- Problem rendering in tablet size HOT 1
- 新版本 v5.9.2 以及以上版本,输入框无法正常弹出? HOT 2
- UI crashes in release mode HOT 2
- [Feature/Enhancement] Request for max and min screen size
- [Feature/Enhancement] Does not take the MediaQueryData of the parent
- Containers have extra space on bottom in ios
- flutter_screenutil 6.0.0-alpha.1 HOT 1
- Provide a concise title for your issue, e.g., "Error: The getter 'backgroundColor' isn't defined for the class 'ThemeData'".
- Integration with device_preview to correctly update MediaQueryData
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_screenutil.