Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: image preview (#1998)

LiggMax95972ad2b6bc
diff --git a/lib/bbcode/bbcode_widget.dart b/lib/bbcode/bbcode_widget.dart
index 50818eb4..dd228d08 100644
--- a/lib/bbcode/bbcode_widget.dart
+++ b/lib/bbcode/bbcode_widget.dart
@@ -3,6 +3,7 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:antlr4/antlr4.dart';
import 'package:cached_network_image/cached_network_image.dart';
+import 'package:kazumi/bean/widget/image_preview.dart';
import 'package:url_launcher/url_launcher.dart';
import 'bbcode_base_listener.dart';
@@ -120,13 +121,20 @@ class _BBCodeWidgetState extends State<BBCodeWidget> {
);
} else if (e is BBCodeImg) {
return WidgetSpan(
- child: CachedNetworkImage(
- imageUrl: e.imageUrl,
- placeholder: (context, url) =>
- const SizedBox(width: 1, height: 1),
- errorWidget: (context, error, stackTrace) {
- return const Text('.');
- },
+ child: GestureDetector(
+ onTap: () => ImageViewer.show(context,
+ imageUrl: e.imageUrl, heroTag: e.imageUrl),
+ child: Hero(
+ tag: e.imageUrl,
+ child: CachedNetworkImage(
+ imageUrl: e.imageUrl,
+ placeholder: (context, url) =>
+ const SizedBox(width: 1, height: 1),
+ errorWidget: (context, error, stackTrace) {
+ return const Text('.');
+ },
+ ),
+ ),
),
);
} else if (e is BBCodeBgm) {
@@ -154,7 +162,8 @@ class _BBCodeWidgetState extends State<BBCodeWidget> {
} else if (e is BBCodeMusume) {
return WidgetSpan(
child: CachedNetworkImage(
- imageUrl: 'https://lain.bgm.tv/img/smiles/musume/musume_${e.id}.gif',
+ imageUrl:
+ 'https://lain.bgm.tv/img/smiles/musume/musume_${e.id}.gif',
placeholder: (context, url) =>
const SizedBox(width: 1, height: 1),
errorWidget: (context, error, stackTrace) {
diff --git a/lib/bean/widget/image_preview.dart b/lib/bean/widget/image_preview.dart
new file mode 100644
index 00000000..baef73cf
--- /dev/null
+++ b/lib/bean/widget/image_preview.dart
@@ -0,0 +1,155 @@
+import 'package:cached_network_image/cached_network_image.dart';
+import 'package:flutter/material.dart';
+import 'package:flutter/gestures.dart';
+import 'package:photo_view/photo_view.dart';
+
+class ImageViewer extends StatefulWidget {
+ final String imageUrl;
+ final String? heroTag;
+
+ const ImageViewer({super.key, required this.imageUrl, this.heroTag});
+
+ /// 显示图片预览
+ static void show(BuildContext context,
+ {required String imageUrl, String? heroTag}) {
+ Navigator.of(context).push(
+ PageRouteBuilder(
+ opaque: false,
+ barrierColor: Colors.black.withValues(alpha: 0.8),
+ pageBuilder: (context, animation, secondaryAnimation) {
+ return ImageViewer(imageUrl: imageUrl, heroTag: heroTag);
+ },
+ transitionDuration: const Duration(milliseconds: 300),
+ transitionsBuilder: (context, animation, secondaryAnimation, child) {
+ return FadeTransition(
+ opacity: animation,
+ child: ScaleTransition(
+ scale: Tween<double>(begin: 0.8, end: 1.0).animate(
+ CurvedAnimation(parent: animation, curve: Curves.easeOutCubic),
+ ),
+ child: child,
+ ),
+ );
+ },
+ ),
+ );
+ }
+
+ @override
+ State<ImageViewer> createState() => _ImageViewerState();
+}
+
+class _ImageViewerState extends State<ImageViewer> {
+ late final PhotoViewController _photoViewController;
+ late final PhotoViewScaleStateController _scaleStateController;
+
+ /// 初始缩放值
+ double? _initialScale;
+
+ /// 滚轮单次缩放步长
+ static const double _wheelScaleStep = 1.1;
+
+ /// 最小缩放倍数
+ static const double _minScaleFactor = 1.0;
+
+ /// 最大缩放倍数
+ static const double _maxScaleFactor = 6.0;
+
+ @override
+ void initState() {
+ super.initState();
+ _photoViewController = PhotoViewController();
+ _scaleStateController = PhotoViewScaleStateController();
+ }
+
+ @override
+ void dispose() {
+ _photoViewController.dispose();
+ _scaleStateController.dispose();
+ super.dispose();
+ }
+
+ void _handlePointerSignal(PointerSignalEvent event) {
+ if (event is! PointerScrollEvent) return;
+ final currentScale = _photoViewController.scale ?? _initialScale ?? 1.0;
+ _initialScale ??= currentScale;
+
+ final factor = event.scrollDelta.dy < 0 ? _wheelScaleStep : 1 / _wheelScaleStep;
+ final minScale = _initialScale! * _minScaleFactor;
+ final maxScale = _initialScale! * _maxScaleFactor;
+ final newScale = (currentScale * factor).clamp(minScale, maxScale);
+
+ if (newScale == currentScale) return;
+
+ _photoViewController.scale = newScale;
+ _scaleStateController.scaleState = PhotoViewScaleState.zoomedIn;
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ return Scaffold(
+ backgroundColor: Colors.transparent,
+ body: Stack(
+ children: [
+ Listener(
+ onPointerSignal: _handlePointerSignal,
+ child: GestureDetector(
+ onTap: () => Navigator.of(context).pop(),
+ child: PhotoView(
+ imageProvider: CachedNetworkImageProvider(widget.imageUrl),
+ controller: _photoViewController,
+ scaleStateController: _scaleStateController,
+ minScale: PhotoViewComputedScale.contained,
+ maxScale: PhotoViewComputedScale.covered * 3,
+ backgroundDecoration: const BoxDecoration(
+ color: Colors.transparent,
+ ),
+ heroAttributes: widget.heroTag != null
+ ? PhotoViewHeroAttributes(tag: widget.heroTag!)
+ : null,
+ loadingBuilder: (context, event) => const Center(
+ child: CircularProgressIndicator(),
+ ),
+ errorBuilder: (context, error, stackTrace) => Center(
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Icon(
+ Icons.broken_image,
+ size: 48,
+ color: Theme.of(context).colorScheme.onErrorContainer,
+ ),
+ const SizedBox(height: 8),
+ Text(
+ '图片加载失败',
+ style: TextStyle(
+ color:
+ Theme.of(context).colorScheme.onErrorContainer,
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ ),
+ ),
+ // 关闭按钮
+ Positioned(
+ top: MediaQuery.of(context).padding.top + 16,
+ right: 16,
+ child: Container(
+ decoration: BoxDecoration(
+ color: Colors.black.withValues(alpha: 0.5),
+ shape: BoxShape.circle,
+ ),
+ child: IconButton(
+ onPressed: () => Navigator.of(context).pop(),
+ icon: const Icon(Icons.close, color: Colors.white),
+ ),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
diff --git a/pubspec.yaml b/pubspec.yaml
index ae7cb198..ef2f199c 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -98,6 +98,7 @@ dependencies:
html: any
material_color_utilities: any
path: any
+ photo_view: ^0.15.0
webview_windows:
git:
url: https://github.com/Predidit/flutter-webview-windows.git