168 lines
5.4 KiB
Dart
168 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter/services.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
||
|
||
import '../../../../core/app/app_user_cubit.dart';
|
||
import '../../../devices/presentation/bloc/devices_cubit.dart';
|
||
import '../bloc/remote_control_cubit.dart';
|
||
import '../bloc/remote_control_state.dart';
|
||
import '../widgets/center_control_area.dart';
|
||
import '../widgets/emergency_overlay.dart';
|
||
import '../widgets/left_joystick_area.dart';
|
||
import '../widgets/my_video_player.dart';
|
||
import '../widgets/right_joystick_area.dart';
|
||
import '../widgets/top_status_bar.dart'; // 假设路径
|
||
|
||
class RemoteControlPage extends StatefulWidget {
|
||
const RemoteControlPage({super.key});
|
||
|
||
@override
|
||
State<RemoteControlPage> createState() => _RemoteControlPageState();
|
||
}
|
||
|
||
class _RemoteControlPageState extends State<RemoteControlPage> {
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
// 强制横屏与沉浸式
|
||
SystemChrome.setPreferredOrientations([
|
||
DeviceOrientation.landscapeLeft,
|
||
DeviceOrientation.landscapeRight,
|
||
]);
|
||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
// 恢复竖屏
|
||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// 1. 监听全局状态 (实现方案二:掉线实时刷新)
|
||
final userState = context.watch<AppUserCubit>().state;
|
||
final deviceState = context.watch<DevicesCubit>().state;
|
||
final currentDevice = deviceState.selectedDevice;
|
||
|
||
// 2. 监听局部遥控状态 (对应 Android Compose 的 collectAsState)
|
||
final remoteState = context.watch<RemoteControlCubit>().state;
|
||
|
||
// 如果设备突然掉线,显示遮罩 (对应 Android 的 finishEvent 逻辑)
|
||
if (currentDevice == null /*|| !currentDevice.isOnline*/ ) {
|
||
return _buildOfflineScaffold();
|
||
}
|
||
|
||
return Scaffold(
|
||
backgroundColor: Colors.black,
|
||
body: Stack(
|
||
children: [
|
||
// 底层:视频流 (传入 device 和 user)
|
||
Positioned.fill(
|
||
child: MyVideoPlayer(
|
||
deviceIp: TCPConsts.TCP_IP,
|
||
device: currentDevice,
|
||
user: userState.user!, // 假设 AppUserCubit 存有 user 实体
|
||
),
|
||
),
|
||
|
||
// 中层:急停呼吸灯光晕 (对应 Android 的 EmergencyBreathingOverlay)
|
||
if (remoteState.isEmergency)
|
||
const Positioned.fill(child: EmergencyOverlay()),
|
||
|
||
// 顶层:UI 控制层
|
||
SafeArea(
|
||
child: Column(
|
||
children: [
|
||
// 顶部胶囊状态条
|
||
const TopStatusBar(),
|
||
|
||
// 左右摇杆及中间控制区
|
||
Expanded(
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(
|
||
horizontal: 20,
|
||
vertical: 10,
|
||
),
|
||
child: Row(
|
||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||
crossAxisAlignment: CrossAxisAlignment.end,
|
||
children: [
|
||
// 左摇杆:前后控制
|
||
LeftJoystickArea(isLocked: remoteState.isLocked),
|
||
|
||
// 中间区:底盘升降/急停按钮
|
||
const CenterControlArea(),
|
||
|
||
// 右摇杆:左右控制
|
||
RightJoystickArea(isLocked: remoteState.isLocked),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
|
||
// 权限请求弹窗 (对应 Android 的 PermissionRequestDialog)
|
||
if (remoteState.showPermissionRequestDialog)
|
||
_buildPermissionDialog(context, remoteState),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildOfflineScaffold() {
|
||
return Scaffold(
|
||
backgroundColor: Colors.black,
|
||
body: Center(
|
||
child: Column(
|
||
mainAxisSize: MainAxisSize.min,
|
||
children: [
|
||
const Icon(Icons.signal_wifi_off, color: Colors.white, size: 60),
|
||
const SizedBox(height: 20),
|
||
const Text(
|
||
"设备已断开连接",
|
||
style: TextStyle(color: Colors.white, fontSize: 18),
|
||
),
|
||
const SizedBox(height: 20),
|
||
ElevatedButton(
|
||
onPressed: () => context.pop(),
|
||
child: const Text("返回"),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildPermissionDialog(
|
||
BuildContext context,
|
||
RemoteControlState state,
|
||
) {
|
||
return Container(
|
||
color: Colors.black54,
|
||
child: AlertDialog(
|
||
title: const Text("权限变更"),
|
||
content: Text("${state.permissionPlatform}端正请求控制权,同意释放吗?"),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () =>
|
||
context.read<RemoteControlCubit>().respondPermission(false),
|
||
child: const Text("拒绝"),
|
||
),
|
||
TextButton(
|
||
onPressed: () =>
|
||
context.read<RemoteControlCubit>().respondPermission(true),
|
||
child: const Text("同意"),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|