304 lines
8.9 KiB
Dart
304 lines
8.9 KiB
Dart
// Full-screen picture viewer, modelled on dsh-Go's image gallery: black
|
|||
|
|
// background, one picture per page, pinch to 8x, double-tap to 2.5x around the
|
||
|
|
// touched point, drag to pan. Paging is disabled while a picture is zoomed so
|
||
|
|
// a drag pans the picture instead of swiping to the next one.
|
||
|
|
import 'dart:convert';
|
||
|
|
import 'dart:typed_data';
|
||
|
|
|
||
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter/services.dart';
|
||
|
|
|
||
|
|
import '../api/client.dart';
|
||
|
|
import '../l10n.dart';
|
||
|
|
|
||
|
|
/// One picture in the viewer. [source] is a `data:` URL or a file path the
|
||
|
|
/// client can read with the session's directory.
|
||
|
|
class ImageSlide {
|
||
|
|
final String title;
|
||
|
|
final String source;
|
||
|
|
const ImageSlide(this.title, this.source);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Opens [images] at [initial] as a full-screen gallery.
|
||
|
|
Future<void> showImageViewer(
|
||
|
|
BuildContext context, {
|
||
|
|
required List<ImageSlide> images,
|
||
|
|
int initial = 0,
|
||
|
|
required String directory,
|
||
|
|
required Client client,
|
||
|
|
}) {
|
||
|
|
if (images.isEmpty) return Future.value();
|
||
|
|
return Navigator.of(context).push(PageRouteBuilder<void>(
|
||
|
|
opaque: true,
|
||
|
|
transitionDuration: const Duration(milliseconds: 180),
|
||
|
|
reverseTransitionDuration: const Duration(milliseconds: 140),
|
||
|
|
pageBuilder: (_, __, ___) => ImageViewerPage(
|
||
|
|
images: images,
|
||
|
|
initial: initial.clamp(0, images.length - 1),
|
||
|
|
directory: directory,
|
||
|
|
client: client,
|
||
|
|
),
|
||
|
|
transitionsBuilder: (_, animation, __, child) =>
|
||
|
|
FadeTransition(opacity: animation, child: child),
|
||
|
|
));
|
||
|
|
}
|
||
|
|
|
||
|
|
class ImageViewerPage extends StatefulWidget {
|
||
|
|
final List<ImageSlide> images;
|
||
|
|
final int initial;
|
||
|
|
final String directory;
|
||
|
|
final Client client;
|
||
|
|
|
||
|
|
const ImageViewerPage({
|
||
|
|
super.key,
|
||
|
|
required this.images,
|
||
|
|
this.initial = 0,
|
||
|
|
required this.directory,
|
||
|
|
required this.client,
|
||
|
|
});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<ImageViewerPage> createState() => _ImageViewerPageState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _ImageViewerPageState extends State<ImageViewerPage> {
|
||
|
|
late int _index = widget.initial;
|
||
|
|
late final PageController _pages = PageController(initialPage: _index);
|
||
|
|
bool _zoomed = false;
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
_pages.dispose();
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
final padding = MediaQuery.paddingOf(context);
|
||
|
|
final image = widget.images[_index];
|
||
|
|
return AnnotatedRegion<SystemUiOverlayStyle>(
|
||
|
|
value: SystemUiOverlayStyle.light,
|
||
|
|
child: Scaffold(
|
||
|
|
backgroundColor: Colors.black,
|
||
|
|
body: Stack(
|
||
|
|
children: [
|
||
|
|
Positioned.fill(
|
||
|
|
child: PageView.builder(
|
||
|
|
controller: _pages,
|
||
|
|
physics: _zoomed
|
||
|
|
? const NeverScrollableScrollPhysics()
|
||
|
|
: const PageScrollPhysics(),
|
||
|
|
itemCount: widget.images.length,
|
||
|
|
onPageChanged: (index) => setState(() {
|
||
|
|
_index = index;
|
||
|
|
_zoomed = false;
|
||
|
|
}),
|
||
|
|
itemBuilder: (context, index) => _ZoomableImage(
|
||
|
|
// A page that leaves the viewport forgets its zoom.
|
||
|
|
key: ValueKey((index, index == _index)),
|
||
|
|
slide: widget.images[index],
|
||
|
|
directory: widget.directory,
|
||
|
|
client: widget.client,
|
||
|
|
onZoomChanged: (zoomed) {
|
||
|
|
if (index == _index && zoomed != _zoomed) {
|
||
|
|
setState(() => _zoomed = zoomed);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
// A dark scrim keeps the white title and keys readable over a
|
||
|
|
// bright picture.
|
||
|
|
Positioned(
|
||
|
|
top: 0,
|
||
|
|
left: 0,
|
||
|
|
right: 0,
|
||
|
|
child: IgnorePointer(
|
||
|
|
child: Container(
|
||
|
|
height: padding.top + 64,
|
||
|
|
decoration: const BoxDecoration(
|
||
|
|
gradient: LinearGradient(
|
||
|
|
begin: Alignment.topCenter,
|
||
|
|
end: Alignment.bottomCenter,
|
||
|
|
colors: [Colors.black54, Colors.transparent],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
Positioned(
|
||
|
|
top: padding.top + 4,
|
||
|
|
left: 8,
|
||
|
|
right: 8,
|
||
|
|
child: Row(
|
||
|
|
children: [
|
||
|
|
IconButton(
|
||
|
|
icon: const Icon(Icons.arrow_back),
|
||
|
|
color: Colors.white,
|
||
|
|
tooltip: tr('Back'),
|
||
|
|
onPressed: () => Navigator.of(context).maybePop(),
|
||
|
|
),
|
||
|
|
const SizedBox(width: 4),
|
||
|
|
Expanded(
|
||
|
|
child: Text(
|
||
|
|
image.title,
|
||
|
|
maxLines: 1,
|
||
|
|
overflow: TextOverflow.ellipsis,
|
||
|
|
style: const TextStyle(
|
||
|
|
color: Colors.white,
|
||
|
|
fontSize: 16,
|
||
|
|
fontWeight: FontWeight.w500),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
if (widget.images.length > 1)
|
||
|
|
Padding(
|
||
|
|
padding: const EdgeInsets.only(right: 8),
|
||
|
|
child: Text(
|
||
|
|
'${_index + 1} / ${widget.images.length}',
|
||
|
|
style: const TextStyle(
|
||
|
|
color: Colors.white70, fontSize: 13),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
class _ZoomableImage extends StatefulWidget {
|
||
|
|
final ImageSlide slide;
|
||
|
|
final String directory;
|
||
|
|
final Client client;
|
||
|
|
final ValueChanged<bool>? onZoomChanged;
|
||
|
|
|
||
|
|
const _ZoomableImage({
|
||
|
|
super.key,
|
||
|
|
required this.slide,
|
||
|
|
required this.directory,
|
||
|
|
required this.client,
|
||
|
|
this.onZoomChanged,
|
||
|
|
});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<_ZoomableImage> createState() => _ZoomableImageState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _ZoomableImageState extends State<_ZoomableImage> {
|
||
|
|
static const double _maxScale = 8;
|
||
|
|
static const double _doubleTapScale = 2.5;
|
||
|
|
|
||
|
|
final TransformationController _transform = TransformationController();
|
||
|
|
Uint8List? _bytes;
|
||
|
|
Object? _error;
|
||
|
|
bool _loading = true;
|
||
|
|
bool _zoomed = false;
|
||
|
|
Offset _doubleTap = Offset.zero;
|
||
|
|
|
||
|
|
@override
|
||
|
|
void initState() {
|
||
|
|
super.initState();
|
||
|
|
_load();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
void dispose() {
|
||
|
|
_transform.dispose();
|
||
|
|
super.dispose();
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _load() async {
|
||
|
|
try {
|
||
|
|
final source = widget.slide.source;
|
||
|
|
final bytes = source.startsWith('data:')
|
||
|
|
? _dataUrlBytes(source)
|
||
|
|
: Uint8List.fromList(
|
||
|
|
await widget.client.rawFile(source, directory: widget.directory));
|
||
|
|
if (!mounted) return;
|
||
|
|
setState(() {
|
||
|
|
_bytes = bytes;
|
||
|
|
_loading = false;
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
if (!mounted) return;
|
||
|
|
setState(() {
|
||
|
|
_error = e;
|
||
|
|
_loading = false;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static Uint8List _dataUrlBytes(String source) {
|
||
|
|
final comma = source.indexOf(',');
|
||
|
|
if (comma < 0) throw Exception(tr('Invalid data URL'));
|
||
|
|
if (!source.substring(5, comma).contains('base64')) {
|
||
|
|
throw Exception(tr('Unsupported data URL'));
|
||
|
|
}
|
||
|
|
return base64Decode(source.substring(comma + 1));
|
||
|
|
}
|
||
|
|
|
||
|
|
void _notifyZoom() {
|
||
|
|
final zoomed = _transform.value.getMaxScaleOnAxis() > 1.01;
|
||
|
|
if (zoomed == _zoomed) return;
|
||
|
|
setState(() => _zoomed = zoomed);
|
||
|
|
widget.onZoomChanged?.call(zoomed);
|
||
|
|
}
|
||
|
|
|
||
|
|
void _toggleZoom() {
|
||
|
|
final scale = _transform.value.getMaxScaleOnAxis();
|
||
|
|
if (scale > 1.01) {
|
||
|
|
_transform.value = Matrix4.identity();
|
||
|
|
} else {
|
||
|
|
final p = _doubleTap;
|
||
|
|
_transform.value = Matrix4.identity()
|
||
|
|
..translate(p.dx, p.dy)
|
||
|
|
..scale(_doubleTapScale)
|
||
|
|
..translate(-p.dx, -p.dy);
|
||
|
|
}
|
||
|
|
_notifyZoom();
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
if (_loading) {
|
||
|
|
return const Center(
|
||
|
|
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white));
|
||
|
|
}
|
||
|
|
if (_bytes == null) {
|
||
|
|
return Center(
|
||
|
|
child: Padding(
|
||
|
|
padding: const EdgeInsets.all(32),
|
||
|
|
child: Text(
|
||
|
|
trArgs('Cannot preview: {0}', ['$_error']),
|
||
|
|
textAlign: TextAlign.center,
|
||
|
|
style: const TextStyle(color: Colors.white70),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return GestureDetector(
|
||
|
|
onDoubleTapDown: (details) => _doubleTap = details.localPosition,
|
||
|
|
onDoubleTap: _toggleZoom,
|
||
|
|
child: InteractiveViewer(
|
||
|
|
transformationController: _transform,
|
||
|
|
minScale: 1,
|
||
|
|
maxScale: _maxScale,
|
||
|
|
// One finger swipes to the next picture until a pinch zooms in; from
|
||
|
|
// then on it pans the zoomed picture instead.
|
||
|
|
panEnabled: _zoomed,
|
||
|
|
scaleEnabled: true,
|
||
|
|
onInteractionEnd: (_) => _notifyZoom(),
|
||
|
|
child: SizedBox.expand(
|
||
|
|
child: Image.memory(
|
||
|
|
_bytes!,
|
||
|
|
fit: BoxFit.contain,
|
||
|
|
gaplessPlayback: true,
|
||
|
|
),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|