Merge pull request #707 from zjs81/fix/url-image-bubble-adaptive-size
Size URL image previews to the image's aspect ratio
This commit is contained in:
23 files changed
+401
-25
No files matched your search
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../l10n/l10n.dart';
|
||||
import '../models/app_settings.dart';
|
||||
|
||||
/// Finds and renders image URLs embedded in ordinary text messages.
|
||||
@@ -290,9 +291,18 @@ class _MessageUrlImageFutureBuilderState
|
||||
}
|
||||
|
||||
class MessageUrlImagePreview extends StatelessWidget {
|
||||
const MessageUrlImagePreview({super.key, required this.imageUrl});
|
||||
const MessageUrlImagePreview({
|
||||
super.key,
|
||||
required this.imageUrl,
|
||||
@visibleForTesting this.imageProvider,
|
||||
});
|
||||
|
||||
static const double maxHeight = 400;
|
||||
static const double fallbackWidth = 320;
|
||||
static const double statusHeight = 130;
|
||||
|
||||
final String imageUrl;
|
||||
final ImageProvider? imageProvider;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -347,31 +357,139 @@ class MessageUrlImagePreview extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
},
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: SizedBox(
|
||||
width: 240,
|
||||
height: 240,
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: imageUrl,
|
||||
fit: BoxFit.contain,
|
||||
placeholder: (context, url) => ColoredBox(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
child: const Center(
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
errorWidget: (context, url, error) => Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
child: Text(
|
||||
'Unable to load image',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: _AdaptiveUrlImage(
|
||||
provider: imageProvider ?? CachedNetworkImageProvider(imageUrl),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fits an image of [imageWidth] x [imageHeight] into [maxWidth], keeping its
|
||||
/// aspect ratio. Images taller than [maxHeight] are scaled down whole instead.
|
||||
Size urlImagePreviewSize(
|
||||
double imageWidth,
|
||||
double imageHeight,
|
||||
double maxWidth, {
|
||||
double maxHeight = MessageUrlImagePreview.maxHeight,
|
||||
}) {
|
||||
var width = maxWidth;
|
||||
var height = width * imageHeight / imageWidth;
|
||||
if (height > maxHeight) {
|
||||
height = maxHeight;
|
||||
width = height * imageWidth / imageHeight;
|
||||
}
|
||||
return Size(width, height);
|
||||
}
|
||||
|
||||
class _AdaptiveUrlImage extends StatefulWidget {
|
||||
const _AdaptiveUrlImage({required this.provider});
|
||||
|
||||
final ImageProvider provider;
|
||||
|
||||
@override
|
||||
State<_AdaptiveUrlImage> createState() => _AdaptiveUrlImageState();
|
||||
}
|
||||
|
||||
class _AdaptiveUrlImageState extends State<_AdaptiveUrlImage> {
|
||||
ImageStream? _stream;
|
||||
late final ImageStreamListener _listener = ImageStreamListener(
|
||||
(info, _) {
|
||||
if (!mounted) {
|
||||
info.dispose();
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_info?.dispose();
|
||||
_info = info;
|
||||
_failed = false;
|
||||
});
|
||||
},
|
||||
onError: (_, _) {
|
||||
if (mounted) setState(() => _failed = true);
|
||||
},
|
||||
);
|
||||
ImageInfo? _info;
|
||||
bool _failed = false;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_resolve();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _AdaptiveUrlImage oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.provider != widget.provider) {
|
||||
_info?.dispose();
|
||||
_info = null;
|
||||
_failed = false;
|
||||
_resolve();
|
||||
}
|
||||
}
|
||||
|
||||
void _resolve() {
|
||||
final stream = widget.provider.resolve(
|
||||
createLocalImageConfiguration(context),
|
||||
);
|
||||
if (stream.key == _stream?.key) return;
|
||||
_stream?.removeListener(_listener);
|
||||
_stream = stream..addListener(_listener);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_stream?.removeListener(_listener);
|
||||
_info?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = Theme.of(context).colorScheme.surfaceContainerHighest;
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final maxWidth = constraints.maxWidth.isFinite
|
||||
? constraints.maxWidth
|
||||
: MessageUrlImagePreview.fallbackWidth;
|
||||
final info = _info;
|
||||
final Widget child;
|
||||
if (info != null) {
|
||||
final size = urlImagePreviewSize(
|
||||
info.image.width.toDouble(),
|
||||
info.image.height.toDouble(),
|
||||
maxWidth,
|
||||
);
|
||||
child = SizedBox.fromSize(
|
||||
size: size,
|
||||
child: RawImage(image: info.image, fit: BoxFit.fill),
|
||||
);
|
||||
} else if (_failed) {
|
||||
child = Container(
|
||||
width: maxWidth,
|
||||
height: MessageUrlImagePreview.statusHeight,
|
||||
padding: const EdgeInsets.all(12),
|
||||
alignment: Alignment.centerLeft,
|
||||
color: color,
|
||||
child: Text(
|
||||
context.l10n.messageUrlImageLoadFailed,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
child = SizedBox(
|
||||
width: maxWidth,
|
||||
height: MessageUrlImagePreview.statusHeight,
|
||||
child: ColoredBox(
|
||||
color: color,
|
||||
child: const Center(
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return ClipRRect(borderRadius: BorderRadius.circular(12), child: child);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4880,5 +4880,9 @@
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
},
|
||||
"messageUrlImageLoadFailed": "Unable to load image",
|
||||
"@messageUrlImageLoadFailed": {
|
||||
"description": "Shown in a message bubble when a linked image fails to load"
|
||||
}
|
||||
}
|
||||
@@ -13809,6 +13809,12 @@ abstract class AppLocalizations {
|
||||
/// In en, this message translates to:
|
||||
/// **'{name} claimed a win on time'**
|
||||
String chess_notifyTimeout(String name);
|
||||
|
||||
/// Shown in a message bubble when a linked image fails to load
|
||||
///
|
||||
/// In en, this message translates to:
|
||||
/// **'Unable to load image'**
|
||||
String get messageUrlImageLoadFailed;
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
|
||||
@@ -8309,4 +8309,7 @@ class AppLocalizationsBg extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name поиска победа по време';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8284,4 +8284,7 @@ class AppLocalizationsCs extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return 'Hráč $name uplatnil výhru na čas';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8325,4 +8325,7 @@ class AppLocalizationsDe extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name hat den Sieg wegen Zeit beansprucht';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8194,4 +8194,7 @@ class AppLocalizationsEn extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name claimed a win on time';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8303,4 +8303,7 @@ class AppLocalizationsEs extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name reclamó la victoria por tiempo';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8328,4 +8328,7 @@ class AppLocalizationsFr extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name a réclamé la victoire au temps';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8292,4 +8292,7 @@ class AppLocalizationsHu extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name időtúllépés miatt nyerést követelt';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8315,4 +8315,7 @@ class AppLocalizationsIt extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name ha rivendicato la vittoria a tempo';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -7945,4 +7945,7 @@ class AppLocalizationsJa extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name が時間切れによる勝ちを申請しました';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -7954,4 +7954,7 @@ class AppLocalizationsKo extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name님이 시간 초과 승리를 요청했습니다';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8268,4 +8268,7 @@ class AppLocalizationsNl extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name heeft winst op tijd geclaimd';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8354,4 +8354,7 @@ class AppLocalizationsPl extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name zgłosił(a) wygraną na czas';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8291,6 +8291,9 @@ class AppLocalizationsPt extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name reivindicou vitória por tempo';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
|
||||
/// The translations for Portuguese, as used in Portugal (`pt_PT`).
|
||||
|
||||
@@ -8334,4 +8334,7 @@ class AppLocalizationsRu extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name требует победу по времени';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8293,4 +8293,7 @@ class AppLocalizationsSk extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return 'Hráč $name uplatnil výhru na čas';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8293,4 +8293,7 @@ class AppLocalizationsSl extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return 'Igralec $name je zahteval zmago na čas';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8242,4 +8242,7 @@ class AppLocalizationsSv extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name krävde vinst på tid';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -8348,4 +8348,7 @@ class AppLocalizationsUk extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name вимагає перемогу за часом';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
@@ -7784,4 +7784,7 @@ class AppLocalizationsZh extends AppLocalizations {
|
||||
String chess_notifyTimeout(String name) {
|
||||
return '$name 申请超时判胜';
|
||||
}
|
||||
|
||||
@override
|
||||
String get messageUrlImageLoadFailed => 'Unable to load image';
|
||||
}
|
||||
Reference in new issue
Block a user