Merge branch 'feature/my' of http://1.95.137.212:57001/APP/FlutterApp into feature/my

This commit is contained in:
mmc
2026-03-05 14:39:23 +08:00

View File

@@ -183,6 +183,7 @@ class _UserInfoCardState extends State<UserInfoCard> {
} }
// 扫码绑定设备逻辑(保留) // 扫码绑定设备逻辑(保留)
// 扫码绑定设备逻辑(修复版)
Future<void> _handleScanAndBind(BuildContext context) async { Future<void> _handleScanAndBind(BuildContext context) async {
bool isScanned = false; bool isScanned = false;
@@ -208,7 +209,6 @@ class _UserInfoCardState extends State<UserInfoCard> {
final String? code = barcodes.first.rawValue; final String? code = barcodes.first.rawValue;
if (code != null && code.isNotEmpty) { if (code != null && code.isNotEmpty) {
isScanned = true; isScanned = true;
// 使用 addPostFrameCallback 确保在帧渲染后执行,避免锁死
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
if (context.mounted && Navigator.canPop(context)) { if (context.mounted && Navigator.canPop(context)) {
Navigator.pop(context, code); Navigator.pop(context, code);
@@ -261,32 +261,38 @@ class _UserInfoCardState extends State<UserInfoCard> {
return; return;
} }
// 3. 显示加载框 // 3. 显示加载框:保存加载弹窗的 BuildContext
// 保存 BuildContext 引用,防止后续 context 变化导致找不到正确的 Navigator BuildContext? loadingContext; // 新增:保存加载弹窗的上下文
showDialog( showDialog(
context: context, context: context,
barrierDismissible: false, barrierDismissible: false,
builder: (loadingContext) => const Center(child: CircularProgressIndicator()), builder: (context) {
loadingContext = context; // 保存这个 context
return const Center(child: CircularProgressIndicator());
},
); );
try { try {
final devicesCubit = GetIt.I<DevicesCubit>(); final devicesCubit = GetIt.I<DevicesCubit>();
await devicesCubit.bindDevice(deviceCode, ''); await devicesCubit.bindDevice(deviceCode, '');
// ✅ 成功:安全关闭加载框 // ✅ 成功:用 loadingContext 关闭弹窗
if (context.mounted && Navigator.canPop(context)) { if (loadingContext != null && Navigator.canPop(loadingContext!)) {
Navigator.pop(context); Navigator.pop(loadingContext!);
}
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('设备绑定成功!'), backgroundColor: Colors.green), const SnackBar(content: Text('设备绑定成功!'), backgroundColor: Colors.green),
); );
} }
} catch (e) { } catch (e) {
// ✅ 失败:安全关闭加载框 (关键修复点) // ❌ 失败:用 loadingContext 关闭弹窗
// 即使发生了 Framework 错误,也要尝试关闭弹窗 if (loadingContext != null && Navigator.canPop(loadingContext!)) {
if (context.mounted && Navigator.canPop(context)) { Navigator.pop(loadingContext!);
Navigator.pop(context); }
// 显示具体错误信息 if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text('绑定失败:${e.toString()}'), content: Text('绑定失败:${e.toString()}'),
@@ -294,20 +300,6 @@ class _UserInfoCardState extends State<UserInfoCard> {
duration: const Duration(seconds: 4), duration: const Duration(seconds: 4),
), ),
); );
} else {
// 极端情况:如果 Navigator 已经锁死无法 pop,尝试用其他方式提示用户
// 或者等待下一帧再试
WidgetsBinding.instance.addPostFrameCallback((_) {
if (context.mounted) {
// 尝试再次关闭,或者只显示错误提示
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('绑定失败:${e.toString()} (请手动返回)'),
backgroundColor: Colors.orange,
),
);
}
});
} }
debugPrint('设备绑定失败详情:$e'); debugPrint('设备绑定失败详情:$e');
} }