4992d916d0
MethodChannel pangolin/vpn (start/stop/getStatus/selectOutbound/ getActiveOutbound/setKillSwitch) + 双 EventChannel (status/stats) 契约定义在 lib/bridge/vpn_bridge.dart 头部注释冻结。 - Dart: VpnBridge 抽象接口 + VpnNativeBridge 原生实现 + VpnBridgeMock 假内核(含 connectDelay/statsInterval 可配参数) - Dart 桌面: kernel_process.dart KernelProcess/ClashApiClient 接口骨架 - Android: PangolinVpnService (VpnService 骨架 + 前台通知占位) + VpnEventBus 解耦总线 + MainActivity MethodChannel/EventChannel 注册 - iOS: PacketTunnelProvider (NEPacketTunnelProvider 骨架) + VpnManager (NETunnelProviderManager + NEVPNStatus 订阅) + AppDelegate 通道注册 + Xcode project.pbxproj 添加 PacketTunnel extension target - Widget: VpnStatus enum 迁移至 bridge/vpn_bridge.dart (+error 态), connect_button/home_shell 更新穷举匹配 - Test: test/bridge/vpn_bridge_mock_test.dart 覆盖 start→connecting→on →stop→off 序列、stats 周期推帧、selectOutbound 重连序列、schema 回测 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
150 lines
5.1 KiB
Dart
150 lines
5.1 KiB
Dart
// connect_button.dart — 核心连接键(三态:off / connecting / on)
|
|
// VpnStatus 枚举定义在 lib/bridge/vpn_bridge.dart(含 error 扩展态)
|
|
import 'package:flutter/material.dart';
|
|
import '../bridge/vpn_bridge.dart' show VpnStatus;
|
|
import '../pangolin_theme.dart';
|
|
import 'pangolin_icons.dart';
|
|
|
|
// 重导出便于其他 widget 从 connect_button.dart 引入(向后兼容)
|
|
export '../bridge/vpn_bridge.dart' show VpnStatus;
|
|
|
|
class ConnectButton extends StatefulWidget {
|
|
const ConnectButton({
|
|
super.key,
|
|
required this.status,
|
|
required this.onTap,
|
|
this.elapsed = Duration.zero,
|
|
this.size = 208,
|
|
});
|
|
|
|
final VpnStatus status;
|
|
final VoidCallback onTap;
|
|
final Duration elapsed;
|
|
final double size;
|
|
|
|
@override
|
|
State<ConnectButton> createState() => _ConnectButtonState();
|
|
}
|
|
|
|
class _ConnectButtonState extends State<ConnectButton> with SingleTickerProviderStateMixin {
|
|
late final AnimationController _spin =
|
|
AnimationController(vsync: this, duration: const Duration(milliseconds: 1400))..repeat();
|
|
|
|
@override
|
|
void dispose() {
|
|
_spin.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String _fmt(Duration d) {
|
|
String two(int n) => n.toString().padLeft(2, '0');
|
|
return '${two(d.inHours)}:${two(d.inMinutes % 60)}:${two(d.inSeconds % 60)}';
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final c = context.pangolin;
|
|
final s = widget.status;
|
|
|
|
final Color fill = switch (s) {
|
|
VpnStatus.off => c.bgSubtle,
|
|
VpnStatus.connecting => c.accent,
|
|
VpnStatus.on => c.success,
|
|
VpnStatus.error => c.danger,
|
|
};
|
|
final Color fg = s == VpnStatus.off ? c.accent : PangolinColors.white;
|
|
final IconData icon = switch (s) {
|
|
VpnStatus.off => PangolinIcons.power,
|
|
VpnStatus.connecting => PangolinIcons.loader,
|
|
VpnStatus.on => PangolinIcons.shieldCheck,
|
|
VpnStatus.error => PangolinIcons.power,
|
|
};
|
|
|
|
final List<BoxShadow> glow = s == VpnStatus.off
|
|
? PangolinShadow.md
|
|
: [
|
|
BoxShadow(
|
|
color: (s == VpnStatus.on ? c.success : c.accent).withOpacity(0.18),
|
|
blurRadius: 0,
|
|
spreadRadius: 9,
|
|
),
|
|
...PangolinShadow.lg,
|
|
];
|
|
|
|
return GestureDetector(
|
|
onTap: widget.onTap,
|
|
child: AnimatedContainer(
|
|
duration: PangolinMotion.slow,
|
|
curve: PangolinMotion.easeOut,
|
|
width: widget.size,
|
|
height: widget.size,
|
|
decoration: BoxDecoration(color: fill, shape: BoxShape.circle, boxShadow: glow),
|
|
child: Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Positioned.fill(
|
|
child: AnimatedBuilder(
|
|
animation: _spin,
|
|
builder: (_, __) => CustomPaint(
|
|
painter: _RingPainter(
|
|
status: s,
|
|
track: c.sand200,
|
|
progress: PangolinColors.white,
|
|
turns: s == VpnStatus.connecting ? _spin.value : 0,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(icon, size: 50, color: fg),
|
|
const SizedBox(height: 6),
|
|
if (s == VpnStatus.on)
|
|
Text(_fmt(widget.elapsed), style: PangolinText.mono.copyWith(color: fg, fontSize: 18, fontWeight: FontWeight.w600))
|
|
else
|
|
Text(s == VpnStatus.connecting ? '···' : 'TAP',
|
|
style: PangolinText.mono.copyWith(color: fg, fontSize: 14, fontWeight: FontWeight.w700, letterSpacing: 1.1)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _RingPainter extends CustomPainter {
|
|
_RingPainter({required this.status, required this.track, required this.progress, required this.turns});
|
|
final VpnStatus status;
|
|
final Color track;
|
|
final Color progress;
|
|
final double turns;
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
final center = size.center(Offset.zero);
|
|
final radius = size.width / 2 - 8;
|
|
final rect = Rect.fromCircle(center: center, radius: radius);
|
|
|
|
if (status == VpnStatus.off) {
|
|
final p = Paint()..style = PaintingStyle.stroke..strokeWidth = 3..strokeCap = StrokeCap.round..color = track;
|
|
const dash = 0.045, gap = 0.11;
|
|
for (double a = 0; a < 6.283; a += dash + gap) {
|
|
canvas.drawArc(rect, a, dash, false, p);
|
|
}
|
|
} else if (status == VpnStatus.connecting) {
|
|
final base = Paint()..style = PaintingStyle.stroke..strokeWidth = 4..color = progress.withOpacity(0.3);
|
|
canvas.drawArc(rect, 0, 6.283, false, base);
|
|
final arc = Paint()..style = PaintingStyle.stroke..strokeWidth = 4..strokeCap = StrokeCap.round..color = progress;
|
|
canvas.drawArc(rect, turns * 6.283, 1.6, false, arc);
|
|
} else {
|
|
final p = Paint()..style = PaintingStyle.stroke..strokeWidth = 4..strokeCap = StrokeCap.round..color = progress.withOpacity(0.85);
|
|
canvas.drawArc(rect, 0, 6.283, false, p);
|
|
}
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(_RingPainter old) => old.turns != turns || old.status != status;
|
|
}
|