import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/svg.dart'; import 'package:vibration/vibration.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; @override State createState() => _EmergencyStopButtonState(); } class _EmergencyStopButtonState extends State { bool _isConfirming = false; Future _confirmAndSubmitEmergency(bool on) async { final cubit = context.read(); 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 Widget build(BuildContext context) => ModeIndependentControl(child: _buildButton(context)); Widget _buildButton(BuildContext context) { final state = context.watch().state; final active = state.isEmergency; final pending = state.isEmergencyRequestPending; 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, ), ] : [], ), 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', ), style: TextStyle( color: Colors.white.withValues(alpha: 0.5), fontSize: 14, fontWeight: FontWeight.w900, ), ), ], ), ), ), ), ); } Future _vibrate() async { try { if (await Vibration.hasVibrator()) { await Vibration.vibrate(duration: 50, amplitude: 255); } } catch (_) { // 无震动能力时仍可正常发起急停请求。 } } }