完成扫码绑定的cubit层
完成扫码依赖的下载和权限配置 完成扫码绑定设备-初步测试打印扫码结果
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
// 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;
|
||||
@@ -28,7 +33,7 @@ class UserInfoCard extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(userName, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
||||
Text('用户id:$userId', style: TextStyle(fontSize: 12, color: Colors.grey)),
|
||||
Text('用户 id:$userId', style: TextStyle(fontSize: 12, color: Colors.grey)),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -39,9 +44,75 @@ class UserInfoCard extends StatelessWidget {
|
||||
Spacer(),
|
||||
IconButton(
|
||||
icon: Icon(Icons.add),
|
||||
onPressed: () {},
|
||||
// 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user