feat(app): 初始化家庭网络监控Flutter应用基本结构

- 添加Flutter项目基础文件与配置,包括.gitignore和analysis_options.yaml
- 配置Android相关文件及Gradle构建脚本支持
- 新增设备状态与网络状态数据模型及相关枚举
- 实现网络检测及网速测试服务
- 创建监控状态管理Provider,实现设备状态管理和自动刷新机制
- 编写主界面HomeScreen,包括设备列表、网速仪表盘及基础交互
- 添加资源文件支持应用图标和启动画面
- 配置项目元数据文件,支持Flutter迁移及版本控制
- 新增项目README,提供入门指引和相关资源链接
This commit is contained in:
2025-12-08 09:01:47 +08:00
parent 360cb1a991
commit 6a0d84f063
76 changed files with 4153 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
import 'dart:async';
import 'package:dio/dio.dart';
import '../models/models.dart';
import 'network_service.dart';
/// 网速测试服务
class SpeedTestService {
static final SpeedTestService _instance = SpeedTestService._internal();
factory SpeedTestService() => _instance;
SpeedTestService._internal();
final Dio _dio = Dio();
// 测试文件URL可配置
static const _testUrls = [
'https://speed.cloudflare.com/__down?bytes=10000000', // 10MB
'https://speed.hetzner.de/100MB.bin',
];
/// 测试下载速度
Future<double> testDownloadSpeed({int testDurationSeconds = 5}) async {
double totalBytes = 0;
final stopwatch = Stopwatch()..start();
try {
final response = await _dio.get<List<int>>(
_testUrls[0],
options: Options(
responseType: ResponseType.bytes,
receiveTimeout: Duration(seconds: testDurationSeconds + 2),
),
onReceiveProgress: (received, total) {
totalBytes = received.toDouble();
},
);
stopwatch.stop();
// 计算速度 (Mbps)
final seconds = stopwatch.elapsedMilliseconds / 1000;
final mbps = (totalBytes * 8) / (seconds * 1000000);
return mbps;
} catch (e) {
return 0;
}
}
/// 测试上传速度简化版通过POST测试
Future<double> testUploadSpeed({int testDurationSeconds = 5}) async {
// 生成测试数据 (1MB)
final testData = List.generate(1024 * 1024, (i) => i % 256);
final stopwatch = Stopwatch()..start();
try {
await _dio.post(
'https://speed.cloudflare.com/__up',
data: testData,
options: Options(
sendTimeout: Duration(seconds: testDurationSeconds + 2),
),
);
stopwatch.stop();
final seconds = stopwatch.elapsedMilliseconds / 1000;
final mbps = (testData.length * 8) / (seconds * 1000000);
return mbps;
} catch (e) {
return 0;
}
}
/// 完整网速测试
Future<NetworkStatus> runFullTest() async {
final networkService = NetworkService();
// 检测连接
final isConnected = await networkService.checkInternetConnection();
if (!isConnected) {
return NetworkStatus(
isConnected: false,
lastCheck: DateTime.now(),
);
}
// 获取本地IP
final localIp = await networkService.getLocalIp();
// 测量延迟
final latency = await networkService.measureLatency('8.8.8.8');
// 测试下载速度
final downloadSpeed = await testDownloadSpeed();
// 测试上传速度
final uploadSpeed = await testUploadSpeed();
return NetworkStatus(
isConnected: true,
downloadSpeed: downloadSpeed,
uploadSpeed: uploadSpeed,
latency: latency,
localIp: localIp,
lastCheck: DateTime.now(),
);
}
/// 快速网络检测(仅检测连通性和延迟)
Future<NetworkStatus> quickCheck() async {
final networkService = NetworkService();
final isConnected = await networkService.checkInternetConnection();
final localIp = await networkService.getLocalIp();
final latency = isConnected
? await networkService.measureLatency('8.8.8.8')
: -1;
return NetworkStatus(
isConnected: isConnected,
latency: latency,
localIp: localIp,
lastCheck: DateTime.now(),
);
}
}