2023-07-29 02:17:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:palette_generator/palette_generator.dart';
|
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:photo_view/photo_view.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
|
|
|
|
ImagesDatabase imagesDatabase = ImagesDatabase();
|
|
|
|
|
|
|
|
class ImagesDatabase {
|
|
|
|
/*
|
|
|
|
!!! Using the wrappers so i don't have to rewrite most of the code, because of migration to cached network image
|
|
|
|
*/
|
|
|
|
|
|
|
|
void saveImage(String url) {
|
|
|
|
CachedNetworkImageProvider(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<PaletteGenerator> getPaletteGenerator(String url) {
|
|
|
|
return PaletteGenerator.fromImageProvider(CachedNetworkImageProvider(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Color> getPrimaryColor(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.dominantColor!.color;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isDark(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.colors.first.computeLuminance() > 0.5
|
|
|
|
? false
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CachedImage extends StatefulWidget {
|
2023-09-26 00:06:59 +00:00
|
|
|
final String url;
|
2023-07-29 02:17:26 +00:00
|
|
|
final double? width;
|
|
|
|
final double? height;
|
|
|
|
final bool circular;
|
|
|
|
final bool fullThumb;
|
|
|
|
final bool rounded;
|
|
|
|
|
|
|
|
const CachedImage(
|
|
|
|
{Key? key,
|
2023-09-26 00:06:59 +00:00
|
|
|
required this.url,
|
2023-07-29 02:17:26 +00:00
|
|
|
this.height,
|
|
|
|
this.width,
|
|
|
|
this.circular = false,
|
|
|
|
this.fullThumb = false,
|
|
|
|
this.rounded = false})
|
|
|
|
: super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_CachedImageState createState() => _CachedImageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CachedImageState extends State<CachedImage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
if (widget.rounded)
|
|
|
|
return ClipRRect(
|
2023-09-26 00:06:59 +00:00
|
|
|
borderRadius: BorderRadius.circular(4.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: CachedImage(
|
|
|
|
url: widget.url,
|
|
|
|
height: widget.height,
|
|
|
|
width: widget.width,
|
|
|
|
circular: false,
|
|
|
|
rounded: false,
|
|
|
|
fullThumb: widget.fullThumb),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (widget.circular)
|
|
|
|
return ClipOval(
|
|
|
|
child: CachedImage(
|
|
|
|
url: widget.url,
|
|
|
|
height: widget.height,
|
|
|
|
width: widget.width,
|
|
|
|
circular: false,
|
|
|
|
rounded: false,
|
|
|
|
fullThumb: widget.fullThumb,
|
|
|
|
));
|
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
if (!widget.url.startsWith('http'))
|
2023-07-29 02:17:26 +00:00
|
|
|
return Image.asset(
|
2023-09-26 00:06:59 +00:00
|
|
|
widget.url,
|
2023-07-29 02:17:26 +00:00
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
);
|
|
|
|
|
|
|
|
return CachedNetworkImage(
|
2023-09-26 00:06:59 +00:00
|
|
|
imageUrl: widget.url,
|
2023-07-29 02:17:26 +00:00
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
placeholder: (context, url) {
|
|
|
|
if (widget.fullThumb)
|
|
|
|
return Image.asset(
|
|
|
|
'assets/cover.jpg',
|
|
|
|
width: widget.width,
|
|
|
|
height: widget.height,
|
|
|
|
);
|
|
|
|
return Image.asset('assets/cover_thumb.jpg',
|
|
|
|
width: widget.width, height: widget.height);
|
|
|
|
},
|
2023-09-26 00:06:59 +00:00
|
|
|
errorWidget: (context, url, error) => Image.asset('assets/cover.jpg',
|
|
|
|
width: widget.width, height: widget.height),
|
2023-07-29 02:17:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ZoomableImage extends StatefulWidget {
|
2023-09-26 00:06:59 +00:00
|
|
|
final String url;
|
2023-07-29 02:17:26 +00:00
|
|
|
final bool rounded;
|
|
|
|
final double? width;
|
|
|
|
final bool enableHero;
|
|
|
|
final Object? heroTag;
|
|
|
|
|
|
|
|
ZoomableImage({
|
|
|
|
required this.url,
|
|
|
|
this.rounded = false,
|
|
|
|
this.width,
|
|
|
|
this.enableHero = true,
|
|
|
|
this.heroTag,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_ZoomableImageState createState() => _ZoomableImageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ZoomableImageState extends State<ZoomableImage> {
|
|
|
|
PhotoViewController? controller;
|
|
|
|
bool photoViewOpened = false;
|
|
|
|
late final Object? _key =
|
|
|
|
widget.enableHero ? (widget.heroTag ?? UniqueKey()) : null;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
controller = PhotoViewController()..outputStateStream.listen(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listener of PhotoView scale changes. Used for closing PhotoView by pinch-in
|
|
|
|
void listener(PhotoViewControllerValue value) {
|
|
|
|
if (value.scale! < 0.16 && photoViewOpened) {
|
|
|
|
Navigator.pop(context);
|
|
|
|
photoViewOpened =
|
|
|
|
false; // to avoid multiple pop() when picture are being scaled out too slowly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
print('key: ' + _key.toString());
|
|
|
|
final image = CachedImage(
|
|
|
|
url: widget.url,
|
|
|
|
rounded: widget.rounded,
|
|
|
|
width: widget.width,
|
|
|
|
fullThumb: true,
|
|
|
|
);
|
|
|
|
final child = _key != null
|
|
|
|
? Hero(
|
|
|
|
tag: _key!,
|
|
|
|
child: image,
|
|
|
|
)
|
|
|
|
: image;
|
|
|
|
return GestureDetector(
|
|
|
|
child: Semantics(
|
|
|
|
child: child,
|
|
|
|
label: "Album art".i18n,
|
|
|
|
),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(PageRouteBuilder(
|
|
|
|
opaque: false, // transparent background
|
|
|
|
pageBuilder: (context, animation, __) {
|
|
|
|
print('key: ' + _key.toString());
|
|
|
|
photoViewOpened = true;
|
|
|
|
return FadeTransition(
|
|
|
|
opacity: animation,
|
|
|
|
child: PhotoView(
|
2023-09-26 00:06:59 +00:00
|
|
|
imageProvider: CachedNetworkImageProvider(widget.url),
|
2023-07-29 02:17:26 +00:00
|
|
|
maxScale: 8.0,
|
|
|
|
minScale: 0.2,
|
|
|
|
controller: controller,
|
|
|
|
heroAttributes: _key == null
|
|
|
|
? null
|
|
|
|
: PhotoViewHeroAttributes(tag: _key!),
|
|
|
|
backgroundDecoration: const BoxDecoration(
|
|
|
|
color: Color.fromARGB(0x90, 0, 0, 0))),
|
|
|
|
);
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|