채팅을 할 수 있는 ChatView 패키지 이용하기
채팅하는 View를 만들어보고자 pub.dev에 있는 ChatView 패키지를 이용했다.
https://pub.dev/packages/chatview
위 사이트에서도 설명이 친절하게 되어있는 편이지만, 아직 사용한 사람이 많이 없는 것 같아서 내용을 정리해보려한다!
dependencies:
chatview: ^1.3.1
flutter pub add chatview
import 'package:chatview/chatview.dart';
나는 Scaffold의 AppBar, Drawer도 이용하고자 Scaffold의 body 부분에 ChatView를 넣어주었다. (AppBar, Drawer는 포스팅에서 다루지 않는다.)
ChatView는 currentUser, chatController, chatViewState가 필수인 위젯이기 때문에 먼저 세 개를 선언해준다.
ChatController chatController = ChatController(
initialMessageList: [],
scrollController: ScrollController(),
chatUsers: []
);
ChatUser currentUser = ChatUser(id: '0', name: '');
List<Message> messageList = [
Message(
id: '1',
message: "Hi",
createdAt: createdAt,
sendBy: userId,
),
Message(
id: '2',
message: "Hello",
createdAt: createdAt,
sendBy: userId,
),
];
ChatView(
currentUser: currentUser,
chatController: chatController,
chatViewState: ChatViewState.hasMessages,
)
sendMessageConfig: SendMessageConfiguration( // 메시지 입력창 설정
textFieldBackgroundColor: Colors.grey.shade50,
enableCameraImagePicker: false, // 카메라 제거
enableGalleryImagePicker: false, // 갤러리 제거,
allowRecordingVoice: false, // 녹음(음성메시지) 제거
sendButtonIcon: const Icon(Icons.send_rounded, color: Colors.blue,),
textFieldConfig: TextFieldConfiguration(
maxLines: 35,
hintText: "메시지를 입력해주세요",
textStyle: const TextStyle(color: Colors.black),
borderRadius: BorderRadius.circular(10),
),
),
chatBubbleConfig: ChatBubbleConfiguration(
outgoingChatBubbleConfig: ChatBubble( // 내가 보낸 채팅
textStyle: TextStyle(fontSize: SizeConfig.defaultSize * 1.5, color: Colors.white),
linkPreviewConfig: const LinkPreviewConfiguration(
backgroundColor: Color(0xff272336),
bodyStyle: TextStyle(color: Colors.white),
titleStyle: TextStyle(color: Colors.white),
),
color: Color(0xffFF5C58),
),
inComingChatBubbleConfig: ChatBubble( // 상대방 채팅
linkPreviewConfig: const LinkPreviewConfiguration(
backgroundColor: Color(0xff9f85ff),
bodyStyle: TextStyle(color: Colors.black),
titleStyle: TextStyle(color: Colors.black),
),
textStyle: TextStyle(fontSize: SizeConfig.defaultSize * 1.5, color: Colors.black),
senderNameTextStyle: const TextStyle(color: Colors.black),
color: Colors.grey.shade100,
),
),
chatBackgroundConfig: const ChatBackgroundConfiguration( // 채팅방 배경
backgroundImage: "image URL",
messageTimeIconColor: Colors.grey,
messageTimeTextStyle: TextStyle(color: Colors.grey),
defaultGroupSeparatorConfig: DefaultGroupSeparatorConfiguration(
textStyle: TextStyle(
color: Colors.grey,
fontSize: 17,
),
),
backgroundColor: Colors.white,
),
featureActiveConfig: const FeatureActiveConfig(
enableSwipeToReply: false,
enableReactionPopup: false,
enableDoubleTapToLike: false,
enableReplySnackBar: false,
enableCurrentUserProfileAvatar: false,
enableSwipeToSeeTime: true,
enablePagination: true,
),
void onSendTap(String message, ReplyMessage replyMessage, Message messageType){
final message = Message(
id: '3',
message: "How are you",
createdAt: DateTime.now(),
sendBy: currentUser.id,
replyMessage: replyMessage,
messageType: messageType,
);
chatController.addMessage(message);
}
onSendTap: onSendTap,
reactionPopupConfig: ReactionPopupConfiguration(
emojiConfig: const EmojiConfiguration(
emojiList: [
"❤️", "🥰", "👍🏻", "🥺", "💌", "🌟",
],
size: 24,
),
glassMorphismConfig: GlassMorphismConfiguration(
backgroundColor: Colors.white,
borderRadius: 14,
borderColor: Colors.white,
strokeWidth: 4,
),
shadow: BoxShadow(
color: Colors.grey.shade400,
blurRadius: 20,
),
backgroundColor: Colors.grey.shade200,
// onEmojiTap: (emoji, messageId) =>
// chatController. setReaction(
// emoji: emoji,
// messageId: messageld,
// userId: currentUser.id,
// ),
),
messageConfig: MessageConfiguration( // 채팅 한 개에 이모지 달아주는 뷰
messageReactionConfig: MessageReactionConfiguration(
backgroundColor: Color(0xff383152),
borderColor: Color(0xff383152),
reactedUserCountTextStyle:
TextStyle(color: Colors.white),
reactionCountTextStyle:
TextStyle(color: Colors.white),
reactionsBottomSheetConfig: ReactionsBottomSheetConfiguration(
backgroundColor: Color(0xff272336),
reactedUserTextStyle: TextStyle(
color: Colors.white,
),
reactionWidgetDecoration: BoxDecoration(
color: Color(0xff383152),
boxShadow: [
BoxShadow(
color: Colors.black12,
offset: const Offset(0, 20),
blurRadius: 40,
)
],
borderRadius: BorderRadius.circular(10),
),
),
),
// imageMessageConfig: ImageMessageConfiguration(
// margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 15),
// shareIconConfig: ShareIconConfiguration(
// defaultIconBackgroundColor: Color(0xff383152),
// defaultIconColor: Colors.white,
// ),
// ),
),
페이지네이션 및 서버와 통신하는 부분은 추후 업데이트 해볼 예정!