import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:maibu_satabot_v2/core/localization/app_localizations.dart'; import 'package:maibu_satabot_v2/features/remote_control/presentation/widgets/webrtc/webrtc_local_player.dart'; import '../models/pv_telemetry.dart'; import '../models/pv_video_fit.dart'; import '../models/pv_video_source.dart'; import '../theme/pv_colors.dart'; import 'pv_local_rtsp_video.dart'; import 'pv_usb_camera_video.dart'; import 'pv_video_fit_box.dart'; /// 实时视频区:底层播放 WebRTC 流,上层叠加设计稿中的信息浮层。 /// /// 浮层(左上分辨率、顶部障碍预警、右上网络状态、右侧工具按钮、左下罗盘) /// 目前为静态样式还原,障碍物测距等 AR 数据需真实算法接入后驱动。 class PvVideoArea extends StatelessWidget { const PvVideoArea({ super.key, required this.streamUrl, required this.telemetry, this.onFullscreen, this.onLocate, this.onScreenshot, this.showSideTools = true, this.showCompass = true, this.videoSource = PvVideoSource.webrtc, this.videoFit = PvVideoFit.contain, }); final String streamUrl; final PvTelemetry telemetry; final VoidCallback? onFullscreen; final VoidCallback? onLocate; final VoidCallback? onScreenshot; /// 手动控制全屏态可隐藏右侧工具按钮,避免与摇杆冲突 final bool showSideTools; /// 隐藏左下罗盘(手动控制全屏态下摇杆占据下角,避免与罗盘重叠) final bool showCompass; /// 视频链路(多源可扩展):webrtc=云端流 / localRtsp=以太网 RTSP / usbCamera=USB 采集卡。 /// cubit 由页面层按链路单实例提供(pv_control_page._wrapVideoProvider)。 final PvVideoSource videoSource; /// 视频展示方式(设置面板可改):contain 完整显示(可能留黑边)/ cover 等比铺满(裁边)。 final PvVideoFit videoFit; @override Widget build(BuildContext context) { return Container( clipBehavior: Clip.antiAlias, decoration: PvColors.panel(radius: 16, fill: Colors.black, glow: true), child: Stack( fit: StackFit.expand, children: [ // ① 视频层:按链路枚举分发,再按「视频展示」档位做 contain / cover 适配 PvVideoFitBox( fit: videoFit, source: videoSource, child: _buildLayer(), ), // ② 顶部左侧:实时视频 + 分辨率/帧率 Positioned(left: 14, top: 12, child: _buildTopLeftBadge(context)), // ③ 顶部中间:障碍物预警 if (telemetry.obstacleAlert) Positioned(left: 0, right: 0, top: 10, child: _buildObstacleBanner()), // ④ 顶部右侧:网络状态 Positioned(right: 14, top: 12, child: _buildNetworkBadge(context)), // ⑤ 右侧工具按钮列 if (showSideTools) Positioned(right: 12, top: 54, child: _buildSideTools()), // ⑥ 左下罗盘 if (showCompass) Positioned(left: 14, bottom: 14, child: _buildCompass()), // ⑦ 圆角描边覆盖层:视频画面会把 Container 边框盖住, // 这里在最上层单独画一圈青色圆角边框,保证四角圆角 + 边框始终可见。 Positioned.fill( child: IgnorePointer( child: DecoratedBox( decoration: BoxDecoration( borderRadius: BorderRadius.circular(16), border: Border.all(color: PvColors.cardBorder, width: 1.5), ), ), ), ), ], ), ); } /// 各链路的原始视频层(组件内部自己等比居中)。 Widget _buildLayer() { if (videoSource == PvVideoSource.usbCamera) return const PvUsbCameraVideo(); if (videoSource == PvVideoSource.localRtsp) return const PvLocalRtspVideo(); if (streamUrl.isNotEmpty) { return WebRTCLocalPlayer( streamUrl: streamUrl, showLeftPip: false, showRightPip: false, mainViewAlignment: Alignment.center, ); } return _VideoPlaceholder(); } Widget _buildTopLeftBadge(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ const Icon(Icons.circle, size: 8, color: PvColors.danger), const SizedBox(width: 5), Text( AppLocalizations.of( context, ).translate('pv_control.realtime_video'), style: const TextStyle( color: PvColors.textPrimary, fontSize: 12, fontWeight: FontWeight.w700, shadows: [Shadow(blurRadius: 6, color: Colors.black)], ), ), ], ), const SizedBox(height: 2), Text( '${telemetry.resolution} | ${telemetry.fps}fps', style: const TextStyle( color: PvColors.textSecondary, fontSize: 9, shadows: [Shadow(blurRadius: 4, color: Colors.black)], ), ), ], ); } Widget _buildObstacleBanner() => const _ObstacleBannerPill(); Widget _buildNetworkBadge(BuildContext context) { final ok = telemetry.networkNormal; final l10n = AppLocalizations.of(context); return Container( padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5), decoration: BoxDecoration( color: Colors.black.withValues(alpha: 0.35), borderRadius: BorderRadius.circular(20), border: Border.all( color: (ok ? PvColors.online : PvColors.warning).withValues(alpha: 0.6), ), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.wifi, size: 14, color: ok ? PvColors.online : PvColors.warning), const SizedBox(width: 4), Text( ok ? l10n.translate('pv_control.network_good') : l10n.translate('pv_control.network_bad'), style: TextStyle( fontSize: 10, fontWeight: FontWeight.w600, color: ok ? PvColors.online : PvColors.warning, ), ), ], ), ); } Widget _buildSideTools() { return Column( mainAxisSize: MainAxisSize.min, children: [ _ToolButton(icon: Icons.fullscreen, onTap: onFullscreen), const SizedBox(height: 10), _ToolButton(icon: Icons.person_search, onTap: onLocate), const SizedBox(height: 10), _ToolButton(icon: Icons.photo_camera, onTap: onScreenshot), ], ); } Widget _buildCompass() { return SizedBox( width: 80, height: 80, child: CustomPaint(painter: _CompassPainter()), ); } } class _VideoPlaceholder extends StatelessWidget { @override Widget build(BuildContext context) { return Container( color: Colors.black87, child: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.videocam_off, size: 48, color: Colors.white38), const SizedBox(height: 8), Text( AppLocalizations.of( context, ).translate('pv_control.video_not_loaded'), style: const TextStyle(color: Colors.white54, fontSize: 12), ), ], ), ), ); } } /// 障碍物预警横幅:脉动动画(透明度 + 轻微缩放 + 红色辉光呼吸)以引起注意。 class _ObstacleBannerPill extends StatefulWidget { const _ObstacleBannerPill(); @override State<_ObstacleBannerPill> createState() => _ObstacleBannerPillState(); } class _ObstacleBannerPillState extends State<_ObstacleBannerPill> with SingleTickerProviderStateMixin { late final AnimationController _controller; @override void initState() { super.initState(); _controller = AnimationController( vsync: this, duration: const Duration(milliseconds: 750), )..repeat(reverse: true); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return AnimatedBuilder( animation: _controller, builder: (context, child) { final t = _controller.value; // 0..1 来回 return Center( child: Opacity( opacity: 0.72 + 0.28 * t, child: Transform.scale( scale: 1.0 + 0.07 * t, child: Container( padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 7), decoration: BoxDecoration( gradient: const LinearGradient( colors: [Color(0xFFE01E32), Color(0xFFFF5A4D)], ), borderRadius: BorderRadius.circular(8), boxShadow: [ BoxShadow( color: PvColors.danger.withValues(alpha: 0.3 + 0.4 * t), blurRadius: 12 + 16 * t, spreadRadius: 1 + 2 * t, ), ], ), child: Row( mainAxisSize: MainAxisSize.min, children: [ const _Chevron(left: true), const SizedBox(width: 7), const Icon(Icons.warning_rounded, color: Colors.white, size: 16), const SizedBox(width: 5), Text( AppLocalizations.of( context, ).translate('pv_control.obstacle_alert'), style: const TextStyle( color: Colors.white, fontSize: 14, fontWeight: FontWeight.w800, letterSpacing: 0.5, ), ), const SizedBox(width: 7), const _Chevron(left: false), ], ), ), ), ), ); }, ); } } class _Chevron extends StatelessWidget { const _Chevron({required this.left}); final bool left; @override Widget build(BuildContext context) { return Icon( left ? Icons.chevron_left : Icons.chevron_right, color: Colors.white70, size: 18, ); } } class _ToolButton extends StatelessWidget { const _ToolButton({required this.icon, this.onTap}); final IconData icon; final VoidCallback? onTap; @override Widget build(BuildContext context) { return GestureDetector( onTap: onTap, child: Container( width: 36, height: 36, decoration: BoxDecoration( color: Colors.black.withValues(alpha: 0.45), borderRadius: BorderRadius.circular(10), border: Border.all(color: PvColors.cardBorder), ), child: Icon(icon, color: PvColors.textPrimary, size: 18), ), ); } } /// 简易罗盘(N/E/S/W + 中心车辆图标 + 指向扇形) class _CompassPainter extends CustomPainter { @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final r = size.width / 2; // 外圈 final ring = Paint() ..color = PvColors.accent.withValues(alpha: 0.7) ..style = PaintingStyle.stroke ..strokeWidth = 1.5; canvas.drawCircle(center, r - 1, ring); // 指向扇形(北方向红色) final sector = Paint() ..color = PvColors.danger.withValues(alpha: 0.55) ..style = PaintingStyle.fill; canvas.drawArc( Rect.fromCircle(center: center, radius: r - 2), -math.pi / 2 - 0.5, 1.0, true, sector, ); const labels = { 'N': -math.pi / 2, 'E': 0.0, 'S': math.pi / 2, 'W': math.pi, }; for (final e in labels.entries) { final p = Offset( center.dx + math.cos(e.value) * (r - 12), center.dy + math.sin(e.value) * (r - 12), ); final tp = TextPainter( text: TextSpan( text: e.key, style: const TextStyle(color: PvColors.textPrimary, fontSize: 11, fontWeight: FontWeight.bold), ), textDirection: TextDirection.ltr, )..layout(); tp.paint(canvas, p - Offset(tp.width / 2, tp.height / 2)); } // 中心车辆图标(用简单圆点代替) final dot = Paint()..color = PvColors.accentBright; canvas.drawCircle(center, 4, dot); } @override bool shouldRepaint(covariant CustomPainter oldDelegate) => false; }