USB 摄像头+远程遥控+中英的适配+topic的集成和状态的使用到相应页面

This commit is contained in:
2026-10-20 17:03:37 +08:00
parent 178cd2533c
commit 582242e9aa
144 changed files with 15759 additions and 406 deletions

View File

@@ -0,0 +1,87 @@
import 'package:flutter/material.dart';
import 'package:usb_camera_android/usb_camera_android.dart';
import '../../domain/usb_camera_device.dart';
import '../cubit/usb_camera_state.dart';
/// USB 摄像头预览区。
class UsbCameraPreview extends StatelessWidget {
const UsbCameraPreview({
super.key,
required this.device,
required this.phase,
required this.onReconnect,
this.aspectRatio,
});
final UsbCameraDevice device;
final UsbCameraPhase phase;
final VoidCallback onReconnect;
final double? aspectRatio;
@override
Widget build(BuildContext context) {
return Stack(
fit: StackFit.expand,
children: [
// 视频画面:保持宽高比居中,上下/左右填黑边
Container(
color: Colors.black,
child: Center(
child: aspectRatio != null
? AspectRatio(
aspectRatio: aspectRatio!,
child: const UsbCameraPreviewView(),
)
: const UsbCameraPreviewView(),
),
),
// 设备名浮层
Positioned(
left: 16,
top: 16,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
decoration: BoxDecoration(
color: Colors.black.withValues(alpha: 0.5),
borderRadius: BorderRadius.circular(6),
),
child: Text(
device.productName ?? 'USB 摄像头',
style: const TextStyle(color: Colors.white70, fontSize: 13),
),
),
),
// 断开遮罩
if (phase == UsbCameraPhase.disconnected)
Container(
color: Colors.black.withValues(alpha: 0.65),
alignment: Alignment.center,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.usb_off, color: Colors.white54, size: 52),
const SizedBox(height: 18),
const Text(
'USB 摄像头已断开',
style: TextStyle(color: Colors.white, fontSize: 15),
),
const SizedBox(height: 22),
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF165DFF),
foregroundColor: Colors.white,
),
onPressed: onReconnect,
icon: const Icon(Icons.refresh, size: 18),
label: const Text('重新连接'),
),
],
),
),
],
);
}
}

View File

@@ -0,0 +1,174 @@
import 'package:flutter/material.dart';
import '../../domain/usb_camera_device.dart';
import '../cubit/usb_camera_cubit.dart';
import '../cubit/usb_camera_state.dart';
/// 未出画面时的状态层:扫描中 / 无设备 / 多设备选择 / 失败。
class UsbCameraStatusLayer extends StatelessWidget {
const UsbCameraStatusLayer({
super.key,
required this.state,
required this.cubit,
});
final UsbCameraState state;
final UsbCameraCubit cubit;
@override
Widget build(BuildContext context) {
switch (state.phase) {
case UsbCameraPhase.scanning:
case UsbCameraPhase.connecting:
return _center(
icon: Icons.usb,
spinning: true,
message: state.phase == UsbCameraPhase.scanning
? '正在检测 USB 摄像头…'
: '正在打开 USB 摄像头…',
);
case UsbCameraPhase.selecting:
return _deviceList(state.devices);
case UsbCameraPhase.openFailed:
return _center(
icon: Icons.usb_off,
message: state.message ?? 'USB 摄像头打开失败',
actionLabel: '重试',
onAction: cubit.reconnect,
);
case UsbCameraPhase.needPermission:
case UsbCameraPhase.idle:
case UsbCameraPhase.previewing:
case UsbCameraPhase.disconnected:
return _center(icon: Icons.usb, message: state.message ?? '准备就绪');
}
}
Widget _center({
required IconData icon,
required String message,
bool spinning = false,
String? actionLabel,
VoidCallback? onAction,
}) {
return Container(
color: Colors.black,
alignment: Alignment.center,
// FittedBox:嵌进受限高度的小窗(如 pv 控制页视频区)时整组等比缩小,
// 避免「重试」按钮溢出;全屏独立页空间充足时保持原样。
child: FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (spinning)
const SizedBox(
width: 42,
height: 42,
child: CircularProgressIndicator(
color: Color(0xFF165DFF),
strokeWidth: 3,
),
)
else
Icon(icon, color: Colors.white54, size: 52),
const SizedBox(height: 18),
Text(
message,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 15,
height: 1.5,
),
),
if (actionLabel != null && onAction != null) ...[
const SizedBox(height: 22),
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF165DFF),
foregroundColor: Colors.white,
),
onPressed: onAction,
icon: const Icon(Icons.refresh, size: 18),
label: Text(actionLabel),
),
],
],
),
),
),
);
}
Widget _deviceList(List<UsbCameraDevice> devices) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
child: Text(
'发现多个 USB 摄像头,请点击选择',
style: TextStyle(color: Colors.white70, fontSize: 14),
),
),
Expanded(
child: ListView.separated(
padding: const EdgeInsets.symmetric(horizontal: 12),
itemCount: devices.length,
separatorBuilder: (context, index) => const SizedBox(height: 8),
itemBuilder: (context, i) {
final d = devices[i];
return Material(
color: const Color(0xFF1C1C1E),
borderRadius: BorderRadius.circular(12),
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () => cubit.selectAndOpen(d),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 14,
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: const Color(
0xFF165DFF,
).withValues(alpha: 0.15),
borderRadius: BorderRadius.circular(10),
),
child: const Icon(
Icons.usb,
color: Color(0xFF165DFF),
size: 22,
),
),
const SizedBox(width: 12),
Expanded(
child: Text(
d.productName ?? d.deviceId,
style: const TextStyle(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w600,
),
),
),
const Icon(Icons.chevron_right, color: Colors.white38),
],
),
),
),
);
},
),
),
],
);
}
}