119 lines
3.6 KiB
Dart
119 lines
3.6 KiB
Dart
// user_info_card.dart
|
|
import 'package:flutter/material.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart'; // 1. 导入扫码库
|
|
import 'package:get_it/get_it.dart'; // 用于获取 Cubit 实例
|
|
// 2. 导入你的 Cubit 文件,请根据实际路径调整
|
|
import '../../../auth/presentation/bloc/auth_cubit.dart';
|
|
|
|
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,
|
|
),
|
|
),
|
|
),
|
|
SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(userName, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
Text('用户 id:$userId', style: TextStyle(fontSize: 12, color: Colors.grey)),
|
|
],
|
|
),
|
|
),
|
|
IconButton(
|
|
icon: Icon(Icons.edit),
|
|
onPressed: () {},
|
|
),
|
|
Spacer(),
|
|
IconButton(
|
|
icon: Icon(Icons.add),
|
|
// 3. 绑定点击事件
|
|
onPressed: () => _handleScanAndBind(context),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
// 4. 定义扫码与绑定逻辑
|
|
Future<void> _handleScanAndBind(BuildContext context) async {
|
|
// 跳转到扫码页面
|
|
final String? qrCode = await Navigator.push<String>(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => MobileScanner(
|
|
onDetect: (capture) {
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
if (barcodes.isNotEmpty) {
|
|
debugPrint('识别到的数据: $barcodes');
|
|
final String? code = barcodes.first.rawValue;
|
|
if (code != null && code.isNotEmpty) {
|
|
// 识别成功,立即返回结果并关闭页面
|
|
debugPrint('扫码成功,识别到的数据: $code');
|
|
Navigator.pop(context, code);
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
// 如果用户取消或未扫到码,直接返回
|
|
if (qrCode == null || qrCode.isEmpty) {
|
|
return;
|
|
}
|
|
|
|
// 显示加载对话框
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (context) => Center(child: CircularProgressIndicator()),
|
|
);
|
|
|
|
try {
|
|
// 5. 获取 Cubit 实例并调用绑定接口
|
|
// 假设 AuthCubit 中已定义 bindDeviceByQr 方法
|
|
final Cubit = GetIt.I<DevicesCubit>();
|
|
|
|
// 执行绑定逻辑 (请确保 Cubit 层已实现此方法)
|
|
//await Cubit.bindDevice(qrCode);
|
|
|
|
// 关闭加载框
|
|
if (context.mounted) Navigator.pop(context);
|
|
|
|
// 成功提示
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('设备绑定成功!'), backgroundColor: Colors.green),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
// 关闭加载框
|
|
if (context.mounted) Navigator.pop(context);
|
|
|
|
// 失败提示
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('绑定失败:$e'), backgroundColor: Colors.red),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|