2020-08-13 19:39:22 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:palette_generator/palette_generator.dart';
|
2020-08-13 19:39:22 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
|
|
|
|
ImagesDatabase imagesDatabase = ImagesDatabase();
|
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
|
2020-06-23 21:23:12 +02:00
|
|
|
class ImagesDatabase {
|
|
|
|
|
|
|
|
/*
|
2020-08-13 19:39:22 +02:00
|
|
|
!!! Using the wrappers so i don't have to rewrite most of the code, because of migration to cached network image
|
2020-06-23 21:23:12 +02:00
|
|
|
*/
|
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
void saveImage(String url) {
|
|
|
|
CachedNetworkImageProvider(url);
|
2020-06-23 21:23:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
Future<PaletteGenerator> getPaletteGenerator(String url) {
|
|
|
|
return PaletteGenerator.fromImageProvider(CachedNetworkImageProvider(url));
|
2020-06-23 21:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Color> getPrimaryColor(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.colors.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isDark(String url) async {
|
|
|
|
PaletteGenerator paletteGenerator = await getPaletteGenerator(url);
|
|
|
|
return paletteGenerator.colors.first.computeLuminance() > 0.5 ? false : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class CachedImage extends StatefulWidget {
|
|
|
|
final String url;
|
|
|
|
final double width;
|
|
|
|
final double height;
|
|
|
|
final bool circular;
|
2020-08-13 19:39:22 +02:00
|
|
|
final bool fullThumb;
|
2020-06-23 21:23:12 +02:00
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
const CachedImage({Key key, this.url, this.height, this.width, this.circular = false, this.fullThumb = false}): super(key: key);
|
2020-06-23 21:23:12 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
_CachedImageState createState() => _CachedImageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CachedImageState extends State<CachedImage> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-08-13 19:39:22 +02:00
|
|
|
if (widget.circular) return ClipOval(
|
|
|
|
child: CachedImage(url: widget.url, height: widget.height, width: widget.width, circular: false)
|
|
|
|
);
|
2020-07-16 22:25:30 +02:00
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
return CachedNetworkImage(
|
|
|
|
imageUrl: widget.url,
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
errorWidget: (context, url, error) => Image.asset('assets/cover_thumb.jpg', width: widget.width, height: widget.height),
|
2020-06-23 21:23:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|