| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import 'dart:typed_data';
- import 'package:flutter/material.dart';
- import 'package:tdesign_flutter/tdesign_flutter.dart';
- import '../../core/i18n/app_localizations.dart';
- import '../../core/navigation/host_app_channel.dart';
- import '../models/bill_attachment.dart';
- import '../widgets/loading_dialog.dart';
- /// 附件下载辅助类,供详情页复用下载逻辑。
- class AttachmentDownloadHelper {
- /// 根据扩展名返回 MIME 类型
- static String mimeType(String ext) {
- switch (ext.toLowerCase()) {
- case 'pdf':
- return 'application/pdf';
- case 'doc':
- return 'application/msword';
- case 'docx':
- return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
- case 'xls':
- return 'application/vnd.ms-excel';
- case 'xlsx':
- return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
- case 'jpg':
- case 'jpeg':
- return 'image/jpeg';
- case 'png':
- return 'image/png';
- case 'gif':
- return 'image/gif';
- case 'bmp':
- return 'image/bmp';
- case 'webp':
- return 'image/webp';
- default:
- return 'application/octet-stream';
- }
- }
- /// 下载附件并保存到公共目录,成功显示路径弹窗。
- static Future<void> downloadAndSave(
- BuildContext context,
- BillAttachment attachment,
- Future<Uint8List?> Function(String id) downloader,
- ) async {
- final l10n = AppLocalizations.of(context);
- try {
- LoadingDialog.show(context, text: l10n.get('downloading'));
- final bytes = await downloader(attachment.id);
- if (!context.mounted) return;
- LoadingDialog.hide(context);
- if (bytes == null) {
- TDToast.showText(l10n.get('downloadFailed'), context: context);
- return;
- }
- final path = await HostAppChannel.saveFileToDownloads(
- bytes,
- attachment.fileName,
- mimeType(attachment.ext),
- );
- if (!context.mounted) return;
- if (path != null) {
- _showSuccessDialog(context, l10n, path);
- } else {
- TDToast.showText(l10n.get('downloadFailed'), context: context);
- }
- } catch (_) {
- if (context.mounted) LoadingDialog.hide(context);
- if (context.mounted) {
- TDToast.showText(l10n.get('downloadFailed'), context: context);
- }
- }
- }
- static void _showSuccessDialog(
- BuildContext context,
- AppLocalizations l10n,
- String path,
- ) {
- showGeneralDialog(
- context: context,
- pageBuilder: (buildContext, animation, secondaryAnimation) {
- return TDConfirmDialog(
- title: l10n.get('downloadSuccess'),
- content: path,
- buttonStyle: TDDialogButtonStyle.text,
- buttonText: l10n.get('confirm'),
- );
- },
- );
- }
- }
|