3d1fc0d4ba
新增 safePrint 封装,所有打印调用点统一 catch 任意异常(含字体加载失败), 自动上报 error_reporter 并展示 SnackBar,不再静默失败。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
56 lines
1.5 KiB
Dart
56 lines
1.5 KiB
Dart
import 'dart:typed_data';
|
|
import 'package:flutter/material.dart';
|
|
import '../../models/stock_in.dart';
|
|
import '../../models/stock_out.dart';
|
|
import '../errors/error_reporter.dart';
|
|
import 'print_util_stub.dart'
|
|
if (dart.library.js_interop) 'print_util_web.dart';
|
|
|
|
Future<void> printProductLabel({
|
|
required Uint8List qrBytes,
|
|
required String name,
|
|
required String code,
|
|
String? spec,
|
|
String? series,
|
|
String? batchNo,
|
|
String? productionDate,
|
|
String? remark,
|
|
String shopName = '',
|
|
String shopAddress = '',
|
|
String shopPhone = '',
|
|
}) =>
|
|
printProductLabelImpl(
|
|
qrBytes: qrBytes,
|
|
name: name,
|
|
code: code,
|
|
spec: spec,
|
|
series: series,
|
|
batchNo: batchNo,
|
|
productionDate: productionDate,
|
|
remark: remark,
|
|
shopName: shopName,
|
|
shopAddress: shopAddress,
|
|
shopPhone: shopPhone,
|
|
);
|
|
|
|
Future<void> printStockInOrder(StockInOrder order) =>
|
|
printStockInOrderImpl(order);
|
|
|
|
Future<void> printStockOutOrder(StockOutOrder order) =>
|
|
printStockOutOrderImpl(order);
|
|
|
|
/// 统一打印入口:catch 任意异常,上报并弹 SnackBar 给用户。
|
|
/// 用法:await safePrint(context, () => printStockInOrder(order));
|
|
Future<void> safePrint(BuildContext context, Future<void> Function() fn) async {
|
|
try {
|
|
await fn();
|
|
} catch (e, st) {
|
|
reportError(e, st);
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('打印失败:$e'), backgroundColor: Colors.red),
|
|
);
|
|
}
|
|
}
|
|
}
|