image_preview.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'package:flutter/material.dart';
  2. import 'package:image_preview/preview.dart';
  3. import 'package:image_preview/preview_data.dart';
  4. import 'package:shared/utils/date_time_utils.dart';
  5. import 'package:shared/utils/util.dart';
  6. import '../directory/directory_util.dart';
  7. class ImagePreviewEngine {
  8. /// 单图的预览
  9. static void singleImagePreview(
  10. BuildContext context,
  11. String url, {
  12. String? heroTag,
  13. void Function(String? url)? onLongPressAction,
  14. Widget? Function(int index)? tipWidget,
  15. }) {
  16. String path;
  17. // 根据URL类型处理路径
  18. if (isNetworkUrl(url)) {
  19. // 从URL中提取文件名
  20. String fileName = extractFileName(url);
  21. // 生成保存路径
  22. path = DirectoryUtil.getTempPath(category: 'Pictures', fileName: fileName) ?? "";
  23. } else {
  24. // 如果是本地路径,直接使用URL作为路径
  25. path = url;
  26. }
  27. // 调用预览页面
  28. openPreviewPage(
  29. Navigator.of(context),
  30. data: PreviewData(
  31. type: Type.image,
  32. heroTag: heroTag ?? '',
  33. image: ImageData(
  34. url: url,
  35. path: path,
  36. ),
  37. ),
  38. tipWidget: tipWidget,
  39. onLongPressHandler: (context, data) {
  40. onLongPressAction?.call(data.image?.url);
  41. },
  42. );
  43. }
  44. /// 多图的预览
  45. static void multipleImagePreview(
  46. BuildContext context,
  47. List<String?> urls, {
  48. List<String>? heroes,
  49. void Function(String? url)? onLongPressAction,
  50. void Function(int index)? onPageChanged,
  51. Widget? Function(int index)? tipWidget,
  52. }) {
  53. // 过滤掉 null 值的 URL
  54. List<String> filteredUrls = urls.where((url) => url != null).cast<String>().toList();
  55. List<PreviewData> previewDataList = filteredUrls.asMap().entries.map((entry) {
  56. int index = entry.key;
  57. String url = entry.value;
  58. String path;
  59. if (isNetworkUrl(url)) {
  60. String fileName = extractFileName(url);
  61. path = DirectoryUtil.getTempPath(category: 'Pictures', fileName: fileName) ?? "";
  62. } else {
  63. path = url;
  64. }
  65. return PreviewData(
  66. type: Type.image,
  67. heroTag: heroes != null && heroes.length > index ? heroes[index] : index.toString(),
  68. image: ImageData(
  69. url: url,
  70. path: path,
  71. ),
  72. );
  73. }).toList();
  74. // 调用预览页面
  75. openPreviewPages(
  76. Navigator.of(context),
  77. data: previewDataList,
  78. tipWidget: tipWidget,
  79. onPageChanged: onPageChanged,
  80. onLongPressHandler: (context, data) {
  81. onLongPressAction?.call(data.image?.url);
  82. },
  83. );
  84. }
  85. /// 工具函数:从URL提取文件名
  86. static String extractFileName(String url) {
  87. // 检查是否包含 '/'
  88. if (!url.contains('/')) {
  89. return DateTimeUtils.getNowDateMs().toString();
  90. }
  91. // 获取最后一个 '/' 后的部分
  92. String fileName = url.split('/').last;
  93. // 检查是否包含 '.'
  94. if (Utils.isEmpty(fileName)) {
  95. return DateTimeUtils.getNowDateMs().toString();
  96. }
  97. return fileName;
  98. }
  99. /// 工具函数:判断是否为网络URL
  100. static bool isNetworkUrl(String url) {
  101. return url.startsWith('http://') || url.startsWith('https://');
  102. }
  103. }