Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-10-08 15:52:32 +08:00
parent 322885f284
commit 06d8296939
4 changed files with 28 additions and 101 deletions

View File

@@ -124,9 +124,7 @@ class _CachedNetworkSVGImageState extends State<CachedNetworkSVGImage> {
Future<void> _loadImage() async {
try {
var file = (await widget._cacheManager.getFileFromMemory(
_cacheKey,
))?.file;
var file = (await widget._cacheManager.getFileFromCache(_cacheKey))?.file;
file ??= await widget._cacheManager.getSingleFile(
widget._url,
@@ -173,15 +171,14 @@ class _CachedNetworkSVGImageState extends State<CachedNetworkSVGImage> {
if (mounted) {
setState(() {});
} else {
SchedulerBinding.instance.addPostFrameCallback((_) => setState(() {}));
SchedulerBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {});
}
});
}
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(

View File

@@ -78,8 +78,8 @@ class InteractiveviewerGallery extends StatefulWidget {
class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
with SingleTickerProviderStateMixin {
PageController? _pageController;
TransformationController? _transformationController;
late final PageController _pageController;
late final TransformationController _transformationController;
/// The controller to animate the transformation value of the
/// [InteractiveViewer] when it should reset.
@@ -109,24 +109,25 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
duration: const Duration(milliseconds: 300),
)..addListener(listener);
var item = widget.sources[currentIndex.value];
final item = widget.sources[currentIndex.value];
if (item.sourceType == SourceType.livePhoto) {
_onPlay(item.liveUrl!);
}
}
void listener() {
_transformationController!.value = _animation?.value ?? Matrix4.identity();
_transformationController.value = _animation?.value ?? Matrix4.identity();
}
@override
void dispose() {
widget.onClose?.call(true);
_player?.dispose();
_pageController?.dispose();
_pageController.dispose();
_animationController
..removeListener(listener)
..dispose();
_transformationController.dispose();
for (var item in widget.sources) {
if (item.sourceType == SourceType.networkImage) {
CachedNetworkImageProvider(_getActualUrl(item.url)).evict();
@@ -160,7 +161,7 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
/// When the left boundary has been hit after scaling up the source, the page
/// view swiping gets enabled if it has a page to swipe to.
void _onLeftBoundaryHit() {
if (!_enablePageView && _pageController!.page!.floor() > 0) {
if (!_enablePageView && _pageController.page!.floor() > 0) {
setState(() {
_enablePageView = true;
});
@@ -171,7 +172,7 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
/// view swiping gets enabled if it has a page to swipe to.
void _onRightBoundaryHit() {
if (!_enablePageView &&
_pageController!.page!.floor() < widget.sources.length - 1) {
_pageController.page!.floor() < widget.sources.length - 1) {
setState(() {
_enablePageView = true;
});
@@ -206,12 +207,12 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
_onPlay(item.liveUrl!);
}
widget.onPageChanged?.call(page);
if (_transformationController!.value != Matrix4.identity()) {
if (_transformationController.value != Matrix4.identity()) {
// animate the reset for the transformation of the interactive viewer
_animation =
Matrix4Tween(
begin: _transformationController!.value,
begin: _transformationController.value,
end: Matrix4.identity(),
).animate(
CurveTween(curve: Curves.easeOut).animate(_animationController),
@@ -232,7 +233,7 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
widget.onClose!(false);
} else {
Get.back();
widget.onDismissed?.call(_pageController!.page!.floor());
widget.onDismissed?.call(_pageController.page!.floor());
}
}
@@ -322,83 +323,12 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
),
)
: null,
child: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Align(
alignment: Alignment.centerLeft,
child: IconButton(
icon: const Icon(Icons.close, color: Colors.white),
onPressed: onClose,
),
),
if (widget.sources.length > 1)
Align(
alignment: Alignment.center,
child: Obx(
() => Text(
"${currentIndex.value + 1}/${widget.sources.length}",
style: const TextStyle(color: Colors.white),
),
),
),
if (widget.sources[currentIndex.value].sourceType !=
SourceType.fileImage)
Align(
alignment: Alignment.centerRight,
child: PopupMenuButton(
itemBuilder: (context) {
final item = widget.sources[currentIndex.value];
return [
if (Utils.isMobile)
PopupMenuItem(
onTap: () => ImageUtils.onShareImg(item.url),
child: const Text("分享图片"),
),
PopupMenuItem(
onTap: () => Utils.copyText(item.url),
child: const Text("复制链接"),
),
PopupMenuItem(
onTap: () => ImageUtils.downloadImg(
this.context,
[item.url],
),
child: const Text("保存图片"),
),
if (Utils.isDesktop)
PopupMenuItem(
onTap: () => PageUtils.launchURL(item.url),
child: const Text("网页打开"),
)
else if (widget.sources.length > 1)
PopupMenuItem(
onTap: () => ImageUtils.downloadImg(
this.context,
widget.sources.map((item) => item.url).toList(),
),
child: const Text("保存全部"),
),
if (item.sourceType == SourceType.livePhoto)
PopupMenuItem(
onTap: () {
ImageUtils.downloadLivePhoto(
context: this.context,
url: item.url,
liveUrl: item.liveUrl!,
width: item.width!,
height: item.height!,
);
},
child: const Text("保存 Live Photo"),
),
];
},
child: const Icon(Icons.more_horiz, color: Colors.white),
),
),
],
alignment: Alignment.center,
child: Obx(
() => Text(
"${currentIndex.value + 1}/${widget.sources.length}",
style: const TextStyle(color: Colors.white),
),
),
),
),
@@ -444,7 +374,7 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
}
void onDoubleTap() {
Matrix4 matrix = _transformationController!.value.clone();
Matrix4 matrix = _transformationController.value.clone();
double currentScale = matrix.row0.x;
double targetScale = widget.minScale;
@@ -481,7 +411,7 @@ class _InteractiveviewerGalleryState extends State<InteractiveviewerGallery>
_animation =
Matrix4Tween(
begin: _transformationController!.value,
begin: _transformationController.value,
end: matrix,
).animate(
CurveTween(curve: Curves.easeOut).animate(_animationController),