import 'dart:io'; import 'dart:math'; import 'dart:ui'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/material.dart'; class Device { static Locale? locale; final Directory baseDir; Device(this.context, this.baseDir); AndroidDeviceInfo? androidDeviceInfo; // 新增:判断是否32位设备 bool is32BitDevice() { if (Platform.isIOS) return false; // 近年iOS全是64位 if (androidDeviceInfo == null) return false; final abis = androidDeviceInfo!.supportedAbis; return !abis.any((abi) => abi.contains('64')); } /// 获取平台性能 int get androidSdkInt => androidDeviceInfo != null ? androidDeviceInfo!.version.sdkInt : 100; bool get isOldAndroid => androidSdkInt < 26; // 安卓8以下 bool get isLowRamDevice => androidDeviceInfo != null ? androidDeviceInfo!.isLowRamDevice : false; bool get lowCpu => Platform.numberOfProcessors <= 2; bool get isLowEndDevice { if (androidDeviceInfo == null) return false; return lowCpu || isOldAndroid || isLowRamDevice || is32BitDevice(); } static double get devPixelRatio => PlatformDispatcher.instance.views.first.devicePixelRatio; static Size get physicalSize => PlatformDispatcher.instance.views.first.physicalSize; static Size get logicalSize => physicalSize / devPixelRatio; ///final Size screenSize; final BuildContext context; final aspectRatio = 3.0 / 2.0; // 图片宽高比按照 2 / 3 设定,相应的 board play 区域也是这个比例 final _tabletProfile = DeviceProfile(horizontalPadding: 60, verticalPadding: 10); final _profile = DeviceProfile(horizontalPadding: 10, verticalPadding: 10); double _bannerHeight = 0; set bannerHeight(double height) { _bannerHeight = height; } /// 获取当前配置 DeviceProfile get profile => isTablet ? _tabletProfile : _profile; /// 屏幕尺寸 Size get screenSize => MediaQuery.of(context).size; // 真实像素密度 double get realPixelRatio => devPixelRatio; /// 像素密度 /// 像素密度:低端机通过降低采样倍率来保护内存 /// 这里的逻辑是:高端机用真实DPR,低端机降级,但不直接降到1(除非设备极差) double get effectivePixelRatio { double realDPR = PlatformDispatcher.instance.views.first.devicePixelRatio; if (isLowEndDevice) { return (realDPR > 2.0) ? 1.5 : 1.0; // 适当降级,保留一点清晰度 } return realDPR; } String get suggestedQuality { if (isLowEndDevice) return "1200"; if (isTablet) return "2400"; // 平板需要更高像素 return "1800"; } /// safeArea高度 Z double get safeAreaHeight => MediaQuery.of(context).viewPadding.top + MediaQuery.of(context).viewPadding.bottom; /// 获取appbar的高度 double get appBarHeight => AppBar().preferredSize.height; /// Play 尺寸 Size get boardSize => Size(screenSize.width - profile.horizontalPadding * 2, screenSize.height - (safeAreaHeight + appBarHeight + profile.verticalPadding * 2)); /// 根据谷歌的描述,banner尺寸最小50px, 最大不超过15% double get estimateBannerHeight => max(50, screenSize.height * 0.15).truncateToDouble(); // double get bannerHeight => _bannerHeight == 0 ? estimateBannerHeight : _bannerHeight; double get bannerHeight => _bannerHeight != 0 ? _bannerHeight : isTablet ? 90 : 50; /// 是否平板 bool get isTablet => screenSize.shortestSide >= 600; String filePath(String relativePath) => '${baseDir.path}/$relativePath'; // board核心绘制区域 Rect get targetRect { final double availableHeight = screenSize.height - appBarHeight - bannerHeight - 10; // -10 是预留给banner广告的间隔距离 final double paddedWidth = screenSize.width - 2 * profile.horizontalPadding; final double paddedHeight = availableHeight - 2 * profile.verticalPadding; final double targetWidth = paddedWidth; final double targetHeight = targetWidth * aspectRatio; final double finalPuzzleWidth; final double finalPuzzleHeight; if (targetHeight > paddedHeight) { finalPuzzleHeight = paddedHeight; finalPuzzleWidth = paddedHeight / aspectRatio; } else { finalPuzzleWidth = targetWidth; finalPuzzleHeight = targetHeight; } final double targetYStart = appBarHeight + profile.verticalPadding + (paddedHeight - finalPuzzleHeight) / 2; final double targetXStart = profile.horizontalPadding + (paddedWidth - finalPuzzleWidth) / 2; final newTargetRect = Rect.fromLTWH(targetXStart, targetYStart, finalPuzzleWidth, finalPuzzleHeight); return newTargetRect; } // 最佳图片分辨率 Size get bestImageSize => Size(targetRect.width * effectivePixelRatio, targetRect.height * effectivePixelRatio); } class DeviceProfile { final double horizontalPadding; // 水平padding final double verticalPadding; // 垂直padding DeviceProfile({ this.horizontalPadding = 10, this.verticalPadding = 10, //Play board padding }); }