1
1
part of dash_chat;
2
2
3
- class MessageListView extends StatelessWidget {
3
+ class MessageListView extends StatefulWidget {
4
4
final List <ChatMessage > messages;
5
5
final ChatUser user;
6
6
final bool showuserAvatar;
@@ -31,8 +31,6 @@ class MessageListView extends StatelessWidget {
31
31
final Function onLoadEarlier;
32
32
final Function (bool ) defaultLoadCallback;
33
33
34
- double previousPixelPostion = 0.0 ;
35
-
36
34
MessageListView ({
37
35
this .showLoadEarlierWidget,
38
36
this .shouldShowLoadEarlier,
@@ -66,17 +64,24 @@ class MessageListView extends StatelessWidget {
66
64
this .showLoadMore,
67
65
});
68
66
67
+ @override
68
+ _MessageListViewState createState () => _MessageListViewState ();
69
+ }
70
+
71
+ class _MessageListViewState extends State <MessageListView > {
72
+ double previousPixelPostion = 0.0 ;
73
+
69
74
bool scrollNotificationFunc (ScrollNotification scrollNotification) {
70
75
if (previousPixelPostion == 0.0 ) {
71
76
previousPixelPostion = scrollNotification.metrics.maxScrollExtent;
72
77
}
73
78
74
79
if (scrollNotification.metrics.pixels ==
75
80
scrollNotification.metrics.maxScrollExtent) {
76
- changeVisible (false );
81
+ widget. changeVisible (false );
77
82
} else {
78
83
if (previousPixelPostion < scrollNotification.metrics.pixels) {
79
- changeVisible (true );
84
+ widget. changeVisible (true );
80
85
}
81
86
82
87
previousPixelPostion = scrollNotification.metrics.pixels;
@@ -93,36 +98,37 @@ class MessageListView extends StatelessWidget {
93
98
behavior: CustomScrollBehaviour (),
94
99
child: Flexible (
95
100
child: Padding (
96
- padding: messageContainerPadding,
101
+ padding: widget. messageContainerPadding,
97
102
child: NotificationListener <ScrollNotification >(
98
103
onNotification: scrollNotificationFunc,
99
104
child: Stack (
100
105
alignment: AlignmentDirectional .topCenter,
101
106
children: [
102
107
ListView .builder (
103
- controller: scrollController,
108
+ controller: widget. scrollController,
104
109
shrinkWrap: true ,
105
- reverse: inverted,
106
- itemCount: messages.length,
110
+ reverse: widget. inverted,
111
+ itemCount: widget. messages.length,
107
112
itemBuilder: (context, i) {
108
113
final j = i + 1 ;
109
114
bool showAvatar = false ;
110
115
bool showDate;
111
- if (j < messages.length) {
112
- showAvatar = messages[j].user.uid != messages[i].user.uid;
116
+ if (j < widget.messages.length) {
117
+ showAvatar = widget.messages[j].user.uid !=
118
+ widget.messages[i].user.uid;
113
119
} else {
114
120
showAvatar = true ;
115
121
}
116
122
117
123
if (currentDate == null ) {
118
- currentDate = messages[i].createdAt;
124
+ currentDate = widget. messages[i].createdAt;
119
125
showDate = true ;
120
126
} else if (currentDate
121
- .difference (messages[i].createdAt)
127
+ .difference (widget. messages[i].createdAt)
122
128
.inDays !=
123
129
0 ) {
124
130
showDate = true ;
125
- currentDate = messages[i].createdAt;
131
+ currentDate = widget. messages[i].createdAt;
126
132
} else {
127
133
showDate = false ;
128
134
}
@@ -131,11 +137,12 @@ class MessageListView extends StatelessWidget {
131
137
child: Column (
132
138
children: < Widget > [
133
139
if (showDate)
134
- if (dateBuilder != null )
135
- dateBuilder (dateBuilder != null
136
- ? dateFormat.format (messages[i].createdAt)
140
+ if (widget.dateBuilder != null )
141
+ widget.dateBuilder (widget.dateBuilder != null
142
+ ? widget.dateFormat
143
+ .format (widget.messages[i].createdAt)
137
144
: DateFormat ('yyyy-MM-dd' )
138
- .format (messages[i].createdAt))
145
+ .format (widget. messages[i].createdAt))
139
146
else
140
147
Container (
141
148
decoration: BoxDecoration (
@@ -149,41 +156,45 @@ class MessageListView extends StatelessWidget {
149
156
),
150
157
margin: EdgeInsets .symmetric (vertical: 10.0 ),
151
158
child: Text (
152
- dateBuilder != null
153
- ? dateFormat.format (messages[i].createdAt)
159
+ widget.dateBuilder != null
160
+ ? widget.dateFormat
161
+ .format (widget.messages[i].createdAt)
154
162
: DateFormat ('MMM dd' )
155
- .format (messages[i].createdAt),
163
+ .format (widget. messages[i].createdAt),
156
164
style: TextStyle (
157
165
color: Colors .white,
158
166
fontSize: 12.0 ,
159
167
),
160
168
),
161
169
),
162
170
Row (
163
- mainAxisAlignment: messages[i].user.uid == user.uid
164
- ? MainAxisAlignment .end
165
- : MainAxisAlignment .start,
171
+ mainAxisAlignment:
172
+ widget.messages[i].user.uid == widget.user.uid
173
+ ? MainAxisAlignment .end
174
+ : MainAxisAlignment .start,
166
175
crossAxisAlignment: CrossAxisAlignment .center,
167
176
children: < Widget > [
168
177
Padding (
169
178
padding: EdgeInsets .symmetric (horizontal: 10.0 ),
170
- child:
171
- ((showAvatarForEverMessage || showAvatar) &&
172
- messages[i].user.uid != user.uid)
173
- ? AvatarContainer (
174
- user: messages[i].user,
175
- onPress: onPressAvatar,
176
- onLongPress: onLongPressAvatar,
177
- avatarBuilder: avatarBuilder,
178
- )
179
- : SizedBox (
180
- width: 40.0 ,
181
- ),
179
+ child: ((widget.showAvatarForEverMessage ||
180
+ showAvatar) &&
181
+ widget.messages[i].user.uid !=
182
+ widget.user.uid)
183
+ ? AvatarContainer (
184
+ user: widget.messages[i].user,
185
+ onPress: widget.onPressAvatar,
186
+ onLongPress: widget.onLongPressAvatar,
187
+ avatarBuilder: widget.avatarBuilder,
188
+ )
189
+ : SizedBox (
190
+ width: 40.0 ,
191
+ ),
182
192
),
183
193
GestureDetector (
184
194
onLongPress: () {
185
- if (onLongPressMessage != null ) {
186
- onLongPressMessage (messages[i]);
195
+ if (widget.onLongPressMessage != null ) {
196
+ widget
197
+ .onLongPressMessage (widget.messages[i]);
187
198
} else {
188
199
showBottomSheet (
189
200
context: context,
@@ -199,7 +210,8 @@ class MessageListView extends StatelessWidget {
199
210
onTap: () {
200
211
Clipboard .setData (
201
212
ClipboardData (
202
- text: messages[i]
213
+ text: widget
214
+ .messages[i]
203
215
.text));
204
216
Navigator .pop (context);
205
217
},
@@ -209,34 +221,37 @@ class MessageListView extends StatelessWidget {
209
221
));
210
222
}
211
223
},
212
- child: messageBuilder != null
213
- ? messageBuilder (messages[i])
224
+ child: widget. messageBuilder != null
225
+ ? widget. messageBuilder (widget. messages[i])
214
226
: MessageContainer (
215
- isUser:
216
- messages[i].user.uid == user.uid,
217
- message: messages[i],
218
- timeFormat: timeFormat,
227
+ isUser: widget.messages[i].user.uid ==
228
+ widget. user.uid,
229
+ message: widget. messages[i],
230
+ timeFormat: widget. timeFormat,
219
231
messageImageBuilder:
220
- messageImageBuilder,
221
- messageTextBuilder: messageTextBuilder,
222
- messageTimeBuilder: messageTimeBuilder,
232
+ widget.messageImageBuilder,
233
+ messageTextBuilder:
234
+ widget.messageTextBuilder,
235
+ messageTimeBuilder:
236
+ widget.messageTimeBuilder,
223
237
messageContainerDecoration:
224
- messageContainerDecoration,
225
- parsePatterns: parsePatterns,
238
+ widget. messageContainerDecoration,
239
+ parsePatterns: widget. parsePatterns,
226
240
),
227
241
),
228
- if (showuserAvatar)
242
+ if (widget. showuserAvatar)
229
243
Padding (
230
244
padding:
231
245
EdgeInsets .symmetric (horizontal: 10.0 ),
232
- child: ((showAvatarForEverMessage ||
246
+ child: ((widget. showAvatarForEverMessage ||
233
247
showAvatar) &&
234
- messages[i].user.uid == user.uid)
248
+ widget.messages[i].user.uid ==
249
+ widget.user.uid)
235
250
? AvatarContainer (
236
- user: messages[i].user,
237
- onPress: onPressAvatar,
238
- onLongPress: onLongPressAvatar,
239
- avatarBuilder: avatarBuilder,
251
+ user: widget. messages[i].user,
252
+ onPress: widget. onPressAvatar,
253
+ onLongPress: widget. onLongPressAvatar,
254
+ avatarBuilder: widget. avatarBuilder,
240
255
)
241
256
: SizedBox (
242
257
width: 40.0 ,
@@ -254,13 +269,13 @@ class MessageListView extends StatelessWidget {
254
269
},
255
270
),
256
271
AnimatedPositioned (
257
- top: showLoadMore ? 8.0 : - 50.0 ,
272
+ top: widget. showLoadMore ? 8.0 : - 50.0 ,
258
273
duration: Duration (milliseconds: 200 ),
259
- child: showLoadEarlierWidget != null
260
- ? showLoadEarlierWidget ()
274
+ child: widget. showLoadEarlierWidget != null
275
+ ? widget. showLoadEarlierWidget ()
261
276
: LoadEarlierWidget (
262
- onLoadEarlier: onLoadEarlier,
263
- defaultLoadCallback: defaultLoadCallback,
277
+ onLoadEarlier: widget. onLoadEarlier,
278
+ defaultLoadCallback: widget. defaultLoadCallback,
264
279
),
265
280
),
266
281
],
0 commit comments