import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:matrix/matrix.dart'; import 'package:pyramid/core/matrix_client.dart'; class MxcImage extends ConsumerWidget { final Uri? mxcUri; final double? width; final double? height; final Widget Function(BuildContext)? placeholder; const MxcImage({ super.key, required this.mxcUri, this.width, this.height, this.placeholder, }); @override Widget build(BuildContext context, WidgetRef ref) { final clientAsync = ref.watch(matrixClientProvider); final uri = mxcUri; if (uri == null) return _fallback(context); return clientAsync.when( loading: () => _fallback(context), error: (e, st) => _fallback(context), data: (client) => FutureBuilder( future: uri.getThumbnailUri( client, width: width != null ? (width! * 2).toInt() : 64, height: height != null ? (height! * 2).toInt() : 64, method: ThumbnailMethod.crop, ), builder: (context, snap) { if (!snap.hasData) return _fallback(context); return Image.network( snap.data!.toString(), width: width, height: height, fit: BoxFit.cover, errorBuilder: (ctx, err, st) => _fallback(context), ); }, ), ); } Widget _fallback(BuildContext context) => placeholder?.call(context) ?? SizedBox(width: width, height: height); }