From 44ffedb56aa21d8682c48fadfe3823687a8c8791 Mon Sep 17 00:00:00 2001 From: wangjia <809946525@qq.com> Date: Mon, 25 May 2026 20:41:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(client):=20=E6=89=93=E5=8D=B0=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=8A=A0=20try-catch=EF=BC=8C=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BB=A5=20SnackBar=20=E6=8F=90=E7=A4=BA=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- client/lib/core/utils/print_util_stub.dart | 18 +++++++++++--- .../stock_in/stock_in_list_screen.dart | 24 ++++++++++++++++--- .../stock_out/stock_out_list_screen.dart | 24 ++++++++++++++++--- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/client/lib/core/utils/print_util_stub.dart b/client/lib/core/utils/print_util_stub.dart index cf1428f..2a6b7e8 100644 --- a/client/lib/core/utils/print_util_stub.dart +++ b/client/lib/core/utils/print_util_stub.dart @@ -189,7 +189,11 @@ Future printProductLabelImpl({ ), )); - await Printing.layoutPdf(onLayout: (_) async => doc.save()); + try { + await Printing.layoutPdf(onLayout: (_) async => doc.save()); + } catch (e) { + throw Exception('打印失败,请检查打印机连接和驱动是否正常。\n详情:$e'); + } } pw.Widget _label2ColRow( @@ -424,7 +428,11 @@ Future printStockInOrderImpl(StockInOrder order) async { ), ], )); - await Printing.layoutPdf(onLayout: (_) async => doc.save()); + try { + await Printing.layoutPdf(onLayout: (_) async => doc.save()); + } catch (e) { + throw Exception('打印失败,请检查打印机连接和驱动是否正常。\n详情:$e'); + } } Future printStockOutOrderImpl(StockOutOrder order) async { @@ -474,5 +482,9 @@ Future printStockOutOrderImpl(StockOutOrder order) async { ), ], )); - await Printing.layoutPdf(onLayout: (_) async => doc.save()); + try { + await Printing.layoutPdf(onLayout: (_) async => doc.save()); + } catch (e) { + throw Exception('打印失败,请检查打印机连接和驱动是否正常。\n详情:$e'); + } } diff --git a/client/lib/screens/stock_in/stock_in_list_screen.dart b/client/lib/screens/stock_in/stock_in_list_screen.dart index 5b9450a..5c9a355 100644 --- a/client/lib/screens/stock_in/stock_in_list_screen.dart +++ b/client/lib/screens/stock_in/stock_in_list_screen.dart @@ -247,8 +247,16 @@ class _StockInListScreenState extends ConsumerState { ), TextButton( onPressed: () async { - final order = await ref.read(stockInRepositoryProvider).get(o.id); - await printStockInOrder(order); + try { + final order = await ref.read(stockInRepositoryProvider).get(o.id); + await printStockInOrder(order); + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), + ); + } + } }, child: const Text('打印', style: TextStyle(fontSize: 12, color: AppTheme.primary)), @@ -690,7 +698,17 @@ class _StockInDetailDialogState extends ConsumerState<_StockInDetailDialog> { IconButton( icon: const Icon(Icons.print_outlined, color: Colors.white), tooltip: '打印', - onPressed: () => printStockInOrder(_loadedOrder!), + onPressed: () async { + try { + await printStockInOrder(_loadedOrder!); + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), + ); + } + } + }, ), IconButton( icon: const Icon(Icons.close, color: Colors.white), diff --git a/client/lib/screens/stock_out/stock_out_list_screen.dart b/client/lib/screens/stock_out/stock_out_list_screen.dart index a17e669..7b44e23 100644 --- a/client/lib/screens/stock_out/stock_out_list_screen.dart +++ b/client/lib/screens/stock_out/stock_out_list_screen.dart @@ -254,8 +254,16 @@ class _StockOutListScreenState extends ConsumerState { ), TextButton( onPressed: () async { - final order = await ref.read(stockOutRepositoryProvider).get(o.id); - await printStockOutOrder(order); + try { + final order = await ref.read(stockOutRepositoryProvider).get(o.id); + await printStockOutOrder(order); + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), + ); + } + } }, child: const Text('打印', style: TextStyle(fontSize: 12, color: AppTheme.primary)), @@ -682,7 +690,17 @@ class _StockOutDetailDialogState extends ConsumerState<_StockOutDetailDialog> { IconButton( icon: const Icon(Icons.print_outlined, color: Colors.white), tooltip: '打印', - onPressed: () => printStockOutOrder(_loadedOrder!), + onPressed: () async { + try { + await printStockOutOrder(_loadedOrder!); + } catch (e) { + if (context.mounted) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), + ); + } + } + }, ), IconButton( icon: const Icon(Icons.close, color: Colors.white),