重整多余的UI组件和未定功能的逻辑链路的修正

This commit is contained in:
2026-11-11 16:33:04 +08:00
parent 34cb9d2805
commit 47f518f94c
100 changed files with 5789 additions and 3070 deletions

View File

@@ -13,6 +13,8 @@ Future<bool> showControlModeEntryDialog(
required String deviceId,
required ControlModeTarget target,
String? deviceName,
bool allowTargetSelection = false,
bool cancelToPreview = false,
ControlModeEntryCubit? cubit,
}) async {
final entry =
@@ -25,12 +27,22 @@ Future<bool> showControlModeEntryDialog(
);
try {
entry.start();
return await showDialog<bool>(
return await showModalBottomSheet<bool>(
context: context,
barrierDismissible: false,
useRootNavigator: true,
useSafeArea: true,
backgroundColor: Colors.transparent,
barrierColor: const Color(0x8C0B1B2B),
isScrollControlled: true,
isDismissible: false,
enableDrag: false,
builder: (_) => BlocProvider.value(
value: entry,
child: _ControlModeEntryDialog(deviceName: deviceName),
child: _ControlModeEntryDialog(
deviceName: deviceName,
allowTargetSelection: allowTargetSelection,
cancelToPreview: cancelToPreview,
),
),
) ??
false;
@@ -39,7 +51,7 @@ Future<bool> showControlModeEntryDialog(
}
}
/// 当前模式已符合或切换接口成功后进入;不改动摇杆归零与停止协议。
/// 模式确认后启用操作。启用预览时,取消确认会保留页面画面和重试入口。
class ControlModeEntryGate extends StatefulWidget {
const ControlModeEntryGate({
super.key,
@@ -48,12 +60,14 @@ class ControlModeEntryGate extends StatefulWidget {
required this.child,
this.deviceName,
this.onReady,
this.cancelToPreview = false,
});
final String deviceId;
final String? deviceName;
final ControlModeTarget target;
final Widget child;
final VoidCallback? onReady;
final bool cancelToPreview;
@override
State<ControlModeEntryGate> createState() => _ControlModeEntryGateState();
}
@@ -78,11 +92,14 @@ class _ControlModeEntryGateState extends State<ControlModeEntryGate> {
deviceId: widget.deviceId,
deviceName: widget.deviceName,
target: widget.target,
cancelToPreview: widget.cancelToPreview,
);
if (!mounted) return;
if (accepted) {
setState(() => _ready = true);
widget.onReady?.call();
} else if (widget.cancelToPreview) {
setState(() => _cancelled = true);
} else {
final popped = await Navigator.of(context).maybePop();
if (!popped && mounted) setState(() => _cancelled = true);
@@ -96,8 +113,13 @@ class _ControlModeEntryGateState extends State<ControlModeEntryGate> {
Widget build(BuildContext context) => Stack(
fit: StackFit.expand,
children: [
IgnorePointer(ignoring: !_ready, child: widget.child),
if (_cancelled)
ExcludeFocus(
excluding: !_ready,
child: IgnorePointer(ignoring: !_ready, child: widget.child),
),
if (_cancelled && widget.cancelToPreview)
_buildPreviewControls(context)
else if (_cancelled)
ColoredBox(
color: Colors.black54,
child: Center(
@@ -113,11 +135,144 @@ class _ControlModeEntryGateState extends State<ControlModeEntryGate> {
),
],
);
Widget _buildPreviewControls(BuildContext context) {
final l10n = AppLocalizations.of(context);
return Positioned(
left: 0,
right: 0,
bottom: 0,
child: SafeArea(
top: false,
minimum: const EdgeInsets.fromLTRB(12, 0, 12, 10),
child: Align(
alignment: Alignment.bottomCenter,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 520),
child: Material(
color: const Color(0xF21A2B4A),
borderRadius: BorderRadius.circular(14),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 4,
),
child: Row(
children: [
const Icon(
Icons.visibility_outlined,
color: Colors.white,
size: 18,
),
const SizedBox(width: 8),
Expanded(
child: Text(
l10n.translate('device_control_mode.preview_status'),
style: const TextStyle(
fontSize: 12,
color: Colors.white,
),
),
),
TextButton(
onPressed: _enter,
style: TextButton.styleFrom(
foregroundColor: Colors.white,
),
child: Text(
l10n.translate('device_control_mode.confirm_mode'),
),
),
IconButton(
tooltip: l10n.translate('common.back'),
onPressed: () => Navigator.of(context).maybePop(),
icon: const Icon(
Icons.arrow_back_rounded,
color: Colors.white,
size: 20,
),
),
],
),
),
),
),
),
),
);
}
}
class _ControlModeEntryDialog extends StatelessWidget {
const _ControlModeEntryDialog({this.deviceName});
const _ControlModeEntryDialog({
this.deviceName,
this.allowTargetSelection = false,
this.cancelToPreview = false,
});
static const _navy = Color(0xFF1A2B4A);
static const _gray = Color(0xFF86909C);
static const _blue = Color(0xFF1C96F6);
static const _border = Color(0xFFE5EDF7);
static const _softBlue = Color(0xFFEAF4FE);
static const _noteText = Color(0xFF5C6B84);
final String? deviceName;
final bool allowTargetSelection;
final bool cancelToPreview;
Widget _modeCard({
required String label,
required String value,
required IconData icon,
bool target = false,
}) {
return Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: target ? _softBlue : const Color(0xFFF6FAFE),
borderRadius: BorderRadius.circular(14),
border: Border.all(color: target ? const Color(0xFFD6E9FC) : _border),
),
child: Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
),
child: Icon(icon, color: _blue, size: 21),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: const TextStyle(fontSize: 11.5, color: _gray),
),
const SizedBox(height: 4),
Text(
value,
style: const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w700,
color: _navy,
),
),
],
),
),
if (target)
const Icon(Icons.check_circle_rounded, size: 21, color: _blue),
],
),
);
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
@@ -132,8 +287,14 @@ class _ControlModeEntryDialog extends StatelessWidget {
debugPrint(
'[控制模式切换][弹窗状态] deviceId=${cubit.deviceId}, currentRawMode=${state.reading?.rawMode}, targetMode=${cubit.target.value}, phase=${state.phase.name}, confirmEnabled=${state.canConfirm}',
);
if (state.phase == ControlModeEntryPhase.confirmed) {
Navigator.of(context).pop(true);
if (state.phase != ControlModeEntryPhase.confirmed) return;
final route = ModalRoute.of(context);
if (route == null || !route.isActive) return;
final navigator = Navigator.of(context);
if (route.isCurrent) {
navigator.pop(true);
} else {
navigator.removeRoute(route, true);
}
},
builder: (context, state) {
@@ -142,76 +303,247 @@ class _ControlModeEntryDialog extends StatelessWidget {
l10n,
'${cubit.target.value}',
);
final note = state.busy
? tr('submitting')
: matches
? tr(allowTargetSelection ? 'selection_matched' : 'matched')
: state.reading?.hasReportedMode == true
? tr(
allowTargetSelection ? 'selection_hint' : 'mismatch',
).replaceAll('%s', targetText)
: tr('unknown_mode').replaceAll('%s', targetText);
final error = state.errorKey != null
? tr(state.errorKey!)
: state.error;
final canCancel = cancelToPreview || !state.busy;
final cancelText = cancelToPreview
? tr('cancel_to_preview')
: l10n.translate('common.cancel');
void cancel() {
if (ModalRoute.of(context)?.isCurrent == true) {
Navigator.of(context).pop(false);
}
}
return PopScope(
canPop: !state.busy,
child: AlertDialog(
title: Text(tr('title')),
content: SizedBox(
width: 380,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
deviceName?.trim().isNotEmpty == true
? '$deviceName · ${cubit.deviceId}'
: cubit.deviceId,
),
const SizedBox(height: 12),
Text(
'${tr('current')}:${controlModeDisplayText(l10n, state.reading?.rawMode)}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
canPop: canCancel,
child: ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.9,
),
child: Material(
color: Colors.white,
borderRadius: const BorderRadius.vertical(
top: Radius.circular(24),
),
clipBehavior: Clip.antiAlias,
child: SafeArea(
top: false,
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(18, 16, 18, 16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Text(
tr(
allowTargetSelection ? 'switch_title' : 'title',
),
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: _navy,
),
),
),
IconButton(
tooltip: cancelToPreview
? cancelText
: l10n.translate('common.close'),
onPressed: canCancel ? cancel : null,
icon: const Icon(Icons.close_rounded, size: 22),
color: _navy,
visualDensity: VisualDensity.compact,
),
],
),
),
const SizedBox(height: 8),
Text('${tr('target')}:$targetText'),
const SizedBox(height: 12),
Text(
state.busy
? tr('submitting')
: matches
? tr('matched')
: state.reading?.hasReportedMode == true
? tr('mismatch').replaceAll('%s', targetText)
: tr('unknown_mode').replaceAll('%s', targetText),
),
if (state.error != null || state.errorKey != null) ...[
const SizedBox(height: 10),
const SizedBox(height: 8),
if (deviceName?.trim().isNotEmpty == true) ...[
Text(
deviceName!,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w700,
color: _navy,
),
),
const SizedBox(height: 4),
],
Text(
state.errorKey != null
? tr(state.errorKey!)
: state.error!,
style: TextStyle(
color: Theme.of(context).colorScheme.error,
cubit.deviceId,
style: const TextStyle(
fontSize: 11.5,
height: 1.5,
color: _gray,
),
),
],
if (state.busy) ...[
const SizedBox(height: 16),
_modeCard(
label: tr('current'),
value: controlModeDisplayText(
l10n,
state.reading?.rawMode,
),
icon: Icons.sensors_rounded,
),
const SizedBox(height: 10),
// 自主任务选项继续停用,由后端处理;这里展示本次确认的目标。
_modeCard(
label: tr(
allowTargetSelection ? 'selection_target' : 'target',
),
value:
allowTargetSelection &&
cubit.target == ControlModeTarget.remote
? tr('switch_manual')
: targetText,
icon: Icons.swap_horiz_rounded,
target: true,
),
const SizedBox(height: 12),
const LinearProgressIndicator(),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12,
vertical: 10,
),
decoration: BoxDecoration(
color: _softBlue,
borderRadius: BorderRadius.circular(10),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.only(top: 2),
child: Icon(
Icons.info_outline_rounded,
size: 16,
color: _blue,
),
),
const SizedBox(width: 8),
Expanded(
child: Text(
note,
style: const TextStyle(
fontSize: 12,
height: 1.5,
color: _noteText,
),
),
),
],
),
),
if (error != null) ...[
const SizedBox(height: 10),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: const Color(0xFFFFF1F0),
borderRadius: BorderRadius.circular(10),
),
child: Text(
error,
style: const TextStyle(
fontSize: 12,
height: 1.5,
color: Color(0xFFF0483E),
),
),
),
],
if (state.phase == ControlModeEntryPhase.failed &&
state.errorKey != 'invalid_device')
Align(
alignment: Alignment.centerRight,
child: TextButton.icon(
onPressed: cubit.start,
icon: const Icon(Icons.refresh_rounded, size: 17),
label: Text(tr('retry')),
style: TextButton.styleFrom(foregroundColor: _blue),
),
),
const SizedBox(height: 18),
Row(
children: [
Expanded(
child: OutlinedButton.icon(
onPressed: canCancel ? cancel : null,
icon: const Icon(Icons.close_rounded, size: 18),
style: OutlinedButton.styleFrom(
foregroundColor: _blue,
minimumSize: const Size.fromHeight(48),
side: BorderSide(
color: canCancel ? _blue : _border,
width: 1.2,
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
),
),
label: Text(cancelText),
),
),
const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: state.canConfirm
? cubit.confirm
: null,
style: FilledButton.styleFrom(
backgroundColor: _blue,
foregroundColor: Colors.white,
disabledBackgroundColor: const Color(
0xFFDCEBFA,
),
disabledForegroundColor: _gray,
minimumSize: const Size.fromHeight(48),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
textStyle: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.w700,
),
),
child: state.busy
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: _blue,
),
)
: Text(l10n.translate('common.ok')),
),
),
],
),
],
],
),
),
),
),
actions: [
TextButton(
onPressed: state.busy
? null
: () => Navigator.of(context).pop(false),
child: Text(l10n.translate('common.back')),
),
if (state.phase == ControlModeEntryPhase.failed &&
state.errorKey != 'invalid_device')
TextButton(onPressed: cubit.start, child: Text(tr('retry'))),
FilledButton(
onPressed: state.canConfirm ? cubit.confirm : null,
child: Text(l10n.translate('common.ok')),
),
],
),
);
},