| 
									
										
										
										
											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'; | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ChatPage({ChatModel? chatModel}) { | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  |   final icon = Icon(Icons.chat); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   @override | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |   final appBarActions = [ | 
					
						
							|  |  |  |     PopupMenuButton<int>( | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |         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) { | 
					
						
							|  |  |  |             final id = entry.key; | 
					
						
							| 
									
										
										
										
											2022-05-16 00:01:27 +08:00
										 |  |  |             final user = entry.value.chatUser; | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |             return PopupMenuItem<int>( | 
					
						
							| 
									
										
										
										
											2022-08-04 17:24:02 +08:00
										 |  |  |               child: Text("${user.firstName}   ${user.id}"), | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |               value: id, | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }).toList(); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         onSelected: (id) { | 
					
						
							| 
									
										
										
										
											2022-06-13 21:07:26 +08:00
										 |  |  |           gFFI.chatModel.changeCurrentID(id); | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							| 
									
										
										
										
											2022-08-11 10:19:12 +08:00
										 |  |  |         value: chatModel, | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |         child: Container( | 
					
						
							| 
									
										
										
										
											2022-09-23 16:31:50 +08:00
										 |  |  |             color: Theme.of(context).scaffoldBackgroundColor, | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |             child: Consumer<ChatModel>(builder: (context, chatModel, child) { | 
					
						
							| 
									
										
										
										
											2022-04-04 14:54:00 +08:00
										 |  |  |               final currentUser = chatModel.currentUser; | 
					
						
							|  |  |  |               return Stack( | 
					
						
							|  |  |  |                 children: [ | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                   LayoutBuilder(builder: (context, constraints) { | 
					
						
							| 
									
										
										
										
											2023-02-01 21:28:26 +08:00
										 |  |  |                     final chat = DashChat( | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                       onSend: (chatMsg) { | 
					
						
							|  |  |  |                         chatModel.send(chatMsg); | 
					
						
							| 
									
										
										
										
											2022-11-05 10:35:56 +08:00
										 |  |  |                         chatModel.inputNode.requestFocus(); | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                       }, | 
					
						
							|  |  |  |                       currentUser: chatModel.me, | 
					
						
							|  |  |  |                       messages: chatModel | 
					
						
							|  |  |  |                               .messages[chatModel.currentID]?.chatMessages ?? | 
					
						
							|  |  |  |                           [], | 
					
						
							| 
									
										
										
										
											2022-09-04 16:26:08 +08:00
										 |  |  |                       inputOptions: InputOptions( | 
					
						
							|  |  |  |                           sendOnEnter: true, | 
					
						
							| 
									
										
										
										
											2022-10-26 23:50:36 +09:00
										 |  |  |                           focusNode: chatModel.inputNode, | 
					
						
							| 
									
										
										
										
											2022-09-23 16:31:50 +08:00
										 |  |  |                           inputTextStyle: TextStyle( | 
					
						
							| 
									
										
										
										
											2022-10-14 23:50:13 +09:00
										 |  |  |                               fontSize: 14, | 
					
						
							| 
									
										
										
										
											2022-09-23 16:31:50 +08:00
										 |  |  |                               color: Theme.of(context) | 
					
						
							|  |  |  |                                   .textTheme | 
					
						
							|  |  |  |                                   .titleLarge | 
					
						
							| 
									
										
										
										
											2022-10-14 23:50:13 +09:00
										 |  |  |                                   ?.color), | 
					
						
							|  |  |  |                           inputDecoration: isDesktop | 
					
						
							|  |  |  |                               ? InputDecoration( | 
					
						
							|  |  |  |                                   isDense: true, | 
					
						
							|  |  |  |                                   hintText: | 
					
						
							|  |  |  |                                       "${translate('Write a message')}...", | 
					
						
							|  |  |  |                                   filled: true, | 
					
						
							| 
									
										
										
										
											2023-02-23 16:49:31 +01:00
										 |  |  |                                   fillColor: | 
					
						
							|  |  |  |                                       Theme.of(context).colorScheme.background, | 
					
						
							| 
									
										
										
										
											2022-10-14 23:50:13 +09:00
										 |  |  |                                   contentPadding: EdgeInsets.all(10), | 
					
						
							|  |  |  |                                   border: OutlineInputBorder( | 
					
						
							|  |  |  |                                     borderRadius: BorderRadius.circular(6), | 
					
						
							|  |  |  |                                     borderSide: const BorderSide( | 
					
						
							|  |  |  |                                       width: 0, | 
					
						
							|  |  |  |                                       style: BorderStyle.none, | 
					
						
							|  |  |  |                                     ), | 
					
						
							|  |  |  |                                   ), | 
					
						
							|  |  |  |                                 ) | 
					
						
							|  |  |  |                               : defaultInputDecoration( | 
					
						
							|  |  |  |                                   hintText: | 
					
						
							|  |  |  |                                       "${translate('Write a message')}...", | 
					
						
							| 
									
										
										
										
											2023-02-23 16:49:31 +01:00
										 |  |  |                                   fillColor: | 
					
						
							|  |  |  |                                       Theme.of(context).colorScheme.background), | 
					
						
							| 
									
										
										
										
											2022-10-14 23:50:13 +09:00
										 |  |  |                           sendButtonBuilder: defaultSendButton( | 
					
						
							|  |  |  |                               padding: EdgeInsets.symmetric( | 
					
						
							|  |  |  |                                   horizontal: 6, vertical: 0), | 
					
						
							|  |  |  |                               color: Theme.of(context).colorScheme.primary)), | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                       messageOptions: MessageOptions( | 
					
						
							|  |  |  |                           showOtherUsersAvatar: false, | 
					
						
							| 
									
										
										
										
											2022-10-25 21:36:01 +09:00
										 |  |  |                           textColor: Colors.white, | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                           maxWidth: constraints.maxWidth * 0.7, | 
					
						
							| 
									
										
										
										
											2023-02-02 20:05:57 +09:00
										 |  |  |                           messageTextBuilder: (message, _, __) { | 
					
						
							|  |  |  |                             final isOwnMessage = | 
					
						
							|  |  |  |                                 message.user.id == currentUser.id; | 
					
						
							|  |  |  |                             return Column( | 
					
						
							|  |  |  |                               crossAxisAlignment: isOwnMessage | 
					
						
							|  |  |  |                                   ? CrossAxisAlignment.end | 
					
						
							|  |  |  |                                   : CrossAxisAlignment.start, | 
					
						
							|  |  |  |                               children: <Widget>[ | 
					
						
							|  |  |  |                                 Text(message.text, | 
					
						
							|  |  |  |                                     style: TextStyle(color: Colors.white)), | 
					
						
							|  |  |  |                                 Padding( | 
					
						
							|  |  |  |                                   padding: const EdgeInsets.only(top: 5), | 
					
						
							|  |  |  |                                   child: Text( | 
					
						
							|  |  |  |                                     "${message.createdAt.hour}:${message.createdAt.minute}", | 
					
						
							|  |  |  |                                     style: TextStyle( | 
					
						
							|  |  |  |                                       color: Colors.white, | 
					
						
							|  |  |  |                                       fontSize: 10, | 
					
						
							|  |  |  |                                     ), | 
					
						
							|  |  |  |                                   ), | 
					
						
							|  |  |  |                                 ), | 
					
						
							|  |  |  |                               ], | 
					
						
							|  |  |  |                             ); | 
					
						
							|  |  |  |                           }, | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                           messageDecorationBuilder: (_, __, ___) => | 
					
						
							|  |  |  |                               defaultMessageDecoration( | 
					
						
							|  |  |  |                                 color: MyTheme.accent80, | 
					
						
							|  |  |  |                                 borderTopLeft: 8, | 
					
						
							|  |  |  |                                 borderTopRight: 8, | 
					
						
							|  |  |  |                                 borderBottomRight: 8, | 
					
						
							|  |  |  |                                 borderBottomLeft: 8, | 
					
						
							|  |  |  |                               )), | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2023-02-01 21:28:26 +08:00
										 |  |  |                     return SelectionArea(child: chat); | 
					
						
							| 
									
										
										
										
											2022-08-16 23:07:22 +08:00
										 |  |  |                   }), | 
					
						
							| 
									
										
										
										
											2022-09-26 18:10:54 +08:00
										 |  |  |                   desktopType == DesktopType.cm || | 
					
						
							|  |  |  |                           chatModel.currentID == ChatModel.clientModeID | 
					
						
							| 
									
										
										
										
											2022-04-04 14:54:00 +08:00
										 |  |  |                       ? SizedBox.shrink() | 
					
						
							|  |  |  |                       : Padding( | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |                           padding: EdgeInsets.all(12), | 
					
						
							|  |  |  |                           child: Row( | 
					
						
							|  |  |  |                             children: [ | 
					
						
							|  |  |  |                               Icon(Icons.account_circle, | 
					
						
							|  |  |  |                                   color: MyTheme.accent80), | 
					
						
							|  |  |  |                               SizedBox(width: 5), | 
					
						
							|  |  |  |                               Text( | 
					
						
							| 
									
										
										
										
											2022-08-04 17:24:02 +08:00
										 |  |  |                                 "${currentUser.firstName}   ${currentUser.id}", | 
					
						
							| 
									
										
										
										
											2022-04-05 00:51:47 +08:00
										 |  |  |                                 style: TextStyle(color: MyTheme.accent50), | 
					
						
							|  |  |  |                               ), | 
					
						
							|  |  |  |                             ], | 
					
						
							|  |  |  |                           )), | 
					
						
							| 
									
										
										
										
											2022-04-04 14:54:00 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2022-03-25 16:34:27 +08:00
										 |  |  |               ); | 
					
						
							|  |  |  |             }))); | 
					
						
							| 
									
										
										
										
											2022-03-03 14:58:57 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |