import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:vitalapp/architecture/utils/confirm_box.dart'; import 'package:vitalapp/components/alert_dialog.dart'; class UsbState { final EventChannel _eventChannel = EventChannel('com.vinno.vitalapp/usb_events'); void startListening() { _eventChannel.receiveBroadcastStream().listen( (dynamic event) { // 这里我们期望event是一个Map,包含USB设备的信息 Map usbEvent = event as Map; print('USB Event: $usbEvent'); String action = usbEvent['action'] ?? 'unknown'; String name = usbEvent['name'] ?? 'unknown'; String type = usbEvent['type'] ?? 'unknown'; if (action == 'attached') { print('USB device attached: Name - $name, Type - $type'); alertReboot(); // USB设备已连接,可以根据需要处理 } else if (action == 'detached') { print('USB device detached: Name - $name, Type - $type'); alertReboot(); // USB设备已断开,可以根据需要处理 } }, onError: (dynamic error) { print('USB Event Error: $error'); }, ); } void alertReboot() { ConfirmBox.showMultiLines( contents: ["检测到 USB 硬件发生变化", "(为保证程序正常运行,建议重启设备)"], width: 500, showCancel: false, ); // Get.dialog( // VAlertDialog( // title: "提示", // width: 420, // content: Container( // height: 32, // padding: const EdgeInsets.symmetric(horizontal: 24), // alignment: Alignment.center, // child: const Text( // "检测到 USB 硬件发生变化,为保证程序正常运行,建议重启设备", // style: TextStyle(fontSize: 20), // ), // ), // showCancel: false, // onConfirm: () async { // Get.back(); // }, // ), // ); } }