-
Notifications
You must be signed in to change notification settings - Fork 119
/
chat_groupedlist_widget.dart
353 lines (315 loc) · 12.7 KB
/
chat_groupedlist_widget.dart
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* 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 'package:chatview/chatview.dart';
import 'package:chatview/src/extensions/extensions.dart';
import 'package:chatview/src/widgets/type_indicator_widget.dart';
import 'package:flutter/material.dart';
import 'package:grouped_list/grouped_list.dart';
import 'chat_bubble_widget.dart';
import 'chat_group_header.dart';
class ChatGroupedListWidget extends StatefulWidget {
const ChatGroupedListWidget({
Key? key,
required this.showPopUp,
required this.showTypingIndicator,
required this.scrollController,
required this.chatBackgroundConfig,
required this.replyMessage,
required this.assignReplyMessage,
required this.onChatListTap,
required this.onChatBubbleLongPress,
required this.isEnableSwipeToSeeTime,
this.messageConfig,
this.chatBubbleConfig,
this.profileCircleConfig,
this.swipeToReplyConfig,
this.repliedMessageConfig,
this.typeIndicatorConfig,
}) : super(key: key);
/// Allow user to swipe to see time while reaction pop is not open.
final bool showPopUp;
/// Allow user to show typing indicator.
final bool showTypingIndicator;
final ScrollController scrollController;
/// Allow user to give customisation to background of chat
final ChatBackgroundConfiguration chatBackgroundConfig;
/// Allow user to giving customisation different types
/// messages
final MessageConfiguration? messageConfig;
/// Allow user to giving customisation to chat bubble
final ChatBubbleConfiguration? chatBubbleConfig;
/// Allow user to giving customisation to profile circle
final ProfileCircleConfiguration? profileCircleConfig;
/// Allow user to giving customisation to swipe to reply
final SwipeToReplyConfiguration? swipeToReplyConfig;
final RepliedMessageConfiguration? repliedMessageConfig;
/// Allow user to giving customisation typing indicator
final TypeIndicatorConfiguration? typeIndicatorConfig;
/// Provides reply message if actual message is sent by replying any message.
final ReplyMessage replyMessage;
/// Provides callback for assigning reply message when user swipe on chat bubble.
final MessageCallBack assignReplyMessage;
/// Provides callback when user tap anywhere on whole chat.
final VoidCallBack onChatListTap;
/// Provides callback when user press chat bubble for certain time then usual.
final void Function(double, double, Message) onChatBubbleLongPress;
/// Provide flag for turn on/off to see message crated time view when user
/// swipe whole chat.
final bool isEnableSwipeToSeeTime;
@override
State<ChatGroupedListWidget> createState() => _ChatGroupedListWidgetState();
}
class _ChatGroupedListWidgetState extends State<ChatGroupedListWidget>
with TickerProviderStateMixin {
ChatBackgroundConfiguration get chatBackgroundConfig =>
widget.chatBackgroundConfig;
bool get showPopUp => widget.showPopUp;
bool get showTypingIndicator => widget.showTypingIndicator;
bool highlightMessage = false;
final ValueNotifier<String?> _replyId = ValueNotifier(null);
ChatBubbleConfiguration? get chatBubbleConfig => widget.chatBubbleConfig;
ProfileCircleConfiguration? get profileCircleConfig =>
widget.profileCircleConfig;
AnimationController? _animationController;
Animation<Offset>? _slideAnimation;
FeatureActiveConfig? featureActiveConfig;
ChatController? chatController;
bool get isEnableSwipeToSeeTime => widget.isEnableSwipeToSeeTime;
@override
void initState() {
super.initState();
_initializeAnimation();
}
void _initializeAnimation() {
// When this flag is on at that time only animation controllers will be
// initialized.
if (isEnableSwipeToSeeTime) {
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 250),
);
_slideAnimation = Tween<Offset>(
begin: const Offset(0.0, 0.0),
end: const Offset(0.0, 0.0),
).animate(
CurvedAnimation(
curve: Curves.decelerate,
parent: _animationController!,
),
);
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
if (provide != null) {
featureActiveConfig = provide!.featureActiveConfig;
chatController = provide!.chatController;
}
_initializeAnimation();
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
reverse: true,
// When reaction popup is being appeared at that user should not scroll.
physics: showPopUp ? const NeverScrollableScrollPhysics() : null,
padding: EdgeInsets.only(bottom: showTypingIndicator ? 50 : 0),
controller: widget.scrollController,
child: Column(
children: [
GestureDetector(
onHorizontalDragUpdate: (details) => isEnableSwipeToSeeTime
? showPopUp
? null
: _onHorizontalDrag(details)
: null,
onHorizontalDragEnd: (details) => isEnableSwipeToSeeTime
? showPopUp
? null
: _animationController?.reverse()
: null,
onTap: widget.onChatListTap,
child: _animationController != null
? AnimatedBuilder(
animation: _animationController!,
builder: (context, child) {
return _chatStreamBuilder;
},
)
: _chatStreamBuilder,
),
TypingIndicator(
typeIndicatorConfig: widget.typeIndicatorConfig,
chatBubbleConfig: chatBubbleConfig?.inComingChatBubbleConfig,
showIndicator: showTypingIndicator,
profilePic: profileCircleConfig?.profileImageUrl,
),
SizedBox(
height: MediaQuery.of(context).size.width *
(widget.replyMessage.message.isNotEmpty ? 0.3 : 0.14),
),
],
),
);
}
Future<void> _onReplyTap(String id, List<Message>? messages) async {
// Finds the replied message if exists
final repliedMessages = messages?.firstWhere((message) => id == message.id);
// Scrolls to replied message and highlights
if (repliedMessages != null && repliedMessages.key.currentState != null) {
await Scrollable.ensureVisible(
repliedMessages.key.currentState!.context,
// This value will make widget to be in center when auto scrolled.
alignment: 0.5,
curve: widget.repliedMessageConfig?.repliedMsgAutoScrollConfig
.highlightScrollCurve ??
Curves.easeIn,
duration: widget.repliedMessageConfig?.repliedMsgAutoScrollConfig
.highlightDuration ??
const Duration(milliseconds: 300),
);
if (widget.repliedMessageConfig?.repliedMsgAutoScrollConfig
.enableHighlightRepliedMsg ??
false) {
_replyId.value = id;
Future.delayed(
widget.repliedMessageConfig?.repliedMsgAutoScrollConfig
.highlightDuration ??
const Duration(milliseconds: 300),
() {
_replyId.value = null;
},
);
}
}
}
/// When user swipe at that time only animation is assigned with value.
void _onHorizontalDrag(DragUpdateDetails details) {
_slideAnimation = Tween<Offset>(
begin: const Offset(0.0, 0.0),
end: const Offset(-0.2, 0.0),
).animate(
CurvedAnimation(
curve: chatBackgroundConfig.messageTimeAnimationCurve,
parent: _animationController!,
),
);
details.delta.dx > 1
? _animationController?.reverse()
: _animationController?.forward();
}
@override
void dispose() {
_animationController?.dispose();
_replyId.dispose();
super.dispose();
}
Widget get _chatStreamBuilder {
return StreamBuilder<List<Message>>(
stream: chatController?.messageStreamController.stream,
builder: (context, snapshot) {
return snapshot.connectionState.isActive
? GroupedListView<Message, String>(
shrinkWrap: true,
elements: snapshot.data!,
groupBy: (element) => element.createdAt.getDateFromDateTime,
itemComparator: (message1, message2) =>
message1.message.compareTo(message2.message),
physics: const NeverScrollableScrollPhysics(),
order: chatBackgroundConfig.groupedListOrder,
sort: chatBackgroundConfig.sortEnable,
groupSeparatorBuilder: (separator) =>
featureActiveConfig?.enableChatSeparator ?? false
? _GroupSeparatorBuilder(
separator: separator,
defaultGroupSeparatorConfig: chatBackgroundConfig
.defaultGroupSeparatorConfig,
groupSeparatorBuilder:
chatBackgroundConfig.groupSeparatorBuilder,
)
: const SizedBox.shrink(),
indexedItemBuilder: (context, message, index) {
return ValueListenableBuilder<String?>(
valueListenable: _replyId,
builder: (context, state, child) {
return ChatBubbleWidget(
key: message.key,
messageTimeTextStyle:
chatBackgroundConfig.messageTimeTextStyle,
messageTimeIconColor:
chatBackgroundConfig.messageTimeIconColor,
message: message,
messageConfig: widget.messageConfig,
chatBubbleConfig: chatBubbleConfig,
profileCircleConfig: profileCircleConfig,
swipeToReplyConfig: widget.swipeToReplyConfig,
repliedMessageConfig: widget.repliedMessageConfig,
slideAnimation: _slideAnimation,
onLongPress: (yCoordinate, xCoordinate) =>
widget.onChatBubbleLongPress(
yCoordinate,
xCoordinate,
message,
),
onSwipe: widget.assignReplyMessage,
shouldHighlight: state == message.id,
onReplyTap: widget
.repliedMessageConfig
?.repliedMsgAutoScrollConfig
.enableScrollToRepliedMsg ??
false
? (replyId) => _onReplyTap(replyId, snapshot.data)
: null,
);
},
);
},
)
: Center(
child: chatBackgroundConfig.loadingWidget ??
const CircularProgressIndicator(),
);
},
);
}
}
class _GroupSeparatorBuilder extends StatelessWidget {
const _GroupSeparatorBuilder({
Key? key,
required this.separator,
this.groupSeparatorBuilder,
this.defaultGroupSeparatorConfig,
}) : super(key: key);
final String separator;
final StringWithReturnWidget? groupSeparatorBuilder;
final DefaultGroupSeparatorConfiguration? defaultGroupSeparatorConfig;
@override
Widget build(BuildContext context) {
return groupSeparatorBuilder != null
? groupSeparatorBuilder!(separator)
: ChatGroupHeader(
day: DateTime.parse(separator),
groupSeparatorConfig: defaultGroupSeparatorConfig,
);
}
}