2025-07-29 08:50:41 +08:00
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
2025-09-11 15:27:40 +08:00
|
|
|
|
import 'package:flutter/foundation.dart';
|
2025-07-11 11:03:21 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:photo_view/photo_view.dart';
|
|
|
|
|
|
import 'package:qhd_prevention/pages/my_appbar.dart';
|
2025-09-11 15:27:40 +08:00
|
|
|
|
|
2025-07-11 11:03:21 +08:00
|
|
|
|
// 查看大图
|
|
|
|
|
|
class SingleImageViewer extends StatelessWidget {
|
|
|
|
|
|
final String imageUrl;
|
|
|
|
|
|
const SingleImageViewer({Key? key, required this.imageUrl}) : super(key: key);
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-07-29 08:50:41 +08:00
|
|
|
|
ImageProvider provider;
|
2025-09-11 15:27:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 选择图片来源:网络 / asset / 本地文件(优先检测 http,然后 asset,再文件)
|
2025-07-29 08:50:41 +08:00
|
|
|
|
if (imageUrl.toLowerCase().startsWith('http')) {
|
|
|
|
|
|
provider = NetworkImage(imageUrl);
|
2025-09-11 15:27:40 +08:00
|
|
|
|
} else if (imageUrl.startsWith('assets/') ||
|
|
|
|
|
|
imageUrl.startsWith('package:') ||
|
|
|
|
|
|
imageUrl.startsWith('packages/')) {
|
|
|
|
|
|
// 明确标记为工程资源(asset)
|
|
|
|
|
|
provider = AssetImage(imageUrl) as ImageProvider;
|
2025-07-29 08:50:41 +08:00
|
|
|
|
} else {
|
2025-09-11 15:27:40 +08:00
|
|
|
|
// 不是明确的网络或 asset 路径 —— 在非 web 平台尝试作为文件路径
|
|
|
|
|
|
if (!kIsWeb) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
final file = File(imageUrl);
|
|
|
|
|
|
if (file.existsSync()) {
|
|
|
|
|
|
provider = FileImage(file);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 文件不存在时尝试作为 asset 路径回退
|
|
|
|
|
|
provider = AssetImage(imageUrl) as ImageProvider;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
// 任何异常都回退为 asset 尝试(避免抛出)
|
|
|
|
|
|
provider = AssetImage(imageUrl) as ImageProvider;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// web 平台没有 File API,可直接尝试当作 asset
|
|
|
|
|
|
provider = AssetImage(imageUrl) as ImageProvider;
|
|
|
|
|
|
}
|
2025-07-29 08:50:41 +08:00
|
|
|
|
}
|
2025-09-11 15:27:40 +08:00
|
|
|
|
|
2025-07-11 11:03:21 +08:00
|
|
|
|
return Scaffold(
|
2025-07-28 16:50:40 +08:00
|
|
|
|
backgroundColor: Colors.black.withValues(alpha: 0.5),
|
2025-07-11 11:03:21 +08:00
|
|
|
|
appBar: MyAppbar(
|
2025-07-28 14:22:07 +08:00
|
|
|
|
isBack: false,
|
|
|
|
|
|
actions: [
|
2025-09-11 15:27:40 +08:00
|
|
|
|
IconButton(
|
|
|
|
|
|
onPressed: () {
|
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
},
|
|
|
|
|
|
icon: const Icon(
|
|
|
|
|
|
Icons.close,
|
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
|
size: 40,
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
2025-07-28 14:22:07 +08:00
|
|
|
|
],
|
2025-09-11 15:27:40 +08:00
|
|
|
|
backgroundColor: Colors.black.withValues(alpha: 0.5),
|
|
|
|
|
|
title: '',
|
2025-07-11 11:03:21 +08:00
|
|
|
|
),
|
|
|
|
|
|
body: Center(
|
|
|
|
|
|
child: PhotoView(
|
2025-07-29 08:50:41 +08:00
|
|
|
|
imageProvider: provider,
|
2025-09-11 15:27:40 +08:00
|
|
|
|
backgroundDecoration: BoxDecoration(color: Colors.black.withValues(alpha: 0.5)),
|
2025-07-11 11:03:21 +08:00
|
|
|
|
minScale: PhotoViewComputedScale.contained,
|
|
|
|
|
|
maxScale: PhotoViewComputedScale.covered * 2,
|
|
|
|
|
|
onTapUp: (context, details, controllerValue) {
|
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|