device.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'dart:io';
  2. import 'dart:math';
  3. import 'dart:ui';
  4. import 'package:device_info_plus/device_info_plus.dart';
  5. import 'package:flutter/material.dart';
  6. class Device {
  7. static Locale? locale;
  8. final Directory baseDir;
  9. Device(this.context, this.baseDir);
  10. AndroidDeviceInfo? androidDeviceInfo;
  11. /// 获取平台性能
  12. int get androidSdkInt => androidDeviceInfo != null ? androidDeviceInfo!.version.sdkInt : 100;
  13. bool get isOldAndroid => androidSdkInt < 26; // 安卓8以下
  14. bool get isLowRamDevice => androidDeviceInfo != null ? androidDeviceInfo!.isLowRamDevice : false;
  15. bool get isLowEndDevice => Platform.numberOfProcessors <= 1 || isOldAndroid || isLowRamDevice;
  16. static double get devPixelRatio => PlatformDispatcher.instance.views.first.devicePixelRatio;
  17. static Size get physicalSize => PlatformDispatcher.instance.views.first.physicalSize;
  18. static Size get logicalSize => physicalSize / devPixelRatio;
  19. ///final Size screenSize;
  20. final BuildContext context;
  21. final aspectRatio = 3.0 / 2.0; // 图片宽高比按照 2 / 3 设定,相应的 board play 区域也是这个比例
  22. final _tabletProfile = DeviceProfile(horizontalPadding: 60, verticalPadding: 10);
  23. final _profile = DeviceProfile(horizontalPadding: 10, verticalPadding: 10);
  24. double _bannerHeight = 0;
  25. int get previewImageSize => (boardSize.shortestSide * devicePixelRatio).toInt();
  26. set bannerHeight(double height) {
  27. _bannerHeight = height;
  28. }
  29. /// 获取当前配置
  30. DeviceProfile get profile => isTablet ? _tabletProfile : _profile;
  31. /// 屏幕尺寸
  32. Size get screenSize => MediaQuery.of(context).size;
  33. /// 像素密度
  34. double get devicePixelRatio => isLowEndDevice ? 1 : MediaQuery.of(context).devicePixelRatio;
  35. double get dpi => devicePixelRatio * 160;
  36. /// safeArea高度 Z
  37. double get safeAreaHeight => MediaQuery.of(context).viewPadding.top + MediaQuery.of(context).viewPadding.bottom;
  38. /// 获取appbar的高度
  39. double get appBarHeight => AppBar().preferredSize.height;
  40. /// Play 尺寸
  41. Size get boardSize =>
  42. Size(screenSize.width - profile.horizontalPadding * 2, screenSize.height - (safeAreaHeight + appBarHeight + profile.verticalPadding * 2));
  43. /// 根据谷歌的描述,banner尺寸最小50px, 最大不超过15%
  44. double get estimateBannerHeight => max(50, screenSize.height * 0.15).truncateToDouble();
  45. // double get bannerHeight => _bannerHeight == 0 ? estimateBannerHeight : _bannerHeight;
  46. double get bannerHeight => _bannerHeight != 0
  47. ? _bannerHeight
  48. : isTablet
  49. ? 90
  50. : 50;
  51. /// 是否平板
  52. bool get isTablet => screenSize.shortestSide >= 600;
  53. String filePath(String relativePath) => '${baseDir.path}/$relativePath';
  54. // board核心绘制区域
  55. Rect get targetRect {
  56. final double availableHeight = screenSize.height - appBarHeight - bannerHeight - 10; // -10 是预留给banner广告的间隔距离
  57. final double paddedWidth = screenSize.width - 2 * profile.horizontalPadding;
  58. final double paddedHeight = availableHeight - 2 * profile.verticalPadding;
  59. final double targetWidth = paddedWidth;
  60. final double targetHeight = targetWidth * aspectRatio;
  61. final double finalPuzzleWidth;
  62. final double finalPuzzleHeight;
  63. if (targetHeight > paddedHeight) {
  64. finalPuzzleHeight = paddedHeight;
  65. finalPuzzleWidth = paddedHeight / aspectRatio;
  66. } else {
  67. finalPuzzleWidth = targetWidth;
  68. finalPuzzleHeight = targetHeight;
  69. }
  70. final double targetYStart = appBarHeight + profile.verticalPadding + (paddedHeight - finalPuzzleHeight) / 2;
  71. final double targetXStart = profile.horizontalPadding + (paddedWidth - finalPuzzleWidth) / 2;
  72. final newTargetRect = Rect.fromLTWH(targetXStart, targetYStart, finalPuzzleWidth, finalPuzzleHeight);
  73. return newTargetRect;
  74. }
  75. // 最佳图片分辨率
  76. Size get bestImageSize => Size(targetRect.width * devPixelRatio, targetRect.height * devPixelRatio);
  77. }
  78. class DeviceProfile {
  79. final double horizontalPadding; // 水平padding
  80. final double verticalPadding; // 垂直padding
  81. DeviceProfile({
  82. this.horizontalPadding = 10,
  83. this.verticalPadding = 10, //Play board padding
  84. });
  85. }