mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-12-18 02:42:34 +01:00
Merge branch 'krille/follow-up-mxc-image' into 'main'
chore: Follow up mxc image fixes See merge request famedly/fluffychat!970
This commit is contained in:
commit
7d96fe7224
@ -63,11 +63,13 @@ class Avatar extends StatelessWidget {
|
|||||||
child: noPic
|
child: noPic
|
||||||
? textWidget
|
? textWidget
|
||||||
: MxcImage(
|
: MxcImage(
|
||||||
|
key: Key(mxContent.toString()),
|
||||||
uri: mxContent,
|
uri: mxContent,
|
||||||
fit: BoxFit.cover,
|
fit: BoxFit.cover,
|
||||||
width: size,
|
width: size,
|
||||||
height: size,
|
height: size,
|
||||||
placeholder: (_) => textWidget,
|
placeholder: (_) => textWidget,
|
||||||
|
cacheKey: mxContent.toString(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -21,6 +21,7 @@ class MxcImage extends StatefulWidget {
|
|||||||
final Curve animationCurve;
|
final Curve animationCurve;
|
||||||
final ThumbnailMethod thumbnailMethod;
|
final ThumbnailMethod thumbnailMethod;
|
||||||
final Widget Function(BuildContext context)? placeholder;
|
final Widget Function(BuildContext context)? placeholder;
|
||||||
|
final String? cacheKey;
|
||||||
|
|
||||||
const MxcImage({
|
const MxcImage({
|
||||||
this.uri,
|
this.uri,
|
||||||
@ -35,6 +36,7 @@ class MxcImage extends StatefulWidget {
|
|||||||
this.retryDuration = const Duration(seconds: 2),
|
this.retryDuration = const Duration(seconds: 2),
|
||||||
this.animationCurve = Curves.linear,
|
this.animationCurve = Curves.linear,
|
||||||
this.thumbnailMethod = ThumbnailMethod.scale,
|
this.thumbnailMethod = ThumbnailMethod.scale,
|
||||||
|
this.cacheKey,
|
||||||
Key? key,
|
Key? key,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
@ -43,7 +45,21 @@ class MxcImage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MxcImageState extends State<MxcImage> {
|
class _MxcImageState extends State<MxcImage> {
|
||||||
Uint8List? _imageData;
|
static final Map<String, Uint8List> _imageDataCache = {};
|
||||||
|
Uint8List? _imageDataNoCache;
|
||||||
|
Uint8List? get _imageData {
|
||||||
|
final cacheKey = widget.cacheKey;
|
||||||
|
return cacheKey == null ? _imageDataNoCache : _imageDataCache[cacheKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
set _imageData(Uint8List? data) {
|
||||||
|
if (data == null) return;
|
||||||
|
final cacheKey = widget.cacheKey;
|
||||||
|
cacheKey == null
|
||||||
|
? _imageDataNoCache = data
|
||||||
|
: _imageDataCache[cacheKey] = data;
|
||||||
|
}
|
||||||
|
|
||||||
bool? _isCached;
|
bool? _isCached;
|
||||||
|
|
||||||
Future<void> _load() async {
|
Future<void> _load() async {
|
||||||
@ -83,7 +99,14 @@ class _MxcImageState extends State<MxcImage> {
|
|||||||
_isCached = false;
|
_isCached = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final remoteData = await http.get(httpUri).then((r) => r.bodyBytes);
|
final response = await http.get(httpUri);
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
if (response.statusCode == 404) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw Exception();
|
||||||
|
}
|
||||||
|
final remoteData = response.bodyBytes;
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -122,6 +145,12 @@ class _MxcImageState extends State<MxcImage> {
|
|||||||
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget placeholder(BuildContext context) =>
|
||||||
|
widget.placeholder?.call(context) ??
|
||||||
|
const Center(
|
||||||
|
child: CircularProgressIndicator.adaptive(),
|
||||||
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final data = _imageData;
|
final data = _imageData;
|
||||||
@ -130,10 +159,7 @@ class _MxcImageState extends State<MxcImage> {
|
|||||||
duration: widget.animationDuration,
|
duration: widget.animationDuration,
|
||||||
crossFadeState:
|
crossFadeState:
|
||||||
data == null ? CrossFadeState.showFirst : CrossFadeState.showSecond,
|
data == null ? CrossFadeState.showFirst : CrossFadeState.showSecond,
|
||||||
firstChild: widget.placeholder?.call(context) ??
|
firstChild: placeholder(context),
|
||||||
const Center(
|
|
||||||
child: CircularProgressIndicator.adaptive(),
|
|
||||||
),
|
|
||||||
secondChild: data == null || data.isEmpty
|
secondChild: data == null || data.isEmpty
|
||||||
? Container()
|
? Container()
|
||||||
: Image.memory(
|
: Image.memory(
|
||||||
@ -141,6 +167,11 @@ class _MxcImageState extends State<MxcImage> {
|
|||||||
width: widget.width,
|
width: widget.width,
|
||||||
height: widget.height,
|
height: widget.height,
|
||||||
fit: widget.fit,
|
fit: widget.fit,
|
||||||
|
errorBuilder: (context, __, ___) {
|
||||||
|
_isCached = false;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||||
|
return placeholder(context);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user