getDeviceInfo method

Map<String, dynamic> getDeviceInfo()

获取详细的设备信息

Implementation

Map<String, dynamic> getDeviceInfo() {
  final deviceType = _getDeviceTypeEnum();

  Map<String, dynamic> info = {
    'deviceType': deviceType.value,
    'deviceTypeName': getDeviceTypeName(),
  };

  if (kIsWeb) {
    info['platform'] = 'Web';
  } else {
    try {
      if (Platform.isAndroid) {
        info['platform'] = 'Android';
      } else if (Platform.isIOS) {
        info['platform'] = 'iOS';
      } else if (Platform.isMacOS) {
        info['platform'] = 'macOS';
      } else if (Platform.isWindows) {
        info['platform'] = 'Windows';
      } else if (Platform.isLinux) {
        info['platform'] = 'Linux';
      } else if (Platform.isFuchsia) {
        info['platform'] = 'Fuchsia';
      }

      // 添加屏幕信息(仅移动端)
      if (Platform.isAndroid || Platform.isIOS) {
        try {
          final view = ui.PlatformDispatcher.instance.views.first;
          final size = view.physicalSize;
          final devicePixelRatio = view.devicePixelRatio;
          final logicalWidth = size.width / devicePixelRatio;
          final logicalHeight = size.height / devicePixelRatio;

          info['screenInfo'] = {
            'logicalWidth': logicalWidth.round(),
            'logicalHeight': logicalHeight.round(),
            'physicalWidth': size.width.round(),
            'physicalHeight': size.height.round(),
            'devicePixelRatio': devicePixelRatio,
            'shortestSide':
                (logicalWidth < logicalHeight ? logicalWidth : logicalHeight)
                    .round(),
          };
        } catch (e) {
          // 忽略屏幕信息获取错误
        }
      }
    } catch (e) {
      info['platform'] = 'Unknown';
    }
  }

  return info;
}