-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
243 lines (218 loc) · 7.32 KB
/
main.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
import 'package:flutter/material.dart';
import 'package:grouped_infinite_list/grouped_infinite_list.dart';
import 'package:intl/intl.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final dateFormat = DateFormat('dd MMM yyyy HH:mm:ss');
final List<ChatPost> positiveItems = [];
final List<ChatPost> negativeItems = [];
final ScrollController _scrollController = ScrollController();
bool _isPositiveLoading = false;
bool _isNegativeLoading = false;
@override
void initState() {
_addPositiveItem(10);
_addNegativeItem(10);
_scrollController.addListener(_scrollListener);
super.initState();
}
void _scrollListener() {
var thresholdPixels = 120;
var nextPageTrigger =
_scrollController.position.maxScrollExtent - thresholdPixels;
var previousPageTrigger =
_scrollController.position.minScrollExtent + thresholdPixels;
if (_scrollController.position.pixels > nextPageTrigger) {
_addPositiveItemAsync(10);
}
if (_scrollController.position.pixels < previousPageTrigger) {
_addNegativeItemAsync(10);
}
}
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final offset = MediaQuery.of(context).viewPadding.top + kToolbarHeight;
return Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
backgroundColor:
Theme.of(context).colorScheme.primary.withOpacity(0.85),
title: const Text('Grouped Infinite List'),
),
body: GroupedInfiniteList(
controller: _scrollController,
positiveItems: positiveItems,
negativeItems: negativeItems,
useStickyGroupSeparators: true,
groupBy: (item) => item.dateOnly,
stickyHeaderPositionOffset: offset,
groupSeparatorPadding: const EdgeInsets.symmetric(vertical: 8.0),
groupSeparatorBuilder: (item) {
return Container(
alignment: Alignment.center,
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 4.0,
horizontal: 12.0,
),
decoration: BoxDecoration(
color: Colors.grey.shade100,
borderRadius: BorderRadius.circular(12),
),
child: Text(
DateFormat('dd MMMM yyyy').format(item.date),
style: TextStyle(
color: Colors.grey.shade600,
fontWeight: FontWeight.w600,
),
),
),
);
},
itemComparator: (item1, item2) => item1.date.compareTo(item2.date),
// separator: const Divider(),
suffix: _isPositiveLoading
? const Center(child: CircularProgressIndicator())
: null,
negativeSuffix: _isNegativeLoading
? const Center(child: CircularProgressIndicator())
: null,
itemBuilder: (context, item) {
return ChatBubble(
message: item.message,
isMe: item.isMe,
);
},
),
);
}
Future<void> _addPositiveItemAsync(int count) async {
if (_isPositiveLoading) return;
setState(() => _isPositiveLoading = true);
await Future.delayed(const Duration(milliseconds: 1500));
_addPositiveItem(count);
setState(() => _isPositiveLoading = false);
}
Future<void> _addNegativeItemAsync(int count) async {
if (_isNegativeLoading) return;
setState(() => _isNegativeLoading = true);
await Future.delayed(const Duration(milliseconds: 1500));
_addNegativeItem(count);
setState(() => _isNegativeLoading = false);
}
void _addPositiveItem(int count) {
final int startIndex = positiveItems.length;
final ChatPost? lastItem =
positiveItems.isNotEmpty ? positiveItems.last : null;
final date = lastItem?.date ?? DateTime.now();
positiveItems.addAll(
List<ChatPost>.generate(
count,
(index) => ChatPost(
message:
'Message ${startIndex + index} ${dummyMessageList[index % dummyMessageList.length]}',
date: date.add(Duration(hours: (index + 1) * 2)),
isMe: index % 2 == 0,
),
),
);
}
void _addNegativeItem(int count) {
final int startIndex = negativeItems.length;
final ChatPost? lastItem =
negativeItems.isNotEmpty ? negativeItems.last : null;
final date = lastItem?.date ?? DateTime.now();
negativeItems.addAll(
List<ChatPost>.generate(
count,
(index) => ChatPost(
message:
'Message ${(startIndex + index + 1) * -1} ${dummyMessageList[index % dummyMessageList.length]}',
date: date.subtract(Duration(hours: (index + 1) * 2)),
isMe: index % 2 == 1,
),
),
);
}
}
class ChatPost {
ChatPost({
required this.message,
required this.date,
required this.isMe,
});
final String message;
final DateTime date;
final bool isMe;
DateTime get dateOnly => DateTime(date.year, date.month, date.day);
}
class ChatBubble extends StatelessWidget {
final String message;
final bool isMe;
const ChatBubble({super.key, required this.message, required this.isMe});
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, sizingInformation) {
final maxWidth = sizingInformation.maxWidth;
return Column(
crossAxisAlignment:
isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: [
Container(
constraints: BoxConstraints(maxWidth: maxWidth * 0.8),
decoration: BoxDecoration(
color: isMe ? Colors.blue : Colors.grey.shade300,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(12),
topRight: const Radius.circular(12),
bottomLeft:
isMe ? const Radius.circular(12) : const Radius.circular(0),
bottomRight:
isMe ? const Radius.circular(0) : const Radius.circular(12),
),
),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
child: Text(
message,
style: TextStyle(
color: isMe ? Colors.white : Colors.black,
),
),
),
],
);
});
}
}
List<String> dummyMessageList = [
'Lorem ipsum',
'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
];