Files
AeroTrack/lib/screens/lan_sync_screen.dart
T

325 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../services/storage_service.dart';
import '../services/sync_service.dart';
class LanSyncScreen extends StatefulWidget {
final StorageService storageService;
const LanSyncScreen({super.key, required this.storageService});
@override
State<LanSyncScreen> createState() => _LanSyncScreenState();
}
class _LanSyncScreenState extends State<LanSyncScreen> {
late TextEditingController _urlController;
late TextEditingController _deviceNameController;
bool _isTesting = false;
bool _autoSync = true;
String? _statusMessage;
bool? _isOnline;
int? _latencyMs;
DateTime? _lastSyncTime;
late SyncService _syncService;
@override
void initState() {
super.initState();
_syncService = SyncService(widget.storageService);
_urlController = TextEditingController();
_deviceNameController = TextEditingController();
_loadConfig();
}
Future<void> _loadConfig() async {
final url = await SyncService.getServerUrl();
final name = await SyncService.getDeviceName();
final auto = await SyncService.getAutoSyncEnabled();
final lastTime = await SyncService.getLastSyncTime();
setState(() {
_urlController.text = url;
_deviceNameController.text = name;
_autoSync = auto;
_lastSyncTime = lastTime;
});
_testConnection(autoPingOnly: true);
}
@override
void dispose() {
_urlController.dispose();
_deviceNameController.dispose();
super.dispose();
}
Future<void> _saveConfig() async {
await SyncService.setServerUrl(_urlController.text);
await SyncService.setDeviceName(_deviceNameController.text);
await SyncService.setAutoSyncEnabled(_autoSync);
}
Future<void> _testConnection({bool autoPingOnly = false}) async {
final url = _urlController.text.trim();
if (url.isEmpty) return;
setState(() {
_isTesting = true;
_statusMessage = null;
});
await _saveConfig();
if (autoPingOnly) {
final latency = await SyncService.pingServer(url);
setState(() {
_isTesting = false;
_isOnline = latency != null;
_latencyMs = latency;
if (latency != null) {
_statusMessage = '局域网主机在线 (响应 $latency ms)';
} else {
_statusMessage = '当前未连通局域网服务器 (回家连 Wi-Fi 即可自动连上)';
}
});
return;
}
// 执行完整同步
final result = await _syncService.performSync(customUrl: url);
final lastTime = await SyncService.getLastSyncTime();
setState(() {
_isTesting = false;
_isOnline = result.success;
_latencyMs = result.latencyMs;
_statusMessage = result.message;
_lastSyncTime = lastTime;
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(result.message),
backgroundColor: result.success ? const Color(0xFF10B981) : const Color(0xFFEF4444),
),
);
}
}
@override
Widget build(BuildContext context) {
final lastSyncStr = _lastSyncTime != null
? DateFormat('yyyy-MM-dd HH:mm:ss').format(_lastSyncTime!)
: '尚未同步';
return Scaffold(
backgroundColor: const Color(0xFFF8FAFC),
appBar: AppBar(
title: const Text('局域网多设备同步', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
backgroundColor: Colors.white,
elevation: 0,
foregroundColor: const Color(0xFF0F172A),
),
body: ListView(
padding: const EdgeInsets.all(20),
children: [
// 状态卡片
Container(
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(
color: _isOnline == true ? const Color(0xFF86EFAC) : const Color(0xFFCBD5E1),
width: _isOnline == true ? 1.5 : 1,
),
boxShadow: [
BoxShadow(color: Colors.black.withValues(alpha: 0.02), blurRadius: 8, offset: const Offset(0, 2)),
],
),
child: Row(
children: [
Container(
width: 44,
height: 44,
decoration: BoxDecoration(
color: _isOnline == true ? const Color(0xFFECFDF5) : const Color(0xFFF1F5F9),
shape: BoxShape.circle,
),
child: Icon(
_isOnline == true ? Icons.cloud_done : Icons.cloud_off,
color: _isOnline == true ? const Color(0xFF10B981) : Colors.grey,
size: 24,
),
),
const SizedBox(width: 14),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_isOnline == true
? (_latencyMs != null ? '局域网主机已连接 (延迟 ${_latencyMs}ms)' : '局域网主机已连接')
: '局域网离线 / 待连接',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 15,
color: _isOnline == true ? const Color(0xFF065F46) : const Color(0xFF334155),
),
),
const SizedBox(height: 3),
Text(
_statusMessage ?? '正在检测局域网服务器连通性...',
style: TextStyle(
fontSize: 12,
color: _isOnline == true ? const Color(0xFF059669) : Colors.grey,
),
),
const SizedBox(height: 4),
Text('最近成功同步:$lastSyncStr', style: const TextStyle(fontSize: 11, color: Color(0xFF94A3B8))),
],
),
),
],
),
),
const SizedBox(height: 20),
// 配置表单
const Text('局域网服务器连接配置', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13, color: Colors.grey)),
const SizedBox(height: 10),
Container(
padding: const EdgeInsets.all(18),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(18),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('局域网同步服务器地址 (IP + 端口)', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
const SizedBox(height: 8),
TextField(
controller: _urlController,
decoration: InputDecoration(
hintText: '例如: http://192.168.21.200:8090',
contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
prefixIcon: const Icon(Icons.dns, size: 20, color: Color(0xFF2563EB)),
),
),
const SizedBox(height: 8),
// 快捷预设按钮
Row(
children: [
ActionChip(
label: const Text('填入 192.168.21.200:8090', style: TextStyle(fontSize: 11)),
onPressed: () => setState(() => _urlController.text = 'http://192.168.21.200:8090'),
),
const SizedBox(width: 8),
ActionChip(
label: const Text('填入 localhost:8090', style: TextStyle(fontSize: 11)),
onPressed: () => setState(() => _urlController.text = 'http://localhost:8090'),
),
],
),
const SizedBox(height: 18),
const Text('本机设备标识名称 (用于全家记录署名)', style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
const SizedBox(height: 8),
TextField(
controller: _deviceNameController,
decoration: InputDecoration(
hintText: '例如: 妈妈的手机 / 家中 iPad / 爷爷手机',
contentPadding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
prefixIcon: const Icon(Icons.devices, size: 20, color: Color(0xFF2563EB)),
),
),
],
),
),
const SizedBox(height: 20),
// 自动同步开关
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: const Color(0xFFE2E8F0)),
),
child: SwitchListTile(
title: const Text('打卡后自动静默同步', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w600)),
subtitle: const Text('只要连着家庭 Wi-Fi,每次吸入完成自动同步到所有设备', style: TextStyle(fontSize: 12)),
value: _autoSync,
activeTrackColor: const Color(0xFF2563EB),
onChanged: (val) {
setState(() => _autoSync = val);
_saveConfig();
},
),
),
const SizedBox(height: 24),
// 立即同步按钮
ElevatedButton.icon(
onPressed: _isTesting ? null : () => _testConnection(autoPingOnly: false),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF2563EB),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
),
icon: _isTesting
? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white))
: const Icon(Icons.sync),
label: Text(_isTesting ? '正在同步数据...' : '测试连接并立即同步', style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 15)),
),
const SizedBox(height: 24),
// 架构原理说明
Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: const Color(0xFFEFF6FF),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: const Color(0xFFBFDBFE)),
),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.shield_outlined, color: Color(0xFF2563EB), size: 18),
SizedBox(width: 8),
Text('家庭私有化保障说明', style: TextStyle(color: Color(0xFF1E3A8A), fontWeight: FontWeight.bold, fontSize: 13)),
],
),
SizedBox(height: 8),
Text(
'• 数据完全保存在您的私有服务器 (192.168.21.200),不上任何公有云,隐私绝对安全。\n'
'• 离开家时:手机照常在本地打卡计时,不受断网影响。\n'
'• 回家连上 Wi-Fi:系统无感将离线记录合并至服务器,并校准辅舒酮 60 喷剩余数。',
style: TextStyle(fontSize: 12, color: Color(0xFF1E40AF), height: 1.5),
),
],
),
),
],
),
);
}
}