Skip to content

Commit

Permalink
fix: 🐛 added profile picture support with base 64 string data #142
Browse files Browse the repository at this point in the history
  • Loading branch information
apurva010 committed May 17, 2024
1 parent 179c769 commit fc9fe46
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
11 changes: 10 additions & 1 deletion lib/src/models/chat_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@ class ChatUser {
/// Provides profile picture URL of user.
final String? profilePhoto;

/// Provides profile picture's data in base64 string
final String? profilePhotoBase64Data;

ChatUser({
required this.id,
required this.name,
this.profilePhoto,
});
this.profilePhotoBase64Data,
}) : assert(
!(profilePhoto != null && profilePhotoBase64Data != null),
'Please provide either profilePhoto or profilePhotoData',
);

factory ChatUser.fromJson(Map<String, dynamic> json) => ChatUser(
id: json["id"],
name: json["name"],
profilePhoto: json["profilePhoto"],
profilePhotoBase64Data: json['profilePhotoBase64Data'],
);

Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'profilePhoto': profilePhoto,
'profilePhotoData': profilePhotoBase64Data,
};
}
4 changes: 4 additions & 0 deletions lib/src/models/profile_circle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ProfileCircleConfiguration {
/// Provides image url of user
final String? profileImageUrl;

/// Provides profile picture's data in base64 string
final String? profilePhotoBase64Data;

/// Used for give bottom padding to profile circle
final double? bottomPadding;

Expand All @@ -49,5 +52,6 @@ class ProfileCircleConfiguration {
this.bottomPadding,
this.circleRadius,
this.onAvatarLongPress,
this.profilePhotoBase64Data,
});
}
2 changes: 2 additions & 0 deletions lib/src/widgets/chat_bubble_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
: profileCircleConfig?.bottomPadding ?? 2,
profileCirclePadding: profileCircleConfig?.padding,
imageUrl: messagedUser?.profilePhoto,
imageBase64Data: messagedUser?.profilePhotoBase64Data,
circleRadius: profileCircleConfig?.circleRadius,
onTap: () => _onAvatarTap(messagedUser),
onLongPress: () => _onAvatarLongPress(messagedUser),
Expand Down Expand Up @@ -231,6 +232,7 @@ class _ChatBubbleWidgetState extends State<ChatBubbleWidget> {
: profileCircleConfig?.bottomPadding ?? 2,
profileCirclePadding: profileCircleConfig?.padding,
imageUrl: currentUser?.profilePhoto,
imageBase64Data: currentUser?.profilePhotoBase64Data,
circleRadius: profileCircleConfig?.circleRadius,
onTap: () => _onAvatarTap(messagedUser),
onLongPress: () => _onAvatarLongPress(messagedUser),
Expand Down
4 changes: 4 additions & 0 deletions lib/src/widgets/chat_groupedlist_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class _ChatGroupedListWidgetState extends State<ChatGroupedListWidget>
chatBubbleConfig: chatBubbleConfig?.inComingChatBubbleConfig,
showIndicator: widget.showTypingIndicator,
profilePic: profileCircleConfig?.profileImageUrl,
profilePicBase64Data:
profileCircleConfig?.profilePhotoBase64Data,
)
: ValueListenableBuilder(
valueListenable: ChatViewInheritedWidget.of(context)!
Expand All @@ -209,6 +211,8 @@ class _ChatGroupedListWidgetState extends State<ChatGroupedListWidget>
chatBubbleConfig?.inComingChatBubbleConfig,
showIndicator: value,
profilePic: profileCircleConfig?.profileImageUrl,
profilePicBase64Data:
profileCircleConfig?.profilePhotoBase64Data,
)),
SizedBox(
height: (MediaQuery.of(context).size.width *
Expand Down
12 changes: 10 additions & 2 deletions lib/src/widgets/profile_circle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import 'package:flutter/material.dart';
import 'dart:convert';

import 'package:chatview/src/utils/constants/constants.dart';
import 'package:flutter/material.dart';

class ProfileCircle extends StatelessWidget {
const ProfileCircle({
Expand All @@ -31,6 +33,7 @@ class ProfileCircle extends StatelessWidget {
this.circleRadius,
this.onTap,
this.onLongPress,
this.imageBase64Data,
}) : super(key: key);

/// Allow users to give default bottom padding according to user case.
Expand All @@ -39,6 +42,9 @@ class ProfileCircle extends StatelessWidget {
/// Allow user to pass image url of user's profile picture.
final String? imageUrl;

/// Provides profile picture's data in base64 string
final String? imageBase64Data;

/// Allow user to set whole padding of profile circle view.
final EdgeInsetsGeometry? profileCirclePadding;

Expand All @@ -61,7 +67,9 @@ class ProfileCircle extends StatelessWidget {
onTap: onTap,
child: CircleAvatar(
radius: circleRadius ?? 16,
backgroundImage: NetworkImage(imageUrl ?? profileImage),
backgroundImage: imageBase64Data != null
? MemoryImage(base64Decode(imageBase64Data!))
: NetworkImage(imageUrl ?? profileImage) as ImageProvider,
),
),
);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/widgets/type_indicator_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TypingIndicator extends StatefulWidget {
this.profilePic,
this.chatBubbleConfig,
this.typeIndicatorConfig,
this.profilePicBase64Data,
}) : super(key: key);

/// Allow user to turn on/off typing indicator.
Expand All @@ -42,6 +43,9 @@ class TypingIndicator extends StatefulWidget {
/// Represents profile picture url of user.
final String? profilePic;

/// Provides profile picture's data in base64 string
final String? profilePicBase64Data;

/// Provides configurations related to chat bubble such as padding, margin, max
/// width etc.
final ChatBubble? chatBubbleConfig;
Expand Down Expand Up @@ -237,6 +241,7 @@ class _TypingIndicatorState extends State<TypingIndicator>
ProfileCircle(
bottomPadding: 0,
imageUrl: widget.profilePic,
imageBase64Data: widget.profilePicBase64Data,
),
bubble,
],
Expand Down

0 comments on commit fc9fe46

Please sign in to comment.