chore: release client-v1.0.82
Deploy Client / build-client-web (push) Successful in 47s
Deploy Client / build-windows (push) Successful in 1m56s
Deploy Client / build-macos (push) Successful in 2m15s
Deploy Client / build-android (push) Successful in 1m20s
Deploy Client / build-ios (push) Successful in 2m47s
Deploy Client / release-deploy-client (push) Successful in 1m51s
Deploy Client / build-client-web (push) Successful in 47s
Deploy Client / build-windows (push) Successful in 1m56s
Deploy Client / build-macos (push) Successful in 2m15s
Deploy Client / build-android (push) Successful in 1m20s
Deploy Client / build-ios (push) Successful in 2m47s
Deploy Client / release-deploy-client (push) Successful in 1m51s
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,258 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:printing/printing.dart';
|
||||
import '../core/errors/error_reporter.dart';
|
||||
import '../core/theme/app_theme.dart';
|
||||
import '../core/responsive/responsive.dart';
|
||||
import '../core/utils/print_util.dart';
|
||||
import '../models/stock_in.dart';
|
||||
import '../models/stock_out.dart';
|
||||
|
||||
/// 单据打印预览弹窗:把待打印的 A4 PDF 光栅化成图片(所见即所得)逐页展示,
|
||||
/// 页内点「打印」再调系统打印。解决部分 Windows 系统打印对话框无预览的问题。
|
||||
///
|
||||
/// 仅桌面端使用;Web 端走浏览器自带的打印预览(见 [showStockInOrderPrint])。
|
||||
class OrderPrintPreviewDialog extends StatefulWidget {
|
||||
/// 标题,如「入库单打印预览」。
|
||||
final String title;
|
||||
|
||||
/// 系统打印任务名(也作另存默认文件名前缀)。
|
||||
final String printName;
|
||||
|
||||
/// 惰性生成 PDF 字节。
|
||||
final Future<Uint8List> Function() pdfBuilder;
|
||||
|
||||
const OrderPrintPreviewDialog({
|
||||
super.key,
|
||||
required this.title,
|
||||
required this.printName,
|
||||
required this.pdfBuilder,
|
||||
});
|
||||
|
||||
@override
|
||||
State<OrderPrintPreviewDialog> createState() =>
|
||||
_OrderPrintPreviewDialogState();
|
||||
}
|
||||
|
||||
class _OrderPrintPreviewDialogState extends State<OrderPrintPreviewDialog> {
|
||||
Uint8List? _pdfBytes;
|
||||
final List<Uint8List> _pages = [];
|
||||
bool _rendering = true;
|
||||
bool _printing = false;
|
||||
String _error = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_render();
|
||||
}
|
||||
|
||||
Future<void> _render() async {
|
||||
try {
|
||||
final bytes = await widget.pdfBuilder();
|
||||
if (!mounted) return;
|
||||
setState(() => _pdfBytes = bytes);
|
||||
// 逐页光栅化(progressive),DPI 取 110 兼顾清晰与速度。
|
||||
await for (final raster in Printing.raster(bytes, dpi: 110)) {
|
||||
final png = await raster.toPng();
|
||||
if (!mounted) return;
|
||||
setState(() => _pages.add(png));
|
||||
}
|
||||
} catch (e, st) {
|
||||
reportError(e, st);
|
||||
if (mounted) setState(() => _error = '预览生成失败:$e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _rendering = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _doPrint() async {
|
||||
final bytes = _pdfBytes;
|
||||
if (bytes == null || _printing) return;
|
||||
setState(() => _printing = true);
|
||||
try {
|
||||
await Printing.layoutPdf(
|
||||
name: '${widget.printName}_${_ts()}',
|
||||
dynamicLayout: false,
|
||||
onLayout: (_) async => bytes,
|
||||
);
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (e, st) {
|
||||
reportError(e, st);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_printing = false;
|
||||
_error = '打印失败,请检查打印机连接和驱动是否正常。\n详情:$e';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _ts() {
|
||||
final n = DateTime.now();
|
||||
String p(int v) => v.toString().padLeft(2, '0');
|
||||
return '${n.year}${p(n.month)}${p(n.day)}_${p(n.hour)}${p(n.minute)}${p(n.second)}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Dialog(
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
child: Container(
|
||||
width: context.dialogWidth(720),
|
||||
constraints: const BoxConstraints(maxHeight: 720),
|
||||
child: Column(
|
||||
children: [
|
||||
// ── 顶栏 ──────────────────────────────────────────────────────────
|
||||
Container(
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 20, vertical: 14),
|
||||
decoration: const BoxDecoration(
|
||||
color: AppTheme.primary,
|
||||
borderRadius: BorderRadius.only(
|
||||
topLeft: Radius.circular(12),
|
||||
topRight: Radius.circular(12),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(widget.title,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Colors.white)),
|
||||
const Spacer(),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close, color: Colors.white),
|
||||
onPressed:
|
||||
_printing ? null : () => Navigator.of(context).pop(),
|
||||
padding: EdgeInsets.zero,
|
||||
constraints: const BoxConstraints(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// ── 预览区 ─────────────────────────────────────────────────────────
|
||||
Expanded(
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
color: const Color(0xFFE9ECF1),
|
||||
child: _buildBody(),
|
||||
),
|
||||
),
|
||||
|
||||
// ── 底栏 ──────────────────────────────────────────────────────────
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 16, 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_error.isNotEmpty
|
||||
? _error
|
||||
: (_rendering
|
||||
? '正在生成预览…'
|
||||
: '共 ${_pages.length} 页'),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: _error.isNotEmpty
|
||||
? Colors.red
|
||||
: AppTheme.textSecondary),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed:
|
||||
_printing ? null : () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.print, size: 18),
|
||||
onPressed: (_pdfBytes == null || _printing)
|
||||
? null
|
||||
: _doPrint,
|
||||
label: Text(_printing ? '打印中…' : '打印'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
if (_pages.isEmpty) {
|
||||
if (_error.isNotEmpty) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(_error,
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(color: Colors.red, fontSize: 13)),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
|
||||
itemCount: _pages.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 16),
|
||||
itemBuilder: (_, i) => Center(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.18),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Image.memory(_pages[i], fit: BoxFit.contain),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 打印入库单:桌面端先弹应用内预览,Web 端走浏览器自带打印预览。
|
||||
Future<void> showStockInOrderPrint(
|
||||
BuildContext context, StockInOrder order) async {
|
||||
if (kIsWeb) {
|
||||
await safePrint(context, () => printStockInOrder(order));
|
||||
return;
|
||||
}
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => OrderPrintPreviewDialog(
|
||||
title: '入库单打印预览',
|
||||
printName: '入库单',
|
||||
pdfBuilder: () => buildStockInOrderPdf(order),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 打印出库单:桌面端先弹应用内预览,Web 端走浏览器自带打印预览。
|
||||
Future<void> showStockOutOrderPrint(
|
||||
BuildContext context, StockOutOrder order) async {
|
||||
if (kIsWeb) {
|
||||
await safePrint(context, () => printStockOutOrder(order));
|
||||
return;
|
||||
}
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => OrderPrintPreviewDialog(
|
||||
title: '出库单打印预览',
|
||||
printName: '出库单',
|
||||
pdfBuilder: () => buildStockOutOrderPdf(order),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user