| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // collection/collection_screen.dart
- import 'package:flutter/material.dart';
- import 'package:image_puzzle/config/device.dart';
- import 'package:image_puzzle/models/download.dart';
- import 'package:image_puzzle/models/items.dart';
- import 'dart:ui' as ui;
- import 'package:provider/provider.dart';
- class ImageDetailDialog extends StatefulWidget {
- final ListItem item;
- const ImageDetailDialog({super.key, required this.item});
- @override
- State<StatefulWidget> createState() => _ImageDetailDialog();
- }
- class _ImageDetailDialog extends State<ImageDetailDialog> {
- bool _isLoading = true;
- ui.Image? image;
- String? _error; // 新增错误状态
- @override
- void initState() {
- super.initState();
- loadImage();
- }
- void loadImage() async {
- try {
- Device device = context.read<Device>();
- double dpr = device.devicePixelRatio;
- // 计算最佳尺寸(屏幕宽度90%,按2:3比例计算高度)
- final bestWidth = (device.screenSize.width * 0.9 * dpr).round();
- final bestHeight = (bestWidth * 3 / 2).round();
- ItemLoader itemLoader = ItemLoader.load(widget.item);
- final loadedImage = await itemLoader.getImageBySize(bestWidth, bestHeight);
- if (mounted) {
- setState(() {
- image = loadedImage;
- _isLoading = false;
- _error = null;
- });
- }
- } catch (e) {
- if (mounted) {
- setState(() {
- _isLoading = false;
- _error = "图片加载失败";
- });
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- final device = context.read<Device>();
- final screenWidth = device.screenSize.width;
- return Scaffold(
- // 背景设为黑色半透明,突出图片
- backgroundColor: Colors.black.withOpacity(0.8),
- body: Stack(
- children: [
- // 主内容区:加载中/错误/图片
- Center(child: _buildContent(screenWidth)),
- // 左上角关闭按钮
- Positioned(
- top: device.appBarHeight + 16, // 适配状态栏高度
- left: 16,
- child: _buildCloseButton(),
- ),
- ],
- ),
- );
- }
- // 构建主体内容(加载中/错误/图片)
- Widget _buildContent(double screenWidth) {
- if (_isLoading) {
- // 加载状态:居中显示进度条
- return const CircularProgressIndicator(color: Colors.white, strokeWidth: 3);
- }
- if (_error != null) {
- // 错误状态:显示错误信息和重试按钮
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Icon(Icons.error_outline, color: Colors.red, size: 48),
- const SizedBox(height: 16),
- Text(_error!, style: const TextStyle(color: Colors.white, fontSize: 18)),
- TextButton(
- onPressed: loadImage,
- child: const Text("重试", style: TextStyle(color: Colors.blue, fontSize: 16)),
- ),
- ],
- );
- }
- return ConstrainedBox(
- constraints: BoxConstraints(
- maxWidth: screenWidth * 0.9, // 最大宽度为屏幕90%
- ),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(16), // 圆角大小(可自定义,如12、16)
- // 可选:添加边框+背景,让圆角更有层次感
- child: DecoratedBox(
- decoration: BoxDecoration(
- color: Colors.grey[900], // 图片加载前的背景色(避免白边)
- // 可选:添加边框
- border: Border.all(
- color: Colors.white.withOpacity(0.3), // 边框颜色(半透明白色)
- width: 2, // 边框宽度
- ),
- ),
- child: RawImage(image: image!, fit: BoxFit.contain, alignment: Alignment.center),
- ),
- ),
- );
- }
- // 构建关闭按钮
- Widget _buildCloseButton() {
- return GestureDetector(
- onTap: () => Navigator.pop(context),
- child: Container(
- width: 40,
- height: 40,
- decoration: BoxDecoration(
- color: Colors.black54, // 半透黑背景
- shape: BoxShape.circle,
- ),
- child: const Icon(Icons.close, color: Colors.white, size: 24),
- ),
- );
- }
- }
|