Files
flutterApp/lib/features/remote_control/presentation/widgets/emergency_stop_button.dart

144 lines
4.6 KiB
Dart
Raw Normal View History

2026-01-18 20:21:14 +08:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2026-01-19 18:24:24 +08:00
import 'package:flutter_svg/svg.dart';
2026-03-25 16:42:35 +08:00
import 'package:vibration/vibration.dart';
2026-01-18 20:21:14 +08:00
import '../../../../components/capsule_toast.dart';
import '../../../../core/localization/app_localizations.dart';
2026-01-18 20:21:14 +08:00
import '../bloc/remote_control_cubit.dart';
import 'emergency_stop_confirmation.dart';
import '../../../device_control_mode/presentation/widgets/mode_independent_control.dart';
2026-01-18 20:21:14 +08:00
class EmergencyStopButton extends StatefulWidget {
const EmergencyStopButton({super.key, this.width = 120});
2026-01-19 18:24:24 +08:00
final double width;
2026-01-18 20:21:14 +08:00
@override
State<EmergencyStopButton> createState() => _EmergencyStopButtonState();
}
class _EmergencyStopButtonState extends State<EmergencyStopButton> {
bool _isConfirming = false;
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),
);
}
}
2026-01-18 20:21:14 +08:00
@override
Widget build(BuildContext context) =>
ModeIndependentControl(child: _buildButton(context));
2026-01-18 20:21:14 +08:00
Widget _buildButton(BuildContext context) {
final state = context.watch<RemoteControlCubit>().state;
final active = state.isEmergency;
final pending = state.isEmergencyRequestPending;
2026-01-19 18:24:24 +08:00
final width = widget.width * 0.65;
return SizedBox(
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,
),
]
: [],
2026-01-18 20:21:14 +08:00
),
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,
),
),
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',
2026-01-18 20:21:14 +08:00
),
style: TextStyle(
color: Colors.white.withValues(alpha: 0.5),
fontSize: 14,
fontWeight: FontWeight.w900,
),
),
],
2026-01-18 20:21:14 +08:00
),
),
),
2026-01-18 20:21:14 +08:00
),
);
}
2026-03-25 16:42:35 +08:00
Future<void> _vibrate() async {
try {
if (await Vibration.hasVibrator()) {
await Vibration.vibrate(duration: 50, amplitude: 255);
2026-03-25 16:42:35 +08:00
}
} catch (_) {
// 无震动能力时仍可正常发起急停请求。
}
2026-03-25 16:42:35 +08:00
}
2026-01-18 20:21:14 +08:00
}