MusicPlayer/lib/ui/cover_image.dart

85 lines
2.1 KiB
Dart
Raw Normal View History

2022-10-01 12:33:04 +00:00
import 'dart:async';
2022-03-23 20:15:29 +00:00
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:flutter/material.dart';
import 'package:music_web_player/api.dart';
import 'package:music_web_player/config.dart';
2022-10-01 12:33:04 +00:00
class CoverImage extends StatefulWidget {
2022-03-23 20:15:29 +00:00
final MusicEntry music;
final double? width;
final double? height;
final BoxFit? fit;
2022-03-23 20:27:05 +00:00
final Icon? icon;
2022-03-24 08:20:43 +00:00
final Color? backgroundColor;
2022-10-01 12:33:04 +00:00
final Duration? delayLoading;
2022-03-23 20:15:29 +00:00
const CoverImage({
Key? key,
required this.music,
this.width,
this.height,
2022-03-23 20:27:05 +00:00
this.icon,
2022-03-23 20:15:29 +00:00
this.fit,
2022-03-24 08:20:43 +00:00
this.backgroundColor,
2022-10-01 12:33:04 +00:00
this.delayLoading,
2022-03-23 20:15:29 +00:00
}) : super(key: key);
2022-10-01 12:33:04 +00:00
@override
State<StatefulWidget> createState() => _CoverImageState();
}
class _CoverImageState extends State<CoverImage> {
var _canShowImage = true;
Timer? _timer;
@override
void initState() {
super.initState();
if (widget.delayLoading != null) {
_canShowImage = false;
Timer(widget.delayLoading!, () {
if (mounted) {
setState(() => _canShowImage = true);
}
});
}
}
@override
void dispose() {
super.dispose();
_timer?.cancel();
}
2022-03-23 20:15:29 +00:00
@override
Widget build(BuildContext context) {
2022-10-01 12:33:04 +00:00
if (!_canShowImage) return _loadingWidget(null);
2022-03-23 20:15:29 +00:00
return CachedNetworkImage(
2022-10-01 12:33:04 +00:00
width: widget.width,
height: widget.height,
2022-03-23 20:15:29 +00:00
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet,
2022-10-01 12:33:04 +00:00
imageUrl: widget.music.coverURL,
cacheKey: widget.music.coverCacheKey,
2022-03-23 20:15:29 +00:00
httpHeaders: {"Token": config.apiToken},
useOldImageOnUrlChange: false,
2022-03-23 20:27:05 +00:00
progressIndicatorBuilder: (c, s, p) => _loadingWidget(p.progress),
2022-10-01 12:33:04 +00:00
fit: widget.fit,
2022-03-23 20:27:05 +00:00
errorWidget: (c, s, e) => _loadingWidget(null),
2022-03-23 20:15:29 +00:00
);
}
2022-03-23 20:27:05 +00:00
Widget _loadingWidget(double? progress) => Container(
2022-10-01 12:33:04 +00:00
color: widget.backgroundColor ?? Colors.black,
width: widget.width,
height: widget.height,
2022-03-23 20:27:05 +00:00
child: Center(
child: progress == null
2022-10-01 12:33:04 +00:00
? widget.icon
2022-03-23 20:27:05 +00:00
: CircularProgressIndicator(value: progress),
),
);
2022-03-23 20:15:29 +00:00
}