分区相关
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'package:maibu_satabot_v2/features/device_control_mode/domain/entities/device_control_mode.dart';
|
||||
import 'package:maibu_satabot_v2/features/device_control_mode/presentation/widgets/control_mode_entry_gate.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
@@ -37,11 +39,18 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
StreamSubscription? _permissionSubscription; // 🔥 权限监听订阅
|
||||
bool _isShowingPermissionDialog = false; // 🔥 防止弹窗重复显示
|
||||
bool? _lastPermissionDialogState; // 🔥 记录上次弹窗状态,检测状态变化
|
||||
bool _controlSessionStarted = false;
|
||||
late final String _modeDeviceId = _cubit.state.targetDevice?.deviceName ?? '';
|
||||
bool _isLoadingPermission = true; // 🔥 标记是否正在加载权限状态
|
||||
|
||||
/// 缓存本地化实例:dispose 阶段 context 已失效 / 弹窗中需提前拼文案,
|
||||
/// 不能依赖 `AppLocalizations.of(context)`。
|
||||
late AppLocalizations _l10n;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_l10n = AppLocalizations.of(context);
|
||||
_cubit = context.read<RemoteControlCubit>();
|
||||
_devicesCubit = context.read<DevicesCubit>();
|
||||
|
||||
@@ -104,26 +113,6 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
]);
|
||||
// 2. 隐藏状态栏和虚拟按键
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky);
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
final remoteCubit = context.read<RemoteControlCubit>();
|
||||
|
||||
// 🔥 倒车影像:现阶段改用摇杆(滑块)数据驱动,不再启动 Mock 假数据源。
|
||||
// 恢复 Mock:取消下行注释即可(需与 cubit 里 _syncReverseCameraFromJoystick 的调用二选一)。
|
||||
// remoteCubit.startReverseCameraMock();
|
||||
|
||||
// debugPrint('🔍 [RemoteControl] initState - targetDevice: ${remoteCubit.state.targetDevice}');
|
||||
|
||||
// 🔥 检查 targetDevice 是否存在
|
||||
if (remoteCubit.state.targetDevice == null) {
|
||||
// debugPrint('❌ [RemoteControl] targetDevice 为空,无法进入远程控制');
|
||||
return;
|
||||
}
|
||||
|
||||
// 🔥 只在控制循环未启动时才启动
|
||||
remoteCubit.startControlLoop();
|
||||
// debugPrint('✅ [RemoteControl] 控制循环已启动');
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -133,10 +122,12 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
|
||||
// 释放远程控制权限(type=2:退出远程遥控页面释放)
|
||||
final deviceId = _cubit.state.targetDevice?.deviceName;
|
||||
if (deviceId != null && deviceId.isNotEmpty) {
|
||||
if (_controlSessionStarted && deviceId != null && deviceId.isNotEmpty) {
|
||||
_cubit.releasePermission(deviceId: deviceId, type: 2).then((success) {
|
||||
CapsuleToast.show(
|
||||
success ? '已释放远程控制权限' : '控制权限释放失败',
|
||||
success
|
||||
? _l10n.translate('remote_control.toast_released')
|
||||
: _l10n.translate('remote_control.toast_release_failed'),
|
||||
showCheck: success,
|
||||
);
|
||||
});
|
||||
@@ -145,7 +136,17 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
_permissionSubscription?.cancel(); // 🔥 取消订阅
|
||||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||
_cubit.stopControlLoop();
|
||||
// 🔥 退页先把车真正停下:归零双轴 + 连发 30 帧停止指令。
|
||||
// 只靠 stopControlLoop() 断掉 200ms 循环是不够的:设备只认“最后收到的
|
||||
// 运动帧”,若手指还压在摇杆上就退出(没走松手归零),已下发的高速帧会
|
||||
// 让车继续跑到它自己的失联保护为止。与新版 HUD 折叠/退出的 deactivate()
|
||||
// 同一口径。
|
||||
// 必须放在 stopControlLoop() 之前:stopAllMovement 末尾会按 _controlWanted
|
||||
// 决定是否重启循环,这里同步把意图关掉,就不会在退页后留下无主定时器。
|
||||
if (_controlSessionStarted) {
|
||||
unawaited(_cubit.stopAllMovement());
|
||||
_cubit.stopControlLoop();
|
||||
}
|
||||
_cubit.stopReverseCameraMock(); // 🔥 停止倒车影像 Mock,避免定时器空转
|
||||
// 🔥 重置权限状态,避免单例 hasPermission 残留导致下次进入不再查询接口
|
||||
_cubit.resetPermissionState();
|
||||
@@ -153,7 +154,18 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context) => ControlModeEntryGate(
|
||||
deviceId: _modeDeviceId,
|
||||
target: ControlModeTarget.remote,
|
||||
onReady: () {
|
||||
if (_cubit.state.targetDevice?.deviceName != _modeDeviceId) return;
|
||||
_controlSessionStarted = true;
|
||||
_cubit.startControlLoop();
|
||||
},
|
||||
child: _buildPage(context),
|
||||
);
|
||||
|
||||
Widget _buildPage(BuildContext context) {
|
||||
// 🔥 只监听 RemoteControlCubit,避免其他状态变化导致频繁 rebuild
|
||||
final remoteCubit = context.read<RemoteControlCubit>();
|
||||
final targetDevice = remoteCubit.state.targetDevice;
|
||||
@@ -380,12 +392,16 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
Future<void> _showPermissionDialog(RemoteControlState state) async {
|
||||
debugPrint('🔔 [权限弹窗] _showPermissionDialog 被调用');
|
||||
|
||||
final requestingDeviceId = state.requestingDeviceId ?? '未知设备';
|
||||
final requestingPlatform = state.requestingPlatform ?? '未知平台';
|
||||
final dialogContent =
|
||||
'$requestingPlatform 端正在请求控制权\n\n'
|
||||
'设备ID: $requestingDeviceId\n\n'
|
||||
'是否同意释放控制权?';
|
||||
final requestingDeviceId =
|
||||
state.requestingDeviceId ??
|
||||
_l10n.translate('remote_control.perm_unknown_device');
|
||||
final requestingPlatform =
|
||||
state.requestingPlatform ??
|
||||
_l10n.translate('remote_control.perm_unknown_platform');
|
||||
final dialogContent = _l10n
|
||||
.translate('remote_control.permission_request_body')
|
||||
.replaceAll('{platform}', requestingPlatform)
|
||||
.replaceAll('{deviceId}', requestingDeviceId);
|
||||
|
||||
debugPrint(
|
||||
'🔔 [权限弹窗] 准备显示弹窗 - requestingPlatform=$requestingPlatform, requestingDeviceId=$requestingDeviceId',
|
||||
@@ -447,15 +463,16 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
|
||||
// 🔥 关键修复:先重置弹窗标志位,再关闭弹窗
|
||||
// 避免 Navigator.pop() 后上下文失效导致后续代码不执行
|
||||
context.read<RemoteControlCubit>().resetPermissionCoolDown();
|
||||
context.read<RemoteControlCubit>().emit(
|
||||
context.read<RemoteControlCubit>().state.copyWith(
|
||||
showPermissionRequestDialog: false,
|
||||
),
|
||||
);
|
||||
// 🔥 一律用 await 之前就捕获的 remoteCubit,不再 context.read:
|
||||
// 异步回来后页面可能已销毁,在失效 Element 上 context.read 会抛。
|
||||
remoteCubit.resetPermissionCoolDown();
|
||||
remoteCubit.togglePermissionDialog(false);
|
||||
|
||||
// 🔥 关闭弹窗(在状态重置之后)
|
||||
Navigator.pop(dialogContext);
|
||||
// 🔥 关闭弹窗(在状态重置之后):弹窗可能已被上一点击关闭,
|
||||
// 不守卫就会在 defunct context 上 pop 抛异常。
|
||||
if (dialogContext.mounted && Navigator.canPop(dialogContext)) {
|
||||
Navigator.pop(dialogContext);
|
||||
}
|
||||
debugPrint('🔔 [权限弹窗] 弹窗已关闭,重置标志位');
|
||||
},
|
||||
child: Text(
|
||||
@@ -519,15 +536,14 @@ class _RemoteControlPageState extends State<RemoteControlPage> {
|
||||
|
||||
// 🔥 关键修复:先重置弹窗标志位,再关闭弹窗
|
||||
// 避免 Navigator.pop() 后上下文失效导致后续代码不执行
|
||||
context.read<RemoteControlCubit>().resetPermissionCoolDown();
|
||||
context.read<RemoteControlCubit>().emit(
|
||||
context.read<RemoteControlCubit>().state.copyWith(
|
||||
showPermissionRequestDialog: false,
|
||||
),
|
||||
);
|
||||
// 🔥 同拒绝分支:用 await 前就捕获的 remoteCubit,不跨 await 用 context.read。
|
||||
remoteCubit.resetPermissionCoolDown();
|
||||
remoteCubit.togglePermissionDialog(false);
|
||||
|
||||
// 🔥 关闭弹窗(在状态重置之后)
|
||||
Navigator.pop(dialogContext);
|
||||
// 🔥 关闭弹窗(在状态重置之后):同样防重复点击后的 defunct pop。
|
||||
if (dialogContext.mounted && Navigator.canPop(dialogContext)) {
|
||||
Navigator.pop(dialogContext);
|
||||
}
|
||||
debugPrint('🔔 [权限弹窗] 弹窗已关闭,重置标志位');
|
||||
},
|
||||
child: Text(
|
||||
|
||||
Reference in New Issue
Block a user