2026-02-25 13:41:23 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
2026-03-04 17:36:28 +08:00
|
|
|
|
import 'package:flutter/foundation.dart';
|
2026-03-04 10:22:12 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
2026-03-04 17:36:28 +08:00
|
|
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
|
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
|
// 其他导入保持不变...
|
2026-02-25 13:41:23 +08:00
|
|
|
|
|
|
|
|
|
|
class UserInfoCard extends StatelessWidget {
|
|
|
|
|
|
final String userName;
|
|
|
|
|
|
final String userId;
|
|
|
|
|
|
|
|
|
|
|
|
const UserInfoCard({super.key, required this.userName, required this.userId});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
return Row(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Container(
|
|
|
|
|
|
width: 48,
|
|
|
|
|
|
height: 48,
|
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
|
image: DecorationImage(
|
|
|
|
|
|
image: NetworkImage('https://pic4.zhimg.com/v2-c34c61a90095abb8713de9d1dca7ec7b_r.jpg'),
|
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-03-04 17:36:28 +08:00
|
|
|
|
const SizedBox(width: 12),
|
2026-02-25 13:41:23 +08:00
|
|
|
|
Expanded(
|
|
|
|
|
|
child: Column(
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
|
children: [
|
2026-03-04 17:36:28 +08:00
|
|
|
|
Text(userName, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
|
|
|
|
Text('用户 id:$userId', style: const TextStyle(fontSize: 12, color: Colors.grey)),
|
2026-02-25 13:41:23 +08:00
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
IconButton(
|
2026-03-04 17:36:28 +08:00
|
|
|
|
icon: const Icon(Icons.edit),
|
2026-02-25 13:41:23 +08:00
|
|
|
|
onPressed: () {},
|
|
|
|
|
|
),
|
2026-03-04 17:36:28 +08:00
|
|
|
|
const Spacer(),
|
2026-02-25 13:41:23 +08:00
|
|
|
|
IconButton(
|
2026-03-04 17:36:28 +08:00
|
|
|
|
icon: const Icon(Icons.add),
|
2026-03-04 10:22:12 +08:00
|
|
|
|
onPressed: () => _handleScanAndBind(context),
|
2026-02-25 13:41:23 +08:00
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-03-04 10:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
Future<void> _handleScanAndBind(BuildContext context) async {
|
2026-03-04 17:36:28 +08:00
|
|
|
|
bool isScanned = false;
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 扫码逻辑 (保持不变)
|
|
|
|
|
|
final String? deviceCode = await Navigator.push<String>(
|
2026-03-04 10:22:12 +08:00
|
|
|
|
context,
|
|
|
|
|
|
MaterialPageRoute(
|
2026-03-04 17:36:28 +08:00
|
|
|
|
builder: (context) => Scaffold(
|
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
|
title: const Text('扫码绑定设备'),
|
|
|
|
|
|
backgroundColor: Colors.black54,
|
|
|
|
|
|
leading: IconButton(
|
|
|
|
|
|
icon: const Icon(Icons.close),
|
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
body: MobileScanner(
|
|
|
|
|
|
onDetect: (BarcodeCapture capture) {
|
|
|
|
|
|
if (isScanned) return;
|
|
|
|
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
|
|
|
|
if (barcodes.isNotEmpty) {
|
|
|
|
|
|
final String? code = barcodes.first.rawValue;
|
|
|
|
|
|
if (code != null && code.isNotEmpty) {
|
|
|
|
|
|
isScanned = true;
|
|
|
|
|
|
// 使用 addPostFrameCallback 确保在帧渲染后执行,避免锁死
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
if (context.mounted && Navigator.canPop(context)) {
|
|
|
|
|
|
Navigator.pop(context, code);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-03-04 10:22:12 +08:00
|
|
|
|
}
|
2026-03-04 17:36:28 +08:00
|
|
|
|
},
|
|
|
|
|
|
),
|
2026-03-04 10:22:12 +08:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
if (deviceCode == null || deviceCode.isEmpty) {
|
2026-03-04 10:22:12 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
// 2. 确认对话框 (保持不变)
|
|
|
|
|
|
final bool? confirmBind = await showDialog<bool>(
|
2026-03-04 10:22:12 +08:00
|
|
|
|
context: context,
|
|
|
|
|
|
barrierDismissible: false,
|
2026-03-04 17:36:28 +08:00
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
|
title: const Text('确认绑定设备'),
|
|
|
|
|
|
content: Column(
|
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
|
children: [
|
|
|
|
|
|
const Text('设备信息:'),
|
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
|
Text('设备编码:$deviceCode'),
|
|
|
|
|
|
const Text('设备状态:未绑定'),
|
|
|
|
|
|
const Text('设备类型:智能设备'),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, false),
|
|
|
|
|
|
child: const Text('取消'),
|
|
|
|
|
|
),
|
|
|
|
|
|
ElevatedButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, true),
|
|
|
|
|
|
child: const Text('确定绑定'),
|
|
|
|
|
|
style: ElevatedButton.styleFrom(backgroundColor: Colors.blue),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
2026-03-04 10:22:12 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
if (confirmBind != true) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-03-04 10:22:12 +08:00
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
// 3. 显示加载框
|
|
|
|
|
|
// 保存 BuildContext 引用,防止后续 context 变化导致找不到正确的 Navigator
|
|
|
|
|
|
showDialog(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
|
builder: (loadingContext) => const Center(child: CircularProgressIndicator()),
|
|
|
|
|
|
);
|
2026-03-04 10:22:12 +08:00
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
try {
|
|
|
|
|
|
final devicesCubit = GetIt.I<DevicesCubit>();
|
|
|
|
|
|
await devicesCubit.bindDevice(deviceCode, '');
|
2026-03-04 10:22:12 +08:00
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
// ✅ 成功:安全关闭加载框
|
|
|
|
|
|
if (context.mounted && Navigator.canPop(context)) {
|
|
|
|
|
|
Navigator.pop(context);
|
2026-03-04 10:22:12 +08:00
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2026-03-04 17:36:28 +08:00
|
|
|
|
const SnackBar(content: Text('设备绑定成功!'), backgroundColor: Colors.green),
|
2026-03-04 10:22:12 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2026-03-04 17:36:28 +08:00
|
|
|
|
// ✅ 失败:安全关闭加载框 (关键修复点)
|
|
|
|
|
|
// 即使发生了 Framework 错误,也要尝试关闭弹窗
|
|
|
|
|
|
if (context.mounted && Navigator.canPop(context)) {
|
|
|
|
|
|
Navigator.pop(context);
|
2026-03-04 10:22:12 +08:00
|
|
|
|
|
2026-03-04 17:36:28 +08:00
|
|
|
|
// 显示具体错误信息
|
2026-03-04 10:22:12 +08:00
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2026-03-04 17:36:28 +08:00
|
|
|
|
SnackBar(
|
|
|
|
|
|
content: Text('绑定失败:${e.toString()}'),
|
|
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
|
|
),
|
2026-03-04 10:22:12 +08:00
|
|
|
|
);
|
2026-03-04 17:36:28 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 极端情况:如果 Navigator 已经锁死无法 pop,尝试用其他方式提示用户
|
|
|
|
|
|
// 或者等待下一帧再试
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
if (context.mounted) {
|
|
|
|
|
|
// 尝试再次关闭,或者只显示错误提示
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
|
SnackBar(
|
|
|
|
|
|
content: Text('绑定失败:${e.toString()} (请手动返回)'),
|
|
|
|
|
|
backgroundColor: Colors.orange,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-03-04 10:22:12 +08:00
|
|
|
|
}
|
2026-03-04 17:36:28 +08:00
|
|
|
|
debugPrint('设备绑定失败详情:$e');
|
2026-03-04 10:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-04 17:36:28 +08:00
|
|
|
|
|
2026-02-25 13:41:23 +08:00
|
|
|
|
}
|