2025-08-19 10:16:20 +08:00
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
2025-01-21 14:09:09 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:flutter/widgets.dart';
|
2025-08-20 10:06:03 +08:00
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
2025-08-19 10:16:20 +08:00
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
import 'package:video_thumbnail/video_thumbnail.dart';
|
2025-01-21 14:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
class VideoThumbnailImage extends StatefulWidget {
|
|
|
|
|
|
final String videoUrl;
|
|
|
|
|
|
|
2025-08-19 10:16:20 +08:00
|
|
|
|
const VideoThumbnailImage({Key? key, required this.videoUrl})
|
|
|
|
|
|
: super(key: key);
|
2025-01-21 14:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
_VideoThumbnailState createState() => _VideoThumbnailState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _VideoThumbnailState extends State<VideoThumbnailImage> {
|
2025-08-19 10:16:20 +08:00
|
|
|
|
// ✅ 使用 static 缓存:所有实例共享,避免重复请求
|
|
|
|
|
|
static final Map<String, Future<String?>> _pendingThumbnails = {};
|
|
|
|
|
|
|
|
|
|
|
|
late Future<String?> _thumbnailFuture;
|
2025-01-21 14:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
2025-08-19 10:16:20 +08:00
|
|
|
|
// ✅ 如果已存在该 URL 的 Future,复用;否则创建并缓存
|
|
|
|
|
|
_thumbnailFuture = _pendingThumbnails.putIfAbsent(widget.videoUrl, () {
|
|
|
|
|
|
return _generateThumbnail(widget.videoUrl);
|
|
|
|
|
|
});
|
2025-01-21 14:09:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-19 10:16:20 +08:00
|
|
|
|
// 生成缩略图(只执行一次 per URL)
|
|
|
|
|
|
Future<String?> _generateThumbnail(String url) async {
|
2025-01-21 14:09:09 +08:00
|
|
|
|
try {
|
|
|
|
|
|
final tempDir = await getTemporaryDirectory();
|
2025-08-19 10:16:20 +08:00
|
|
|
|
final thumbnail = await VideoThumbnail.thumbnailFile(
|
|
|
|
|
|
video: url,
|
2025-01-21 14:09:09 +08:00
|
|
|
|
thumbnailPath: tempDir.path,
|
|
|
|
|
|
imageFormat: ImageFormat.JPEG,
|
|
|
|
|
|
maxHeight: 200,
|
2025-08-19 10:16:20 +08:00
|
|
|
|
quality: 100,
|
2025-01-21 14:09:09 +08:00
|
|
|
|
);
|
2025-08-19 10:16:20 +08:00
|
|
|
|
return thumbnail;
|
2025-01-21 14:09:09 +08:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
print('Failed to generate thumbnail: $e');
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
return FutureBuilder<String?>(
|
2025-08-19 10:16:20 +08:00
|
|
|
|
future: _thumbnailFuture,
|
2025-01-21 14:09:09 +08:00
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
|
|
return Center(child: CircularProgressIndicator());
|
|
|
|
|
|
} else if (snapshot.hasError || !snapshot.hasData) {
|
2025-08-19 10:16:20 +08:00
|
|
|
|
return Center(
|
|
|
|
|
|
child: Image.asset(
|
|
|
|
|
|
'images/icon_unHaveData.png',
|
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
|
),
|
2025-01-21 14:09:09 +08:00
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return Stack(
|
|
|
|
|
|
alignment: Alignment.center,
|
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
|
RotatedBox(
|
|
|
|
|
|
quarterTurns: -1,
|
|
|
|
|
|
child: Image.file(
|
2025-08-19 10:16:20 +08:00
|
|
|
|
File(snapshot.data!),
|
2025-01-21 14:09:09 +08:00
|
|
|
|
width: 200,
|
|
|
|
|
|
height: 200,
|
|
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
Icon(
|
|
|
|
|
|
Icons.play_arrow_rounded,
|
2025-08-20 10:06:03 +08:00
|
|
|
|
size: 88.sp,
|
2025-01-21 14:09:09 +08:00
|
|
|
|
color: Colors.white.withOpacity(0.8),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|