Skip to content

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

feat(image): muliti images preview (#2232)

LiggMax204443fa11ac
diff --git a/lib/bbcode/bbcode_widget.dart b/lib/bbcode/bbcode_widget.dart
index 43d983fa..5140c915 100644
--- a/lib/bbcode/bbcode_widget.dart
+++ b/lib/bbcode/bbcode_widget.dart
@@ -70,6 +70,12 @@ class _BBCodeWidgetState extends State<BBCodeWidget> {
ParseTreeWalker.DEFAULT.walk(bbcodeBaseListener, tree);
bbCodeTag.clear();
+ final imageUrls = bbcodeBaseListener.bbcode
+ .whereType<BBCodeImg>()
+ .map((e) => e.imageUrl)
+ .toList();
+ var imageIndex = 0;
+
return Wrap(
children: [
RichText(
@@ -120,12 +126,18 @@ class _BBCodeWidgetState extends State<BBCodeWidget> {
),
);
} else if (e is BBCodeImg) {
+ final currentIndex = imageIndex++;
+ final heroTag = ImageViewer.heroTagFor(e.imageUrl, currentIndex);
return WidgetSpan(
child: GestureDetector(
- onTap: () => ImageViewer.show(context,
- imageUrl: e.imageUrl, heroTag: e.imageUrl),
+ onTap: () => ImageViewer.show(
+ context,
+ imageUrls: imageUrls,
+ initialIndex: currentIndex,
+ heroTag: heroTag,
+ ),
child: Hero(
- tag: e.imageUrl,
+ tag: heroTag,
child: CachedNetworkImage(
imageUrl: e.imageUrl,
placeholder: (context, url) =>
diff --git a/lib/bean/widget/image_preview.dart b/lib/bean/widget/image_preview.dart
index dcdeef6c..d3d40a84 100644
--- a/lib/bean/widget/image_preview.dart
+++ b/lib/bean/widget/image_preview.dart
@@ -2,31 +2,57 @@ import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter_modular/flutter_modular.dart';
+import 'package:kazumi/utils/device.dart';
import 'package:photo_view/photo_view.dart';
+import 'package:photo_view/photo_view_gallery.dart';
class ImageViewerRouteArgs {
- const ImageViewerRouteArgs({required this.imageUrl, this.heroTag});
+ ImageViewerRouteArgs({
+ required List<String> imageUrls,
+ this.initialIndex = 0,
+ this.heroTag,
+ }) : imageUrls = List.unmodifiable(imageUrls);
- final String imageUrl;
+ final List<String> imageUrls;
+ final int initialIndex;
final Object? heroTag;
}
class ImageViewer extends StatefulWidget {
static const String routePath = '/image-preview';
- final String imageUrl;
+ final List<String> imageUrls;
+ final int initialIndex;
final Object? heroTag;
- const ImageViewer({super.key, required this.imageUrl, this.heroTag});
+ ImageViewer({
+ super.key,
+ required List<String> imageUrls,
+ this.initialIndex = 0,
+ this.heroTag,
+ }) : imageUrls = List.unmodifiable(imageUrls);
+
+ static Object heroTagFor(String imageUrl, int index) => '$imageUrl-$index';
/// 显示图片预览
- static Future<void> show(BuildContext context,
- {required String imageUrl, Object? heroTag}) async {
- final effectiveHeroTag = heroTag ?? imageUrl;
+ static Future<void> show(
+ BuildContext context, {
+ required List<String> imageUrls,
+ int initialIndex = 0,
+ Object? heroTag,
+ }) async {
+ if (imageUrls.isEmpty) return;
+
+ final index = initialIndex.clamp(0, imageUrls.length - 1);
+ final effectiveHeroTag = heroTag ??
+ (imageUrls.length > 1
+ ? ImageViewer.heroTagFor(imageUrls[index], index)
+ : imageUrls[index]);
await Modular.to.pushNamed(
routePath,
arguments: ImageViewerRouteArgs(
- imageUrl: imageUrl,
+ imageUrls: imageUrls,
+ initialIndex: index,
heroTag: effectiveHeroTag,
),
);
@@ -39,9 +65,11 @@ class ImageViewer extends StatefulWidget {
class _ImageViewerState extends State<ImageViewer> {
late final PhotoViewController _photoViewController;
late final PhotoViewScaleStateController _scaleStateController;
-
- /// 初始缩放值
- double? _initialScale;
+ late final PageController _pageController;
+ late int _currentIndex;
+ final Map<int, PhotoViewController> _galleryControllers = {};
+ final Map<int, PhotoViewScaleStateController> _galleryScaleStateControllers = {};
+ final Map<int, double?> _initialScales = {};
/// 滚轮单次缩放步长
static const double _wheelScaleStep = 1.1;
@@ -52,87 +80,125 @@ class _ImageViewerState extends State<ImageViewer> {
/// 最大缩放倍数
static const double _maxScaleFactor = 6.0;
+ bool get _isMultiImage => widget.imageUrls.length > 1;
+
@override
void initState() {
super.initState();
+ _currentIndex = widget.initialIndex;
_photoViewController = PhotoViewController();
_scaleStateController = PhotoViewScaleStateController();
+ _pageController = PageController(initialPage: widget.initialIndex);
}
@override
void dispose() {
_photoViewController.dispose();
_scaleStateController.dispose();
+ for (final controller in _galleryControllers.values) {
+ controller.dispose();
+ }
+ for (final controller in _galleryScaleStateControllers.values) {
+ controller.dispose();
+ }
+ _pageController.dispose();
super.dispose();
}
+ PhotoViewController? get _activePhotoViewController {
+ if (_isMultiImage) {
+ return _galleryControllers[_currentIndex];
+ }
+ return _photoViewController;
+ }
+
+ PhotoViewScaleStateController? get _activeScaleStateController {
+ if (_isMultiImage) {
+ return _galleryScaleStateControllers[_currentIndex];
+ }
+ return _scaleStateController;
+ }
+
+ int get _activeScaleKey => _isMultiImage ? _currentIndex : -1;
+
void _handlePointerSignal(PointerSignalEvent event) {
if (event is! PointerScrollEvent) return;
- final currentScale = _photoViewController.scale ?? _initialScale ?? 1.0;
- _initialScale ??= currentScale;
+
+ final photoViewController = _activePhotoViewController;
+ final scaleStateController = _activeScaleStateController;
+ if (photoViewController == null || scaleStateController == null) return;
+
+ final scaleKey = _activeScaleKey;
+ final currentScale =
+ photoViewController.scale ?? _initialScales[scaleKey] ?? 1.0;
+ _initialScales[scaleKey] ??= currentScale;
final factor =
event.scrollDelta.dy < 0 ? _wheelScaleStep : 1 / _wheelScaleStep;
- final minScale = _initialScale! * _minScaleFactor;
- final maxScale = _initialScale! * _maxScaleFactor;
+ final minScale = _initialScales[scaleKey]! * _minScaleFactor;
+ final maxScale = _initialScales[scaleKey]! * _maxScaleFactor;
final newScale = (currentScale * factor).clamp(minScale, maxScale);
if (newScale == currentScale) return;
- _photoViewController.scale = newScale;
- _scaleStateController.scaleState = PhotoViewScaleState.zoomedIn;
+ photoViewController.scale = newScale;
+ scaleStateController.scaleState = PhotoViewScaleState.zoomedIn;
}
void _closePreview() {
Navigator.of(context).pop();
}
+ Object _heroTagForIndex(int index) {
+ if (index == widget.initialIndex && widget.heroTag != null) {
+ return widget.heroTag!;
+ }
+ return ImageViewer.heroTagFor(widget.imageUrls[index], index);
+ }
+
+ void _goToPreviousImage() {
+ if (_currentIndex <= 0) return;
+ _pageController.previousPage(
+ duration: const Duration(milliseconds: 250),
+ curve: Curves.easeInOut,
+ );
+ }
+
+ void _goToNextImage() {
+ if (_currentIndex >= widget.imageUrls.length - 1) return;
+ _pageController.nextPage(
+ duration: const Duration(milliseconds: 250),
+ curve: Curves.easeInOut,
+ );
+ }
+
+ Widget _buildNavButton({
+ required IconData icon,
+ required VoidCallback? onPressed,
+ }) {
+ return Container(
+ decoration: BoxDecoration(
+ color: Colors.black.withValues(alpha: 0.5),
+ shape: BoxShape.circle,
+ ),
+ child: IconButton(
+ onPressed: onPressed,
+ icon: Icon(
+ icon,
+ color: onPressed != null ? Colors.white : Colors.white38,
+ size: 32,
+ ),
+ ),
+ );
+ }
+
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Stack(
children: [
- Listener(
- onPointerSignal: _handlePointerSignal,
- child: GestureDetector(
- onTap: _closePreview,
- child: PhotoView(
- imageProvider: CachedNetworkImageProvider(widget.imageUrl),
- controller: _photoViewController,
- scaleStateController: _scaleStateController,
- minScale: PhotoViewComputedScale.contained,
- maxScale: PhotoViewComputedScale.covered * 3,
- backgroundDecoration: const BoxDecoration(color: Colors.black),
- 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,
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- ),
- // 关闭按钮
+ _isMultiImage ? _buildGallery() : _buildSingleImage(),
Positioned(
top: MediaQuery.of(context).padding.top + 16,
right: 16,
@@ -147,8 +213,144 @@ class _ImageViewerState extends State<ImageViewer> {
),
),
),
+ if (_isMultiImage)
+ Positioned(
+ bottom: MediaQuery.of(context).padding.bottom + 24,
+ left: 0,
+ right: 0,
+ child: Center(
+ child: Container(
+ padding:
+ const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
+ decoration: BoxDecoration(
+ color: Colors.black.withValues(alpha: 0.5),
+ borderRadius: BorderRadius.circular(16),
+ ),
+ child: Text(
+ '${_currentIndex + 1} / ${widget.imageUrls.length}',
+ style: const TextStyle(color: Colors.white, fontSize: 14),
+ ),
+ ),
+ ),
+ ),
+ if (isDesktop() && _isMultiImage) ...[
+ Positioned(
+ left: 16,
+ top: 0,
+ bottom: 0,
+ child: Center(
+ child: _buildNavButton(
+ icon: Icons.chevron_left,
+ onPressed: _currentIndex > 0 ? _goToPreviousImage : null,
+ ),
+ ),
+ ),
+ Positioned(
+ right: 16,
+ top: 0,
+ bottom: 0,
+ child: Center(
+ child: _buildNavButton(
+ icon: Icons.chevron_right,
+ onPressed: _currentIndex < widget.imageUrls.length - 1
+ ? _goToNextImage
+ : null,
+ ),
+ ),
+ ),
+ ],
+ ],
+ ),
+ );
+ }
+
+ Widget _buildErrorWidget(BuildContext context) {
+ return 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,
+ ),
+ ),
],
),
);
}
+
+ Widget _buildSingleImage() {
+ return Listener(
+ onPointerSignal: _handlePointerSignal,
+ child: GestureDetector(
+ onTap: _closePreview,
+ child: PhotoView(
+ imageProvider:
+ CachedNetworkImageProvider(widget.imageUrls.first),
+ controller: _photoViewController,
+ scaleStateController: _scaleStateController,
+ minScale: PhotoViewComputedScale.contained,
+ maxScale: PhotoViewComputedScale.covered * 3,
+ backgroundDecoration: const BoxDecoration(color: Colors.black),
+ heroAttributes: widget.heroTag != null
+ ? PhotoViewHeroAttributes(tag: widget.heroTag!)
+ : null,
+ loadingBuilder: (context, event) => const Center(
+ child: CircularProgressIndicator(),
+ ),
+ errorBuilder: (context, error, stackTrace) =>
+ _buildErrorWidget(context),
+ ),
+ ),
+ );
+ }
+
+ Widget _buildGallery() {
+ return Listener(
+ onPointerSignal: _handlePointerSignal,
+ child: GestureDetector(
+ onTap: _closePreview,
+ child: PhotoViewGallery.builder(
+ pageController: _pageController,
+ itemCount: widget.imageUrls.length,
+ onPageChanged: (index) => setState(() => _currentIndex = index),
+ backgroundDecoration: const BoxDecoration(color: Colors.black),
+ loadingBuilder: (context, event) => const Center(
+ child: CircularProgressIndicator(),
+ ),
+ builder: (context, index) {
+ final imageUrl = widget.imageUrls[index];
+ final controller = _galleryControllers.putIfAbsent(
+ index,
+ PhotoViewController.new,
+ );
+ final scaleStateController =
+ _galleryScaleStateControllers.putIfAbsent(
+ index,
+ PhotoViewScaleStateController.new,
+ );
+ return PhotoViewGalleryPageOptions(
+ imageProvider: CachedNetworkImageProvider(imageUrl),
+ controller: controller,
+ scaleStateController: scaleStateController,
+ minScale: PhotoViewComputedScale.contained,
+ maxScale: PhotoViewComputedScale.covered * 3,
+ heroAttributes: index == _currentIndex
+ ? PhotoViewHeroAttributes(tag: _heroTagForIndex(index))
+ : null,
+ errorBuilder: (context, error, stackTrace) =>
+ _buildErrorWidget(context),
+ );
+ },
+ ),
+ ),
+ );
+ }
}
diff --git a/lib/pages/index_module.dart b/lib/pages/index_module.dart
index 2818f936..19ef08e9 100644
--- a/lib/pages/index_module.dart
+++ b/lib/pages/index_module.dart
@@ -81,7 +81,8 @@ class IndexModule extends Module {
child: (_) {
final args = Modular.args.data as ImageViewerRouteArgs;
return ImageViewer(
- imageUrl: args.imageUrl,
+ imageUrls: args.imageUrls,
+ initialIndex: args.initialIndex,
heroTag: args.heroTag,
);
},
diff --git a/lib/pages/info/character_page.dart b/lib/pages/info/character_page.dart
index fd06ca5c..1c23cfc6 100644
--- a/lib/pages/info/character_page.dart
+++ b/lib/pages/info/character_page.dart
@@ -132,11 +132,17 @@ class _CharacterPageState extends State<CharacterPage> {
child: GestureDetector(
onTap: () => ImageViewer.show(
context,
- imageUrl: characterFullItem.image,
- heroTag: characterFullItem.image,
+ imageUrls: [characterFullItem.image],
+ heroTag: ImageViewer.heroTagFor(
+ characterFullItem.image,
+ 0,
+ ),
),
child: Hero(
- tag: characterFullItem.image,
+ tag: ImageViewer.heroTagFor(
+ characterFullItem.image,
+ 0,
+ ),
child: NetworkImgLayer(
width: constraints.maxWidth,
height: constraints.maxHeight,