-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathchat_list_widget.dart
More file actions
240 lines (215 loc) · 8.13 KB
/
chat_list_widget.dart
File metadata and controls
240 lines (215 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright (c) 2022 Simform Solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import 'dart:io' if (kIsWeb) 'dart:html';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:scroll_to_index/scroll_to_index.dart';
import '../../chatview.dart';
import '../extensions/extensions.dart';
import 'chat_groupedlist_widget.dart';
import 'reply_popup_widget.dart';
class ChatListWidget extends StatefulWidget {
const ChatListWidget({
Key? key,
required this.chatController,
required this.assignReplyMessage,
this.loadingWidget,
this.loadMoreData,
this.isLastPage,
this.onChatListTap,
this.textFieldConfig,
}) : super(key: key);
/// Provides controller for accessing few function for running chat.
final ChatController chatController;
/// Provides widget for loading view while pagination is enabled.
final Widget? loadingWidget;
/// Provides callback when user actions reaches to top and needs to load more
/// chat
final PaginationCallback? loadMoreData;
/// Provides flag if there is no more next data left in list.
final ValueGetter<bool>? isLastPage;
/// Provides callback for assigning reply message when user swipe to chat
/// bubble.
final ValueSetter<Message> assignReplyMessage;
/// Provides callback when user tap anywhere on whole chat.
final VoidCallback? onChatListTap;
/// Provides configuration for text field config.
final TextFieldConfiguration? textFieldConfig;
@override
State<ChatListWidget> createState() => ChatListWidgetState();
}
class ChatListWidgetState extends State<ChatListWidget> {
ChatController get chatController => widget.chatController;
List<Message> get messageList => chatController.initialMessageList;
/// 使用 chatController.scrollController(由外部传入的 AutoScrollController)
AutoScrollController get autoScrollController =>
chatController.scrollController as AutoScrollController;
FeatureActiveConfig? featureActiveConfig;
ChatUser? currentUser;
bool get isPaginationEnabled =>
featureActiveConfig?.enablePagination ?? false;
@override
void initState() {
super.initState();
_initialize();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (chatViewIW != null) {
featureActiveConfig = chatViewIW!.featureActiveConfig;
currentUser = chatViewIW!.chatController.currentUser;
}
}
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<bool>(
valueListenable: chatViewIW!.showPopUp,
builder: (_, showPopupValue, __) => ChatGroupedListWidget(
isLastPage: widget.isLastPage,
loadMoreData: widget.loadMoreData,
loadingWidget: widget.loadingWidget,
showPopUp: showPopupValue,
scrollController: autoScrollController,
isEnableSwipeToSeeTime:
featureActiveConfig?.enableSwipeToSeeTime ?? true,
assignReplyMessage: widget.assignReplyMessage,
onChatListTap: _onChatListTap,
textFieldConfig: widget.textFieldConfig,
onChatBubbleLongPress: (yCoordinate, xCoordinate, message) {
if (featureActiveConfig?.enableReactionPopup ?? false) {
chatViewIW
?..reactionPopupKey.currentState?.refreshWidget(
message: message,
xCoordinate: xCoordinate,
yCoordinate: yCoordinate,
)
..showPopUp.value = true;
}
if (featureActiveConfig?.enableReplySnackBar ?? false) {
_showReplyPopup(
message: message,
sentByCurrentUser: message.sentBy == currentUser?.id,
);
}
},
),
);
}
void _initialize() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!chatController.messageStreamController.isClosed) {
chatController.messageStreamController.add(messageList);
}
if (messageList.isNotEmpty) _scrollToLastMessage();
});
}
/// Scroll to last message using AutoScrollController
void _scrollToLastMessage() {
if (messageList.isNotEmpty) {
// Scroll to the first index (which is the last message due to reverse: true)
autoScrollController.scrollToIndex(
0,
preferPosition: AutoScrollPosition.begin,
duration: const Duration(milliseconds: 300),
);
}
}
/// Public method to scroll to a specific message index
Future<void> scrollToIndex(int index,
{AutoScrollPosition? preferPosition}) async {
await autoScrollController.scrollToIndex(
index,
preferPosition: preferPosition ?? AutoScrollPosition.begin,
duration: const Duration(milliseconds: 300),
);
}
/// Scroll to a specific message by its ID
Future<void> scrollToMessageById(String messageId) async {
final index = messageList.indexWhere((message) => message.id == messageId);
if (index != -1) {
// Convert to list view index (list is reversed)
final listViewIndex = messageList.length - 1 - index;
await scrollToIndex(listViewIndex,
preferPosition: AutoScrollPosition.middle);
}
}
void _showReplyPopup({
required Message message,
required bool sentByCurrentUser,
}) {
final replyPopup = chatListConfig.replyPopupConfig;
ScaffoldMessenger.of(context)
..clearSnackBars()
..showSnackBar(
SnackBar(
duration: const Duration(hours: 1),
backgroundColor: replyPopup?.backgroundColor ?? Colors.white,
padding: EdgeInsets.zero,
content: replyPopup?.replyPopupBuilder?.call(
message,
sentByCurrentUser,
) ??
ReplyPopupWidget(
buttonTextStyle: replyPopup?.buttonTextStyle,
topBorderColor: replyPopup?.topBorderColor,
onMoreTap: () {
_onChatListTap();
replyPopup?.onMoreTap?.call(
message,
sentByCurrentUser,
);
},
onReportTap: () {
_onChatListTap();
replyPopup?.onReportTap?.call(
message,
);
},
onUnsendTap: () {
_onChatListTap();
replyPopup?.onUnsendTap?.call(
message,
);
},
onReplyTap: () {
widget.assignReplyMessage(message);
if (featureActiveConfig?.enableReactionPopup ?? false) {
chatViewIW?.showPopUp.value = false;
}
ScaffoldMessenger.of(context).hideCurrentSnackBar();
replyPopup?.onReplyTap?.call(message);
},
sentByCurrentUser: sentByCurrentUser,
),
),
).closed;
}
void _onChatListTap() {
widget.onChatListTap?.call();
if (!kIsWeb && (Platform.isIOS || Platform.isAndroid)) {
FocusScope.of(context).unfocus();
}
chatViewIW?.showPopUp.value = false;
ScaffoldMessenger.of(context).hideCurrentSnackBar();
}
}