Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate BuildContext in Utils.getScreenInfo() #88

Merged
merged 2 commits into from
Jul 23, 2024
Merged

deprecate BuildContext in Utils.getScreenInfo() #88

merged 2 commits into from
Jul 23, 2024

Conversation

WhiteXero
Copy link
Contributor

弃用 MediaQuery.of(context).devicePixelRatio;
MediaQueryData.fromView().devicePixelRatio; 代替

@Predidit
Copy link
Owner

您在windows上实际测试过这个方法吗。

这个方法一样无法在窗体未初始化时使用,并且似乎它在返回窗体分辨率而不是屏幕分辨率。

@Predidit
Copy link
Owner

我希望在 main.dart 中 windows_manager 相关方法被调用之前获取屏幕信息,当分辨率小于等于 1080P 时使用 800600 的窗体,而非 1280800 。

这将解决大部分问题,但是现在的函数似乎没有办法在这种场景下发挥作用。

或者就这一场景,您有什么不同的解决方法吗。

@WhiteXero
Copy link
Contributor Author

您在windows上实际测试过这个方法吗。

这个方法一样无法在窗体未初始化时使用,并且似乎它在返回窗体分辨率而不是屏幕分辨率。

返回的分辨率应当就是屏幕分辨率,因为他和mediaquery无关。

我把他放在了run()之前。是可以执行的。不过这个函数应该要求了 Widgetsbiding.ensureinitialized()。

如果要求在未初始化就能使用那可能没有太好的办法。
稍后我再进行测试

@WhiteXero
Copy link
Contributor Author

您在windows上实际测试过这个方法吗。

这个方法一样无法在窗体未初始化时使用,并且似乎它在返回窗体分辨率而不是屏幕分辨率。

经过测试,这个方法有效

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Map<String, double>? screenInfo = await Utils.getScreenInfo(); // 调用 getScreenInfo()
  debugPrint('Called getScreenInfo'); // 控制台信息提示已调用
  debugPrint('W: ${screenInfo?['width']} H: ${screenInfo?['height']} R: ${screenInfo?['ratio']}'); // 控制台返回分辨率信息
  double? width = screenInfo?['width'];
  double? height = screenInfo?['height'];
  if (Utils.isDesktop()) {
    await windowManager.ensureInitialized(); // 调用 window_manager
    debugPrint('Called windowManager'); // 控制台信息提示已调用
    debugPrint('SET WindowSize = $width x $height'); // 控制台提示设定窗口大小
    WindowOptions windowOptions = WindowOptions(
      size: Size(width ?? 1280, height ?? 960), // 用获取的屏幕分辨率大小替换原大小,作为对比
      center: true,
...

我希望在 main.dart 中 windows_manager 相关方法被调用之前获取屏幕信息,当分辨率小于等于 1080P 时使用 800600 的窗体,而非 1280800 。

我修改了 main()await windowManager.ensureInitialized(); 之前的部分,让窗口大小被设定为了获取到的系统分辨率(1920x1080),而不是1280x860以作为对比,看起来工作良好。

您可以查看 此视频(Youtube) 中的控制台输出信息以更直观地确认,getScreenInfo() 在调用 window_manager 前就已经返回结果,并且就是我所使用的分辨率和缩放比例。

@Predidit
Copy link
Owner

感谢您的工作。这条PR已经合并。

@Predidit Predidit merged commit 68d9a8e into Predidit:main Jul 23, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants