Code Monkey home page Code Monkey logo

Comments (8)

zhyhub avatar zhyhub commented on July 20, 2024

希望大佬查看下原因,完善下。PS:ScreenUtil还是很好用的

from flutter_screenutil.

uljjmhn520 avatar uljjmhn520 commented on July 20, 2024

我也遇到类似的问题

运行官方这段代码

    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.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

已知
稍后看一下原因

from flutter_screenutil.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

我试了一下打开了我的flutter_screenutil项目
在命令行切换到example目录下
然后运行了一下: flutter run --profile
time是打印的当前时间 , 可以看到是极短时间内执行了两次build方法
image

flutter run --release 

结果也是一样的,
ui上面显示也都是正常的
后面我再看看

之所以打印两次 应该是因为第一次的时候, 系统还没有获取到屏幕参数 ,
后来获取到屏幕参数之后是又运行了一次

from flutter_screenutil.

weir008 avatar weir008 commented on July 20, 2024

存在同样的问题.
调试模式一切正常, 打包之后, 发现有个页面文字不显示(用 SU设置了字体大小), 把字体大小改为直接设置就正常了.

from flutter_screenutil.

weir008 avatar weir008 commented on July 20, 2024

比较奇怪的是, 我几乎每个页面都用到了SU, 但只有一个页面出问题..

from flutter_screenutil.

hzysunrain avatar hzysunrain commented on July 20, 2024

为了测试获取屏幕尺寸为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.

lizhuoyuan avatar lizhuoyuan commented on July 20, 2024

@hzysunrain 我试过在didChangeMetrics中初始化ScreenUtil , 但是它的触发时间晚于build (在我尝试的几次)
哪怕我在Myapp中的didChangeMetrics都要在home的build方法之后才调用

比较理想的就是在home(一般作为启动页面)中初始化 , 但是不使用ScreenUtil(避免特殊情况拿不到屏幕数据), 在启动页的下一个页面应该就可以直接使用了

from flutter_screenutil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.