/// 网络状态模型 class NetworkStatus { final bool isConnected; // 是否连接 final double downloadSpeed; // 下载速度 (Mbps) final double uploadSpeed; // 上传速度 (Mbps) final int latency; // 延迟 (ms) final String externalIp; // 外网IP final String localIp; // 内网IP final DateTime lastCheck; // 最后检测时间 const NetworkStatus({ this.isConnected = false, this.downloadSpeed = 0, this.uploadSpeed = 0, this.latency = 0, this.externalIp = '', this.localIp = '', required this.lastCheck, }); /// 默认状态 factory NetworkStatus.initial() { return NetworkStatus(lastCheck: DateTime.now()); } /// 从JSON创建 factory NetworkStatus.fromJson(Map json) { return NetworkStatus( isConnected: json['isConnected'] as bool? ?? false, downloadSpeed: (json['downloadSpeed'] as num?)?.toDouble() ?? 0, uploadSpeed: (json['uploadSpeed'] as num?)?.toDouble() ?? 0, latency: json['latency'] as int? ?? 0, externalIp: json['externalIp'] as String? ?? '', localIp: json['localIp'] as String? ?? '', lastCheck: DateTime.parse(json['lastCheck'] as String), ); } /// 转换为JSON Map toJson() { return { 'isConnected': isConnected, 'downloadSpeed': downloadSpeed, 'uploadSpeed': uploadSpeed, 'latency': latency, 'externalIp': externalIp, 'localIp': localIp, 'lastCheck': lastCheck.toIso8601String(), }; } /// 复制并更新 NetworkStatus copyWith({ bool? isConnected, double? downloadSpeed, double? uploadSpeed, int? latency, String? externalIp, String? localIp, DateTime? lastCheck, }) { return NetworkStatus( isConnected: isConnected ?? this.isConnected, downloadSpeed: downloadSpeed ?? this.downloadSpeed, uploadSpeed: uploadSpeed ?? this.uploadSpeed, latency: latency ?? this.latency, externalIp: externalIp ?? this.externalIp, localIp: localIp ?? this.localIp, lastCheck: lastCheck ?? this.lastCheck, ); } /// 获取网络状态描述 String get statusDescription { if (!isConnected) return '网络断开'; if (latency > 200) return '网络较慢'; if (latency > 100) return '网络一般'; return '网络良好'; } /// 格式化下载速度 String get downloadSpeedText { if (downloadSpeed >= 1000) { return '${(downloadSpeed / 1000).toStringAsFixed(2)} Gbps'; } return '${downloadSpeed.toStringAsFixed(2)} Mbps'; } /// 格式化上传速度 String get uploadSpeedText { if (uploadSpeed >= 1000) { return '${(uploadSpeed / 1000).toStringAsFixed(2)} Gbps'; } return '${uploadSpeed.toStringAsFixed(2)} Mbps'; } } /// 网速历史记录 class SpeedHistory { final DateTime time; final double download; final double upload; const SpeedHistory({ required this.time, required this.download, required this.upload, }); }