| 
									
										
										
										
											2022-08-04 17:24:02 +08:00
										 |  |  | import 'package:dash_chat_2/dash_chat_2.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/common.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | import 'package:flutter_hbb/models/chat_model.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-19 23:21:13 +02:00
										 |  |  | import 'package:get/get.dart'; | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | import 'package:provider/provider.dart'; | 
					
						
							| 
									
										
										
										
											2022-06-13 21:07:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-25 21:36:01 +09:00
										 |  |  | import '../../mobile/pages/home_page.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  | enum ChatPageType { | 
					
						
							|  |  |  |   mobileMain, | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |   desktopCM, | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  | class ChatPage extends StatelessWidget implements PageShape { | 
					
						
							| 
									
										
										
										
											2022-08-11 10:19:12 +08:00
										 |  |  |   late final ChatModel chatModel; | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |   final ChatPageType? type; | 
					
						
							| 
									
										
										
										
											2022-08-11 10:19:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |   ChatPage({ChatModel? chatModel, this.type}) { | 
					
						
							| 
									
										
										
										
											2022-08-11 10:19:12 +08:00
										 |  |  |     this.chatModel = chatModel ?? gFFI.chatModel; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  |   @override | 
					
						
							| 
									
										
										
										
											2022-03-23 15:28:21 +08:00
										 |  |  |   final title = translate("Chat"); | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							| 
									
										
										
										
											2023-07-10 21:03:35 +08:00
										 |  |  |   final icon = unreadTopRightBuilder(gFFI.chatModel.mobileUnreadSum); | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |   final appBarActions = [ | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |     PopupMenuButton<MessageKey>( | 
					
						
							| 
									
										
										
										
											2023-06-29 10:26:03 +08:00
										 |  |  |         tooltip: "", | 
					
						
							| 
									
										
										
										
											2023-07-10 21:03:35 +08:00
										 |  |  |         icon: unreadTopRightBuilder(gFFI.chatModel.mobileUnreadSum, | 
					
						
							|  |  |  |             icon: Icon(Icons.group)), | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |         itemBuilder: (context) { | 
					
						
							| 
									
										
										
										
											2022-08-11 10:19:12 +08:00
										 |  |  |           // only mobile need [appBarActions], just bind gFFI.chatModel
 | 
					
						
							| 
									
										
										
										
											2022-06-13 21:07:26 +08:00
										 |  |  |           final chatModel = gFFI.chatModel; | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |           return chatModel.messages.entries.map((entry) { | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |             final key = entry.key; | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  |             final user = entry.value.chatUser; | 
					
						
							| 
									
										
										
										
											2023-07-10 09:40:41 +08:00
										 |  |  |             final client = gFFI.serverModel.clients | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |                 .firstWhereOrNull((e) => e.id == key.connId); | 
					
						
							|  |  |  |             final connected = | 
					
						
							|  |  |  |                 gFFI.serverModel.clients.any((e) => e.id == key.connId); | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |             return PopupMenuItem<MessageKey>( | 
					
						
							|  |  |  |               child: Row( | 
					
						
							|  |  |  |                 children: [ | 
					
						
							| 
									
										
										
										
											2023-07-10 09:40:41 +08:00
										 |  |  |                   Icon( | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |                           key.isOut | 
					
						
							| 
									
										
										
										
											2023-07-10 09:40:41 +08:00
										 |  |  |                               ? Icons.call_made_rounded | 
					
						
							|  |  |  |                               : Icons.call_received_rounded, | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |                           color: MyTheme.accent) | 
					
						
							|  |  |  |                       .marginOnly(right: 6), | 
					
						
							|  |  |  |                   Text("${user.firstName}   ${user.id}"), | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |                   if (connected) | 
					
						
							|  |  |  |                     Container( | 
					
						
							|  |  |  |                       width: 10, | 
					
						
							|  |  |  |                       height: 10, | 
					
						
							|  |  |  |                       decoration: BoxDecoration( | 
					
						
							| 
									
										
										
										
											2023-07-10 21:03:35 +08:00
										 |  |  |                           shape: BoxShape.circle, | 
					
						
							|  |  |  |                           color: Color.fromARGB(255, 46, 205, 139)), | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |                     ).marginSymmetric(horizontal: 2), | 
					
						
							| 
									
										
										
										
											2023-07-10 09:40:41 +08:00
										 |  |  |                   if (client != null) | 
					
						
							|  |  |  |                     unreadMessageCountBuilder(client.unreadChatMessageCount) | 
					
						
							| 
									
										
										
										
											2023-07-10 21:03:35 +08:00
										 |  |  |                         .marginOnly(left: 4) | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |                 ], | 
					
						
							|  |  |  |               ), | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |               value: key, | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |             ); | 
					
						
							|  |  |  |           }).toList(); | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |         onSelected: (key) { | 
					
						
							|  |  |  |           gFFI.chatModel.changeCurrentKey(key); | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |         }) | 
					
						
							|  |  |  |   ]; | 
					
						
							| 
									
										
										
										
											2022-02-28 21:26:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							|  |  |  |   Widget build(BuildContext context) { | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |     return ChangeNotifierProvider.value( | 
					
						
							| 
									
										
										
										
											2023-05-19 23:21:13 +02:00
										 |  |  |       value: chatModel, | 
					
						
							|  |  |  |       child: Container( | 
					
						
							|  |  |  |         color: Theme.of(context).scaffoldBackgroundColor, | 
					
						
							|  |  |  |         child: Consumer<ChatModel>( | 
					
						
							|  |  |  |           builder: (context, chatModel, child) { | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |             final readOnly = type == ChatPageType.mobileMain && | 
					
						
							|  |  |  |                     (chatModel.currentKey.connId == ChatModel.clientModeID || | 
					
						
							|  |  |  |                         gFFI.serverModel.clients.every((e) => | 
					
						
							|  |  |  |                             e.id != chatModel.currentKey.connId || | 
					
						
							|  |  |  |                             chatModel.currentUser == null)) || | 
					
						
							|  |  |  |                 type == ChatPageType.desktopCM && | 
					
						
							|  |  |  |                     gFFI.serverModel.clients | 
					
						
							|  |  |  |                             .firstWhereOrNull( | 
					
						
							|  |  |  |                                 (e) => e.id == chatModel.currentKey.connId) | 
					
						
							|  |  |  |                             ?.disconnected == | 
					
						
							|  |  |  |                         true; | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |             return Stack( | 
					
						
							|  |  |  |               children: [ | 
					
						
							|  |  |  |                 LayoutBuilder(builder: (context, constraints) { | 
					
						
							|  |  |  |                   final chat = DashChat( | 
					
						
							| 
									
										
										
										
											2023-06-08 16:00:48 +02:00
										 |  |  |                     onSend: chatModel.send, | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                     currentUser: chatModel.me, | 
					
						
							| 
									
										
										
										
											2023-07-09 19:14:57 +08:00
										 |  |  |                     messages: chatModel | 
					
						
							|  |  |  |                             .messages[chatModel.currentKey]?.chatMessages ?? | 
					
						
							|  |  |  |                         [], | 
					
						
							| 
									
										
										
										
											2023-07-10 16:02:47 +08:00
										 |  |  |                     readOnly: readOnly, | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                     inputOptions: InputOptions( | 
					
						
							|  |  |  |                       focusNode: chatModel.inputNode, | 
					
						
							| 
									
										
										
										
											2023-06-08 16:00:48 +02:00
										 |  |  |                       textController: chatModel.textController, | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                       inputTextStyle: TextStyle( | 
					
						
							|  |  |  |                           fontSize: 14, | 
					
						
							|  |  |  |                           color: Theme.of(context).textTheme.titleLarge?.color), | 
					
						
							|  |  |  |                       inputDecoration: InputDecoration( | 
					
						
							|  |  |  |                         isDense: true, | 
					
						
							|  |  |  |                         hintText: translate('Write a message'), | 
					
						
							|  |  |  |                         filled: true, | 
					
						
							|  |  |  |                         fillColor: Theme.of(context).colorScheme.background, | 
					
						
							|  |  |  |                         contentPadding: EdgeInsets.all(10), | 
					
						
							|  |  |  |                         border: OutlineInputBorder( | 
					
						
							|  |  |  |                           borderRadius: BorderRadius.circular(10.0), | 
					
						
							|  |  |  |                           borderSide: const BorderSide( | 
					
						
							|  |  |  |                             width: 1, | 
					
						
							|  |  |  |                             style: BorderStyle.solid, | 
					
						
							| 
									
										
										
										
											2023-05-29 09:44:38 +08:00
										 |  |  |                           ), | 
					
						
							|  |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2023-05-19 23:21:13 +02:00
										 |  |  |                       ), | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                       sendButtonBuilder: defaultSendButton( | 
					
						
							|  |  |  |                         padding: | 
					
						
							|  |  |  |                             EdgeInsets.symmetric(horizontal: 6, vertical: 0), | 
					
						
							|  |  |  |                         color: MyTheme.accent, | 
					
						
							|  |  |  |                         icon: Icons.send_rounded, | 
					
						
							| 
									
										
										
										
											2023-05-25 12:04:52 +02:00
										 |  |  |                       ), | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                     ), | 
					
						
							|  |  |  |                     messageOptions: MessageOptions( | 
					
						
							|  |  |  |                       showOtherUsersAvatar: false, | 
					
						
							|  |  |  |                       showOtherUsersName: false, | 
					
						
							|  |  |  |                       textColor: Colors.white, | 
					
						
							|  |  |  |                       maxWidth: constraints.maxWidth * 0.7, | 
					
						
							|  |  |  |                       messageTextBuilder: (message, _, __) { | 
					
						
							|  |  |  |                         final isOwnMessage = message.user.id.isBlank!; | 
					
						
							|  |  |  |                         return Column( | 
					
						
							|  |  |  |                           crossAxisAlignment: isOwnMessage | 
					
						
							|  |  |  |                               ? CrossAxisAlignment.end | 
					
						
							|  |  |  |                               : CrossAxisAlignment.start, | 
					
						
							|  |  |  |                           children: <Widget>[ | 
					
						
							|  |  |  |                             Text(message.text, | 
					
						
							|  |  |  |                                 style: TextStyle(color: Colors.white)), | 
					
						
							|  |  |  |                             Text( | 
					
						
							|  |  |  |                               "${message.createdAt.hour}:${message.createdAt.minute.toString().padLeft(2, '0')}", | 
					
						
							|  |  |  |                               style: TextStyle( | 
					
						
							|  |  |  |                                 color: Colors.white, | 
					
						
							|  |  |  |                                 fontSize: 8, | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |                               ), | 
					
						
							| 
									
										
										
										
											2023-06-08 12:51:13 +02:00
										 |  |  |                             ).marginOnly(top: 3), | 
					
						
							|  |  |  |                           ], | 
					
						
							|  |  |  |                         ); | 
					
						
							|  |  |  |                       }, | 
					
						
							|  |  |  |                       messageDecorationBuilder: | 
					
						
							|  |  |  |                           (message, previousMessage, nextMessage) { | 
					
						
							|  |  |  |                         final isOwnMessage = message.user.id.isBlank!; | 
					
						
							|  |  |  |                         return defaultMessageDecoration( | 
					
						
							|  |  |  |                           color: | 
					
						
							|  |  |  |                               isOwnMessage ? MyTheme.accent : Colors.blueGrey, | 
					
						
							|  |  |  |                           borderTopLeft: 8, | 
					
						
							|  |  |  |                           borderTopRight: 8, | 
					
						
							|  |  |  |                           borderBottomRight: isOwnMessage ? 2 : 8, | 
					
						
							|  |  |  |                           borderBottomLeft: isOwnMessage ? 8 : 2, | 
					
						
							|  |  |  |                         ); | 
					
						
							|  |  |  |                       }, | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                   ); | 
					
						
							|  |  |  |                   return SelectionArea(child: chat); | 
					
						
							|  |  |  |                 }), | 
					
						
							|  |  |  |               ], | 
					
						
							|  |  |  |             ).paddingOnly(bottom: 8); | 
					
						
							| 
									
										
										
										
											2023-05-19 23:21:13 +02:00
										 |  |  |           }, | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |