完善遥控权限、控制发送和急停反馈
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import 'package:cc_ui_kit/cc_ui_kit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'remote_control_link_feedback.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
|
||||
import '../bloc/remote_control_cubit.dart';
|
||||
import '../bloc/remote_control_state.dart';
|
||||
import 'emergency_stop_button.dart';
|
||||
|
||||
class CenterControlArea extends StatelessWidget {
|
||||
@@ -43,7 +45,7 @@ class CenterControlArea extends StatelessWidget {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
_buildSliderBox(expandedWidth, 'chassis', true,context),
|
||||
_buildSliderBox(expandedWidth, 'chassis', true, context),
|
||||
SizedBox(height: totalWidth * 0.01),
|
||||
],
|
||||
),
|
||||
@@ -60,7 +62,9 @@ class CenterControlArea extends StatelessWidget {
|
||||
SizedBox(height: totalWidth * 0.05),
|
||||
|
||||
Text(
|
||||
AppLocalizations.of(context).translate('remote_control.switch_view_hint'),
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.switch_view_hint'),
|
||||
style: const TextStyle(fontSize: 8, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
@@ -73,7 +77,31 @@ class CenterControlArea extends StatelessWidget {
|
||||
width: expandedWidth,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [_buildSliderBox(expandedWidth, 'mower', false,context)],
|
||||
children: [
|
||||
_buildSliderBox(expandedWidth, 'mower', false, context),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.mower_last_operation', {
|
||||
'operation': AppLocalizations.of(context).translate(
|
||||
switch (remoteState.lastMowerOperation) {
|
||||
MowerLastOperation.turnOn =>
|
||||
'remote_control.mower_operation_on',
|
||||
MowerLastOperation.turnOff =>
|
||||
'remote_control.mower_operation_off',
|
||||
MowerLastOperation.none =>
|
||||
'remote_control.mower_operation_none',
|
||||
},
|
||||
),
|
||||
}),
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.65),
|
||||
fontSize: 10,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -83,12 +111,17 @@ class CenterControlArea extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSliderBox(double boxWidth, String deviceKey, bool isLeft,BuildContext context) {
|
||||
Widget _buildSliderBox(
|
||||
double boxWidth,
|
||||
String deviceKey,
|
||||
bool isLeft,
|
||||
BuildContext context,
|
||||
) {
|
||||
// 🔥 使用deviceKey作为唯一标识,不依赖显示文本
|
||||
final label = deviceKey == 'chassis'
|
||||
final label = deviceKey == 'chassis'
|
||||
? AppLocalizations.of(context).translate('remote_control.chassis')
|
||||
: AppLocalizations.of(context).translate('remote_control.mower');
|
||||
|
||||
|
||||
return SizedBox(
|
||||
width: boxWidth,
|
||||
child: Center(
|
||||
@@ -98,7 +131,7 @@ class CenterControlArea extends StatelessWidget {
|
||||
child: CCExpandSlider(
|
||||
label: label,
|
||||
svgStart: "assets/svgs/remote_up.svg",
|
||||
svgCenter: deviceKey == 'mower'
|
||||
svgCenter: deviceKey == 'mower'
|
||||
? "assets/svgs/remote_scissors.svg"
|
||||
: "assets/svgs/remote_layers.svg",
|
||||
svgEnd: "assets/svgs/remote_down.svg",
|
||||
@@ -113,7 +146,12 @@ class CenterControlArea extends StatelessWidget {
|
||||
}
|
||||
|
||||
// 🔥 新增:统一的业务逻辑分发方法
|
||||
void _handleSliderAction(String deviceKey, String action, BuildContext context) {
|
||||
void _handleSliderAction(
|
||||
String deviceKey,
|
||||
String action,
|
||||
BuildContext context,
|
||||
) {
|
||||
RemoteControlLinkNotice.showIfDisconnected(context);
|
||||
debugPrint('🎯 [Slider 业务] deviceKey: $deviceKey, action: $action');
|
||||
|
||||
// 🔥 门槛检查:与摇杆一致,未收到 TCP 0x02 推送时禁止操作并提示。
|
||||
@@ -133,7 +171,7 @@ class CenterControlArea extends StatelessWidget {
|
||||
_handleMowerAction(action, context);
|
||||
break;
|
||||
default:
|
||||
debugPrint('⚠️ 未知的 deviceKey: $deviceKey');
|
||||
debugPrint('⚠️ 未知的 deviceKey: $deviceKey');
|
||||
}
|
||||
// 🔥 在指令执行后触发震动反馈(增强手感)
|
||||
HapticFeedback.mediumImpact();
|
||||
@@ -147,40 +185,34 @@ class CenterControlArea extends StatelessWidget {
|
||||
case "start":
|
||||
debugPrint('⬆️ 底盘 - 向上(例如:前进)');
|
||||
// TODO: 发送底盘前进指令
|
||||
cubit.sendChassisCommand(1);
|
||||
cubit.sendChassisCommand(1);
|
||||
break;
|
||||
case "center":
|
||||
debugPrint('️ 底盘 - 中间(例如:停止)');
|
||||
// TODO: 发送底盘停止指令
|
||||
cubit.sendChassisCommand(0);
|
||||
cubit.sendChassisCommand(0);
|
||||
break;
|
||||
case "end":
|
||||
debugPrint('⬇️ 底盘 - 向下(例如:后退)');
|
||||
// TODO: 发送底盘后退指令
|
||||
cubit.sendChassisCommand(2);
|
||||
cubit.sendChassisCommand(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 🔥 割刀的业务逻辑(右滑块)
|
||||
// 割刀开关(右滑块);段位映射由 Cubit 统一处理。
|
||||
void _handleMowerAction(String action, BuildContext context) {
|
||||
final cubit = context.read<RemoteControlCubit>();
|
||||
|
||||
switch (action) {
|
||||
case "start":
|
||||
debugPrint('⬆️ 割刀 - 向上(例如:抬刀)');
|
||||
// TODO: 发送抬刀指令
|
||||
cubit.sendMowerCommand(1);
|
||||
cubit.sendMowerSliderCommand(1);
|
||||
break;
|
||||
case "center":
|
||||
debugPrint('⏹️ 割刀 - 中间(例如:保持)');
|
||||
// TODO: 发送保持指令
|
||||
cubit.sendMowerCommand(0);
|
||||
cubit.sendMowerSliderCommand(0);
|
||||
break;
|
||||
case "end":
|
||||
debugPrint('⬇️ 割刀 - 向下(例如:降刀)');
|
||||
// TODO: 发送降刀指令
|
||||
cubit.sendMowerCommand(2);
|
||||
cubit.sendMowerSliderCommand(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,190 +3,141 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_svg/svg.dart';
|
||||
import 'package:vibration/vibration.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
|
||||
import '../../../../components/capsule_toast.dart';
|
||||
import '../../../../core/localization/app_localizations.dart';
|
||||
import '../bloc/remote_control_cubit.dart';
|
||||
import 'emergency_stop_confirmation.dart';
|
||||
import '../../../device_control_mode/presentation/widgets/mode_independent_control.dart';
|
||||
|
||||
class EmergencyStopButton extends StatefulWidget {
|
||||
const EmergencyStopButton({super.key, this.width = 120});
|
||||
final double width;
|
||||
const EmergencyStopButton({
|
||||
Key? key,
|
||||
this.width = 120,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<EmergencyStopButton> createState() => _EmergencyStopButtonState();
|
||||
}
|
||||
|
||||
class _EmergencyStopButtonState extends State<EmergencyStopButton>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _progressController;
|
||||
bool _isPressing = false;
|
||||
class _EmergencyStopButtonState extends State<EmergencyStopButton> {
|
||||
bool _isConfirming = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_progressController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 3),
|
||||
);
|
||||
|
||||
// 添加监听器,确保动画值变化时重建 UI
|
||||
_progressController.addListener(() {
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
});
|
||||
Future<void> _confirmAndSubmitEmergency(bool on) async {
|
||||
final cubit = context.read<RemoteControlCubit>();
|
||||
if (_isConfirming || cubit.state.isEmergencyRequestPending) return;
|
||||
if (!cubit.checkEmergencyRequest(on)) {
|
||||
CapsuleToast.show(
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.${cubit.state.emergencyFeedbackKey}'),
|
||||
showCheck: false,
|
||||
);
|
||||
return;
|
||||
}
|
||||
final sn = cubit.state.targetDevice?.deviceName;
|
||||
_isConfirming = true;
|
||||
bool confirmed;
|
||||
try {
|
||||
confirmed = await confirmEmergencyStop(context, engage: on, sn: sn ?? '');
|
||||
} finally {
|
||||
_isConfirming = false;
|
||||
}
|
||||
if (!confirmed || !mounted || sn != cubit.state.targetDevice?.deviceName) {
|
||||
return;
|
||||
}
|
||||
await cubit.updateEmergency(on);
|
||||
if (!mounted || sn != cubit.state.targetDevice?.deviceName) return;
|
||||
final key = cubit.state.emergencyFeedbackKey;
|
||||
if (key.isNotEmpty) {
|
||||
CapsuleToast.show(
|
||||
AppLocalizations.of(context).translate('remote_control.$key'),
|
||||
showCheck: false,
|
||||
duration: const Duration(seconds: 3),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_progressController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _resetProgress() {
|
||||
setState(() => _isPressing = false);
|
||||
_progressController.stop();
|
||||
_progressController.value = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isEmergencyActive = context
|
||||
.watch<RemoteControlCubit>()
|
||||
.state
|
||||
.isEmergency;
|
||||
Widget build(BuildContext context) =>
|
||||
ModeIndependentControl(child: _buildButton(context));
|
||||
|
||||
Widget _buildButton(BuildContext context) {
|
||||
final state = context.watch<RemoteControlCubit>().state;
|
||||
final active = state.isEmergency;
|
||||
final pending = state.isEmergencyRequestPending;
|
||||
final width = widget.width * 0.65;
|
||||
final double indicatorSize = width + 20;
|
||||
|
||||
return SizedBox(
|
||||
width: indicatorSize,
|
||||
height: indicatorSize,
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
// 进度环 - 只在按压时显示
|
||||
AnimatedOpacity(
|
||||
opacity: _isPressing ? 1.0 : 0.0,
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: SizedBox(
|
||||
width: indicatorSize,
|
||||
height: indicatorSize,
|
||||
child: CircularProgressIndicator(
|
||||
value: _progressController.value,
|
||||
strokeWidth: 6,
|
||||
color: Colors.red.withOpacity(0.9),
|
||||
backgroundColor: Colors.red.withValues(alpha: 0.2),
|
||||
),
|
||||
width: width + 20,
|
||||
height: width + 20,
|
||||
child: Center(
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: pending
|
||||
? null
|
||||
: () {
|
||||
HapticFeedback.heavyImpact();
|
||||
_vibrate();
|
||||
_confirmAndSubmitEmergency(!active);
|
||||
},
|
||||
child: Container(
|
||||
width: width,
|
||||
height: width,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: active
|
||||
? Colors.red.withValues(alpha: 0.5)
|
||||
: const Color(0x80C53030),
|
||||
boxShadow: active
|
||||
? [
|
||||
BoxShadow(
|
||||
color: Colors.red.withValues(alpha: 0.5),
|
||||
blurRadius: 20,
|
||||
),
|
||||
]
|
||||
: [],
|
||||
),
|
||||
),
|
||||
|
||||
// 按钮主体
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
debugPrint('🔴 [点击] 触发急停');
|
||||
_triggerVibration();
|
||||
if (!isEmergencyActive) {
|
||||
context.read<RemoteControlCubit>().updateEmergency(true);
|
||||
context.read<RemoteControlCubit>().toggleLock();
|
||||
HapticFeedback.heavyImpact();
|
||||
}
|
||||
},
|
||||
onLongPressStart: (_) {
|
||||
debugPrint('🔴 [长按开始] isEmergency=$isEmergencyActive');
|
||||
_triggerVibration();
|
||||
if (isEmergencyActive && !_isPressing) {
|
||||
setState(() => _isPressing = true);
|
||||
debugPrint('🔴 [开始动画] 从 0 开始');
|
||||
_triggerVibration();
|
||||
_progressController.reset();
|
||||
_progressController.forward().then((_) {
|
||||
debugPrint('🔴 [动画完成] _isPressing=$_isPressing');
|
||||
if (_isPressing && mounted) {
|
||||
debugPrint('✅ [解除急停] 执行');
|
||||
_triggerVibration();
|
||||
context.read<RemoteControlCubit>().updateEmergency(false);
|
||||
context.read<RemoteControlCubit>().toggleLock();
|
||||
HapticFeedback.vibrate();
|
||||
setState(() => _isPressing = false);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
onLongPressEnd: (_) {
|
||||
_triggerVibration();
|
||||
debugPrint('🔴 [长按结束]');
|
||||
if (_isPressing) {
|
||||
_resetProgress();
|
||||
|
||||
}
|
||||
},
|
||||
onLongPressCancel: () {
|
||||
_triggerVibration();
|
||||
debugPrint('🔴 [长按取消]');
|
||||
if (_isPressing) {
|
||||
_resetProgress();
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
width: width,
|
||||
height: width,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: isEmergencyActive
|
||||
? Colors.red.withOpacity(0.5)
|
||||
: const Color(0x80C53030),
|
||||
boxShadow: isEmergencyActive
|
||||
? [
|
||||
BoxShadow(
|
||||
color: Colors.red.withValues(alpha: 0.5),
|
||||
blurRadius: 20,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
SvgPicture.asset(
|
||||
'assets/svgs/remote_alert.svg',
|
||||
width: width * 0.4,
|
||||
colorFilter: ColorFilter.mode(
|
||||
Colors.white.withValues(alpha: 0.5),
|
||||
BlendMode.srcIn,
|
||||
),
|
||||
]
|
||||
: [],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
SvgPicture.asset(
|
||||
'assets/svgs/remote_alert.svg',
|
||||
width: width * 0.4,
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
AppLocalizations.of(context).translate(
|
||||
pending
|
||||
? 'remote_control.emergency_submitting'
|
||||
: !state.hasEmergencyStatus
|
||||
? 'remote_control.emergency_unknown'
|
||||
: active
|
||||
? 'remote_control.emergency_stopping'
|
||||
: 'remote_control.emergency_inactive',
|
||||
),
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.5),
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
isEmergencyActive
|
||||
? AppLocalizations.of(context).translate('remote_control.emergency_stopping')
|
||||
: AppLocalizations.of(context).translate('remote_control.emergency_stop'),
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.5),
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _triggerVibration() {
|
||||
Vibration.hasVibrator().then((hasVibrator) {
|
||||
if (hasVibrator ?? false) {
|
||||
Vibration.vibrate(duration: 50, amplitude: 380);
|
||||
debugPrint(' 震动执行 - 10ms');
|
||||
} else {
|
||||
debugPrint(' 设备不支持震动');
|
||||
Future<void> _vibrate() async {
|
||||
try {
|
||||
if (await Vibration.hasVibrator()) {
|
||||
await Vibration.vibrate(duration: 50, amplitude: 255);
|
||||
}
|
||||
}).catchError((e) {
|
||||
debugPrint('震动失败:$e');
|
||||
});
|
||||
} catch (_) {
|
||||
// 无震动能力时仍可正常发起急停请求。
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/localization/app_localizations.dart';
|
||||
|
||||
/// 只确认用户意图;关闭、返回或取消弹窗均不发送控制请求。
|
||||
Future<bool> confirmEmergencyStop(
|
||||
BuildContext context, {
|
||||
required bool engage,
|
||||
required String sn,
|
||||
}) async {
|
||||
final tr = AppLocalizations.of(context);
|
||||
return await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: Text(
|
||||
tr.translate(
|
||||
engage
|
||||
? 'remote_control.emergency_confirm_title'
|
||||
: 'remote_control.emergency_release_title',
|
||||
),
|
||||
),
|
||||
content: Text(
|
||||
tr.translate(
|
||||
engage
|
||||
? 'remote_control.emergency_confirm_message'
|
||||
: 'remote_control.emergency_release_message',
|
||||
{'sn': sn},
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
key: const ValueKey('emergency-cancel'),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: Text(tr.translate('remote_control.emergency_cancel')),
|
||||
),
|
||||
FilledButton(
|
||||
key: const ValueKey('emergency-confirm'),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: Text(
|
||||
tr.translate(
|
||||
engage
|
||||
? 'remote_control.emergency_confirm_stop'
|
||||
: 'remote_control.emergency_confirm_release',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
) ??
|
||||
false;
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import '../../../../core/logging/remote_control_diagnostics.dart';
|
||||
import 'dart:async';
|
||||
import 'package:cc_ui_kit/cc_ui_kit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'remote_control_link_feedback.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:vibration/vibration.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
@@ -28,6 +30,10 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
|
||||
/// 检查是否可以控制,不能则弹出提示
|
||||
bool _checkCanControl(BuildContext context) {
|
||||
if (!_hasShownNoPushDialog &&
|
||||
RemoteControlLinkNotice.showIfDisconnected(context)) {
|
||||
_hasShownNoPushDialog = true;
|
||||
}
|
||||
// 🔥 临时关闭「未收到推送不能控制」门槛,便于机器未开时用 Mock 假数据联调倒车影像。
|
||||
// 联调完成后取消下面注释即可恢复。切勿删除本段逻辑。
|
||||
// final cubit = context.read<RemoteControlCubit>();
|
||||
@@ -88,6 +94,7 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
child: CCJoystick(
|
||||
radius: widget.width,
|
||||
axisHint: AxisHint.forwardBackward,
|
||||
onDiagnostic: (event, fields) => RemoteControlDiagnostics.instance.event('joystick_' + event, fields),
|
||||
onValueChanged: (value) {
|
||||
if (!_checkCanControl(context)) return;
|
||||
// 标记:正在触摸
|
||||
@@ -135,7 +142,7 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
debugPrint('✅ 摇杆已停止 -> 强制 X=0, Y=0 停车');
|
||||
|
||||
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
|
||||
await context.read<RemoteControlCubit>().stopAllMovement();
|
||||
await context.read<RemoteControlCubit>().stopAllMovement(reason: 'forward_release');
|
||||
}
|
||||
|
||||
void _triggerVibration() {
|
||||
@@ -143,4 +150,4 @@ class _LeftJoystickAreaState extends State<LeftJoystickArea> {
|
||||
if (has ?? false) Vibration.vibrate(duration: 12);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../components/capsule_toast.dart';
|
||||
import '../../../../core/localization/app_localizations.dart';
|
||||
import '../bloc/remote_control_cubit.dart';
|
||||
import '../bloc/remote_control_state.dart';
|
||||
|
||||
/// Shared by both control entry points. Telemetry rebuilds never repeat a toast.
|
||||
class PermissionRequestFeedback extends StatelessWidget {
|
||||
const PermissionRequestFeedback({
|
||||
super.key,
|
||||
required this.cubit,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final RemoteControlCubit cubit;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
BlocListener<RemoteControlCubit, RemoteControlState>(
|
||||
bloc: cubit,
|
||||
listenWhen: (previous, current) =>
|
||||
previous.permissionFeedbackId != current.permissionFeedbackId &&
|
||||
current.permissionFeedbackKey.isNotEmpty,
|
||||
listener: (context, state) => CapsuleToast.show(
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.' + state.permissionFeedbackKey),
|
||||
duration: const Duration(seconds: 3),
|
||||
showCheck:
|
||||
state.permissionRequestPhase == PermissionRequestPhase.granted,
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
String permissionStatusText(BuildContext context, RemoteControlState state) {
|
||||
final key = switch (state.permissionRequestPhase) {
|
||||
PermissionRequestPhase.checking => 'permission_checking',
|
||||
PermissionRequestPhase.sending => 'permission_sending',
|
||||
PermissionRequestPhase.waiting => 'permission_waiting',
|
||||
PermissionRequestPhase.timedOut => 'permission_unconfirmed',
|
||||
PermissionRequestPhase.failed when !state.hasPermission =>
|
||||
'permission_request_failed',
|
||||
_ => state.hasPermission ? 'controlling' : 'not_controlling',
|
||||
};
|
||||
return AppLocalizations.of(context).translate('remote_control.' + key);
|
||||
}
|
||||
|
||||
String permissionStatusHint(BuildContext context, RemoteControlState state) =>
|
||||
state.permissionFeedbackKey.isEmpty
|
||||
? permissionStatusText(context, state)
|
||||
: AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.' + state.permissionFeedbackKey);
|
||||
@@ -0,0 +1,106 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
|
||||
import '../bloc/remote_control_cubit.dart';
|
||||
|
||||
/// 只展示已有连接收到的心跳,不发送数据,也不参与重连或控制门控。
|
||||
class RemoteControlHeartbeatIndicator extends StatefulWidget {
|
||||
const RemoteControlHeartbeatIndicator({
|
||||
super.key,
|
||||
required this.heartbeats,
|
||||
this.size = 10,
|
||||
});
|
||||
|
||||
final Stream<void> heartbeats;
|
||||
final double size;
|
||||
|
||||
@override
|
||||
State<RemoteControlHeartbeatIndicator> createState() =>
|
||||
_RemoteControlHeartbeatIndicatorState();
|
||||
}
|
||||
|
||||
class _RemoteControlHeartbeatIndicatorState
|
||||
extends State<RemoteControlHeartbeatIndicator> {
|
||||
StreamSubscription<void>? _subscription;
|
||||
Timer? _pulseTimer;
|
||||
bool _lit = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_listen();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(RemoteControlHeartbeatIndicator oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.heartbeats != widget.heartbeats) {
|
||||
_subscription?.cancel();
|
||||
_pulseTimer?.cancel();
|
||||
_lit = false;
|
||||
_listen();
|
||||
}
|
||||
}
|
||||
|
||||
void _listen() {
|
||||
_subscription = widget.heartbeats.listen((_) {
|
||||
if (!mounted) return;
|
||||
_pulseTimer?.cancel();
|
||||
setState(() => _lit = true);
|
||||
_pulseTimer = Timer(const Duration(milliseconds: 300), () {
|
||||
if (mounted) setState(() => _lit = false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription?.cancel();
|
||||
_pulseTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Tooltip(
|
||||
message: AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.tcp_heartbeat'),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(6),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 100),
|
||||
width: widget.size,
|
||||
height: widget.size,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: _lit ? const Color(0xFF36C878) : const Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 操作时读取现有 TCP 连接状态,仅提示,不拦截、不重连、不改变指令。
|
||||
class RemoteControlLinkNotice {
|
||||
static DateTime? _lastShownAt;
|
||||
|
||||
static bool showIfDisconnected(BuildContext context) {
|
||||
if (sl<RemoteControlCubit>().isControlLinkConnected) return false;
|
||||
final now = DateTime.now();
|
||||
if (_lastShownAt == null ||
|
||||
now.difference(_lastShownAt!) >= const Duration(seconds: 2)) {
|
||||
_lastShownAt = now;
|
||||
CapsuleToast.show(
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.tcp_unavailable'),
|
||||
showCheck: false,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import '../../../../core/logging/remote_control_diagnostics.dart';
|
||||
import 'package:cc_ui_kit/cc_ui_kit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'remote_control_link_feedback.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:vibration/vibration.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
@@ -27,6 +29,10 @@ class _RightJoystickAreaState extends State<RightJoystickArea> {
|
||||
|
||||
/// 检查是否可以控制,不能则弹出提示
|
||||
bool _checkCanControl(BuildContext context) {
|
||||
if (!_hasShownNoPushDialog &&
|
||||
RemoteControlLinkNotice.showIfDisconnected(context)) {
|
||||
_hasShownNoPushDialog = true;
|
||||
}
|
||||
// 🔥 临时关闭「未收到推送不能控制」门槛,便于机器未开时用 Mock 假数据联调倒车影像。
|
||||
// 联调完成后取消下面注释即可恢复。切勿删除本段逻辑。
|
||||
// final cubit = context.read<RemoteControlCubit>();
|
||||
@@ -87,6 +93,7 @@ class _RightJoystickAreaState extends State<RightJoystickArea> {
|
||||
child: CCJoystick(
|
||||
radius: widget.width,
|
||||
axisHint: AxisHint.leftRight,
|
||||
onDiagnostic: (event, fields) => RemoteControlDiagnostics.instance.event('joystick_' + event, fields),
|
||||
onValueChanged: (value) {
|
||||
if (!_checkCanControl(context)) return;
|
||||
int currentX = value.x.toInt();
|
||||
@@ -107,7 +114,7 @@ class _RightJoystickAreaState extends State<RightJoystickArea> {
|
||||
_lastX = 0;
|
||||
|
||||
// 🔥 使用安全方法,一次性清零双轴,确保只发送 (X=0, Y=0)
|
||||
await context.read<RemoteControlCubit>().stopAllMovement();
|
||||
await context.read<RemoteControlCubit>().stopAllMovement(reason: 'turn_release');
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -128,4 +135,4 @@ class _RightJoystickAreaState extends State<RightJoystickArea> {
|
||||
void _triggerVibration() {
|
||||
Vibration.vibrate(duration: 12);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../devices/domain/entities/device_entity.dart';
|
||||
import '../../../devices/presentation/bloc/devices_cubit.dart';
|
||||
import '../../data/models/running_status_model.dart';
|
||||
import 'remote_control_link_feedback.dart';
|
||||
import 'permission_request_feedback.dart';
|
||||
import '../bloc/remote_control_cubit.dart';
|
||||
|
||||
class TopStatusBar extends StatelessWidget {
|
||||
@@ -83,41 +85,37 @@ class TopStatusBar extends StatelessWidget {
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// 2. 控制状态 (对应 StatusChipLeft)
|
||||
StatusChip(
|
||||
text: remoteState.hasPermission
|
||||
? AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.controlling')
|
||||
: AppLocalizations.of(
|
||||
context,
|
||||
).translate('remote_control.not_controlling'),
|
||||
color: remoteState.hasPermission
|
||||
? const Color(0xFF1DB954)
|
||||
: Colors.red,
|
||||
icon: Icons.eighteen_mp,
|
||||
breathing: !remoteState.hasPermission,
|
||||
onTap: () {
|
||||
if (!remoteState.hasPermission) {
|
||||
// 🔥 点击后发起抢占:查权限 + 无权限则发 TCP switch_control
|
||||
debugPrint('🔑 [TopStatusBar] 👆 用户手动点击“未在控制”,请求抢占控制权');
|
||||
final targetDevice =
|
||||
_remoteControlCubit.state.targetDevice;
|
||||
if (targetDevice != null) {
|
||||
PermissionRequestFeedback(
|
||||
cubit: _remoteControlCubit,
|
||||
child: Tooltip(
|
||||
message: permissionStatusHint(context, remoteState),
|
||||
child: StatusChip(
|
||||
text: permissionStatusText(context, remoteState),
|
||||
color: remoteState.isPermissionRequestBusy
|
||||
? Colors.orange
|
||||
: remoteState.hasPermission
|
||||
? const Color(0xFF1DB954)
|
||||
: Colors.red,
|
||||
icon: Icons.eighteen_mp,
|
||||
breathing: !remoteState.hasPermission,
|
||||
onTap: () {
|
||||
if (remoteState.hasPermission || remoteState.isPermissionRequestBusy) return;
|
||||
_remoteControlCubit.requestControlPermissionS(
|
||||
targetDevice.deviceName,
|
||||
_remoteControlCubit.state.targetDevice?.deviceName ?? '',
|
||||
'app',
|
||||
source: '手动点击',
|
||||
grabControl: true,
|
||||
);
|
||||
} else {
|
||||
debugPrint('❌ [TopStatusBar] targetDevice 为空,无法请求权限');
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
|
||||
// 3. 锁定状态 (对应 SmallFunctionButton)
|
||||
RemoteControlHeartbeatIndicator(
|
||||
heartbeats: _remoteControlCubit.serverHeartbeats,
|
||||
),
|
||||
_buildIconButton(
|
||||
remoteState.isLocked
|
||||
? "assets/svgs/remote_lock.svg"
|
||||
@@ -476,6 +474,7 @@ class TopStatusBar extends StatelessWidget {
|
||||
|
||||
// 🔥 新增:统一的业务逻辑分发方法
|
||||
void _handleSliderAction(String label, String action, BuildContext context) {
|
||||
RemoteControlLinkNotice.showIfDisconnected(context);
|
||||
debugPrint('🎯 [Slider 业务] label: $label, action: $action');
|
||||
|
||||
// 🔥 根据 label 区分不同的设备
|
||||
|
||||
Reference in New Issue
Block a user