feat: add imageAuthenticationResolver in the ChatOptions for CachedNetworkImage image requests

This commit is contained in:
Freek van de Ven 2025-02-20 14:35:10 +01:00
parent f93a450259
commit 836686f8d2
4 changed files with 43 additions and 18 deletions

View file

@ -20,6 +20,7 @@
- Changed the ChatBottomInputSection to be multiline and go from 45px to 120px in height depending on how many lines are in the textfield
- Added chatScreenBuilder to the userstory configuration to customize the specific chat screen with a ChatModel as argument
- Added senderTitleResolver to the ChatOptions to resolve the title of the sender in the chat message
- Added imageAuthenticationResolver to the ChatOptions to resolve the authentication headers for CachedNetworkImage for all images in the userstory
## 4.0.0
- Move to the new user story architecture

View file

@ -23,6 +23,7 @@ class ChatOptions {
this.iconDisabledColor,
this.chatAlignment,
this.onNoChats,
this.imageAuthenticationResolver,
ChatRepositoryInterface? chatRepository,
UserRepositoryInterface? userRepository,
}) : chatRepository = chatRepository ?? LocalChatRepository(),
@ -90,6 +91,11 @@ class ChatOptions {
/// [onNoChats] is a function that is triggered when there are no chats.
final Function? onNoChats;
/// If [imageAuthenticationResolver] is set, it will be used to get the
/// authentication headers for an image. The [imageUrl] is provided to allow
/// for different authentication headers for different images.
final ImageAuthenticationResolver? imageAuthenticationResolver;
}
/// Typedef for the chatTitleResolver function that is used to get a title for
@ -100,6 +106,12 @@ typedef ChatTitleResolver = String? Function(ChatModel chat);
/// a sender.
typedef SenderTitleResolver = String? Function(UserModel? user);
/// Typedef for the imageAuthenticationResolver function that is used to get
/// authentication headers for an image.
typedef ImageAuthenticationResolver = Map<String, String>? Function(
String imageUrl,
);
/// Typedef for the messageThemeResolver function that is used to get a
/// [MessageTheme] for a message. This can return null so you can fall back to
/// default values for some messages.

View file

@ -241,7 +241,10 @@ class _DefaultChatImage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
var textTheme = Theme.of(context).textTheme;
var imageUrl = message.imageUrl!;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: SizedBox(
@ -251,10 +254,11 @@ class _DefaultChatImage extends StatelessWidget {
child: AnimatedSize(
duration: const Duration(milliseconds: 300),
child: CachedNetworkImage(
imageUrl: message.imageUrl!,
imageUrl: imageUrl,
fit: BoxFit.fitWidth,
httpHeaders: options.imageAuthenticationResolver?.call(imageUrl),
errorWidget: (context, url, error) => Text(
"Something went wrong",
"Something went wrong with loading the image",
style: textTheme.bodyLarge?.copyWith(
color: messageTheme.textColor,
),

View file

@ -161,6 +161,8 @@ class OldChatMessageBuilder extends StatelessWidget {
: message.isImageMessage
? CachedNetworkImage(
imageUrl: message.imageUrl ?? "",
httpHeaders: options.imageAuthenticationResolver
?.call(message.imageUrl ?? ""),
)
: const SizedBox.shrink(),
),
@ -182,7 +184,11 @@ class _ChatImage extends StatelessWidget {
final String image;
@override
Widget build(BuildContext context) => Container(
Widget build(BuildContext context) {
var chatScope = ChatScope.of(context);
var options = chatScope.options;
return Container(
clipBehavior: Clip.hardEdge,
decoration: BoxDecoration(
color: Colors.black,
@ -194,8 +200,10 @@ class _ChatImage extends StatelessWidget {
? CachedNetworkImage(
fadeInDuration: Duration.zero,
imageUrl: image,
httpHeaders: options.imageAuthenticationResolver?.call(image),
fit: BoxFit.cover,
)
: null,
);
}
}