@@ -15,7 +15,8 @@ class Chat extends StatelessWidget {
15
15
final String peerId;
16
16
final String peerAvatar;
17
17
18
- Chat ({Key key, @required this .peerId, @required this .peerAvatar}) : super (key: key);
18
+ Chat ({Key key, @required this .peerId, @required this .peerAvatar})
19
+ : super (key: key);
19
20
20
21
@override
21
22
Widget build (BuildContext context) {
@@ -39,10 +40,12 @@ class ChatScreen extends StatefulWidget {
39
40
final String peerId;
40
41
final String peerAvatar;
41
42
42
- ChatScreen ({Key key, @required this .peerId, @required this .peerAvatar}) : super (key: key);
43
+ ChatScreen ({Key key, @required this .peerId, @required this .peerAvatar})
44
+ : super (key: key);
43
45
44
46
@override
45
- State createState () => new ChatScreenState (peerId: peerId, peerAvatar: peerAvatar);
47
+ State createState () =>
48
+ new ChatScreenState (peerId: peerId, peerAvatar: peerAvatar);
46
49
}
47
50
48
51
class ChatScreenState extends State <ChatScreen > {
@@ -61,7 +64,8 @@ class ChatScreenState extends State<ChatScreen> {
61
64
bool isShowSticker;
62
65
String imageUrl;
63
66
64
- final TextEditingController textEditingController = new TextEditingController ();
67
+ final TextEditingController textEditingController =
68
+ new TextEditingController ();
65
69
final ScrollController listScrollController = new ScrollController ();
66
70
final FocusNode focusNode = new FocusNode ();
67
71
@@ -123,16 +127,15 @@ class ChatScreenState extends State<ChatScreen> {
123
127
Future uploadFile () async {
124
128
String fileName = DateTime .now ().millisecondsSinceEpoch.toString ();
125
129
StorageReference reference = FirebaseStorage .instance.ref ().child (fileName);
126
- StorageUploadTask uploadTask = reference.putFile (imageFile);
127
-
128
- Uri downloadUrl = (await uploadTask.future).downloadUrl;
129
- imageUrl = downloadUrl.toString ();
130
+ reference.putFile (imageFile);
131
+ reference.getDownloadURL ().then ((dynamic value) {
132
+ imageUrl = value.toString ();
133
+ setState (() {
134
+ isLoading = false ;
135
+ });
130
136
131
- setState (() {
132
- isLoading = false ;
137
+ onSendMessage (imageUrl, 1 );
133
138
});
134
-
135
- onSendMessage (imageUrl, 1 );
136
139
}
137
140
138
141
void onSendMessage (String content, int type) {
@@ -158,7 +161,8 @@ class ChatScreenState extends State<ChatScreen> {
158
161
},
159
162
);
160
163
});
161
- listScrollController.animateTo (0.0 , duration: Duration (milliseconds: 300 ), curve: Curves .easeOut);
164
+ listScrollController.animateTo (0.0 ,
165
+ duration: Duration (milliseconds: 300 ), curve: Curves .easeOut);
162
166
} else {
163
167
Fluttertoast .showToast (msg: 'Nothing to send' );
164
168
}
@@ -178,8 +182,12 @@ class ChatScreenState extends State<ChatScreen> {
178
182
),
179
183
padding: EdgeInsets .fromLTRB (15.0 , 10.0 , 15.0 , 10.0 ),
180
184
width: 200.0 ,
181
- decoration: BoxDecoration (color: greyColor2, borderRadius: BorderRadius .circular (8.0 )),
182
- margin: EdgeInsets .only (bottom: isLastMessageRight (index) ? 20.0 : 10.0 , right: 10.0 ),
185
+ decoration: BoxDecoration (
186
+ color: greyColor2,
187
+ borderRadius: BorderRadius .circular (8.0 )),
188
+ margin: EdgeInsets .only (
189
+ bottom: isLastMessageRight (index) ? 20.0 : 10.0 ,
190
+ right: 10.0 ),
183
191
)
184
192
: document['type' ] == 1
185
193
// Image
@@ -188,7 +196,8 @@ class ChatScreenState extends State<ChatScreen> {
188
196
child: CachedNetworkImage (
189
197
placeholder: Container (
190
198
child: CircularProgressIndicator (
191
- valueColor: AlwaysStoppedAnimation <Color >(themeColor),
199
+ valueColor:
200
+ AlwaysStoppedAnimation <Color >(themeColor),
192
201
),
193
202
width: 200.0 ,
194
203
height: 200.0 ,
@@ -210,15 +219,19 @@ class ChatScreenState extends State<ChatScreen> {
210
219
borderRadius: BorderRadius .all (
211
220
Radius .circular (8.0 ),
212
221
),
222
+ clipBehavior: Clip .hardEdge,
213
223
),
214
224
imageUrl: document['content' ],
215
225
width: 200.0 ,
216
226
height: 200.0 ,
217
227
fit: BoxFit .cover,
218
228
),
219
229
borderRadius: BorderRadius .all (Radius .circular (8.0 )),
230
+ clipBehavior: Clip .hardEdge,
220
231
),
221
- margin: EdgeInsets .only (bottom: isLastMessageRight (index) ? 20.0 : 10.0 , right: 10.0 ),
232
+ margin: EdgeInsets .only (
233
+ bottom: isLastMessageRight (index) ? 20.0 : 10.0 ,
234
+ right: 10.0 ),
222
235
)
223
236
// Sticker
224
237
: Container (
@@ -228,7 +241,9 @@ class ChatScreenState extends State<ChatScreen> {
228
241
height: 100.0 ,
229
242
fit: BoxFit .cover,
230
243
),
231
- margin: EdgeInsets .only (bottom: isLastMessageRight (index) ? 20.0 : 10.0 , right: 10.0 ),
244
+ margin: EdgeInsets .only (
245
+ bottom: isLastMessageRight (index) ? 20.0 : 10.0 ,
246
+ right: 10.0 ),
232
247
),
233
248
],
234
249
mainAxisAlignment: MainAxisAlignment .end,
@@ -246,7 +261,8 @@ class ChatScreenState extends State<ChatScreen> {
246
261
placeholder: Container (
247
262
child: CircularProgressIndicator (
248
263
strokeWidth: 1.0 ,
249
- valueColor: AlwaysStoppedAnimation <Color >(themeColor),
264
+ valueColor:
265
+ AlwaysStoppedAnimation <Color >(themeColor),
250
266
),
251
267
width: 35.0 ,
252
268
height: 35.0 ,
@@ -260,6 +276,7 @@ class ChatScreenState extends State<ChatScreen> {
260
276
borderRadius: BorderRadius .all (
261
277
Radius .circular (18.0 ),
262
278
),
279
+ clipBehavior: Clip .hardEdge,
263
280
)
264
281
: Container (width: 35.0 ),
265
282
document['type' ] == 0
@@ -270,7 +287,9 @@ class ChatScreenState extends State<ChatScreen> {
270
287
),
271
288
padding: EdgeInsets .fromLTRB (15.0 , 10.0 , 15.0 , 10.0 ),
272
289
width: 200.0 ,
273
- decoration: BoxDecoration (color: primaryColor, borderRadius: BorderRadius .circular (8.0 )),
290
+ decoration: BoxDecoration (
291
+ color: primaryColor,
292
+ borderRadius: BorderRadius .circular (8.0 )),
274
293
margin: EdgeInsets .only (left: 10.0 ),
275
294
)
276
295
: document['type' ] == 1
@@ -279,7 +298,8 @@ class ChatScreenState extends State<ChatScreen> {
279
298
child: CachedNetworkImage (
280
299
placeholder: Container (
281
300
child: CircularProgressIndicator (
282
- valueColor: AlwaysStoppedAnimation <Color >(themeColor),
301
+ valueColor: AlwaysStoppedAnimation <Color >(
302
+ themeColor),
283
303
),
284
304
width: 200.0 ,
285
305
height: 200.0 ,
@@ -301,13 +321,16 @@ class ChatScreenState extends State<ChatScreen> {
301
321
borderRadius: BorderRadius .all (
302
322
Radius .circular (8.0 ),
303
323
),
324
+ clipBehavior: Clip .hardEdge,
304
325
),
305
326
imageUrl: document['content' ],
306
327
width: 200.0 ,
307
328
height: 200.0 ,
308
329
fit: BoxFit .cover,
309
330
),
310
- borderRadius: BorderRadius .all (Radius .circular (8.0 )),
331
+ borderRadius:
332
+ BorderRadius .all (Radius .circular (8.0 )),
333
+ clipBehavior: Clip .hardEdge,
311
334
),
312
335
margin: EdgeInsets .only (left: 10.0 ),
313
336
)
@@ -318,7 +341,9 @@ class ChatScreenState extends State<ChatScreen> {
318
341
height: 100.0 ,
319
342
fit: BoxFit .cover,
320
343
),
321
- margin: EdgeInsets .only (bottom: isLastMessageRight (index) ? 20.0 : 10.0 , right: 10.0 ),
344
+ margin: EdgeInsets .only (
345
+ bottom: isLastMessageRight (index) ? 20.0 : 10.0 ,
346
+ right: 10.0 ),
322
347
),
323
348
],
324
349
),
@@ -327,9 +352,13 @@ class ChatScreenState extends State<ChatScreen> {
327
352
isLastMessageLeft (index)
328
353
? Container (
329
354
child: Text (
330
- DateFormat ('dd MMM kk:mm' )
331
- .format (DateTime .fromMillisecondsSinceEpoch (int .parse (document['timestamp' ]))),
332
- style: TextStyle (color: greyColor, fontSize: 12.0 , fontStyle: FontStyle .italic),
355
+ DateFormat ('dd MMM kk:mm' ).format (
356
+ DateTime .fromMillisecondsSinceEpoch (
357
+ int .parse (document['timestamp' ]))),
358
+ style: TextStyle (
359
+ color: greyColor,
360
+ fontSize: 12.0 ,
361
+ fontStyle: FontStyle .italic),
333
362
),
334
363
margin: EdgeInsets .only (left: 50.0 , top: 5.0 , bottom: 5.0 ),
335
364
)
@@ -343,15 +372,21 @@ class ChatScreenState extends State<ChatScreen> {
343
372
}
344
373
345
374
bool isLastMessageLeft (int index) {
346
- if ((index > 0 && listMessage != null && listMessage[index - 1 ]['idFrom' ] == id) || index == 0 ) {
375
+ if ((index > 0 &&
376
+ listMessage != null &&
377
+ listMessage[index - 1 ]['idFrom' ] == id) ||
378
+ index == 0 ) {
347
379
return true ;
348
380
} else {
349
381
return false ;
350
382
}
351
383
}
352
384
353
385
bool isLastMessageRight (int index) {
354
- if ((index > 0 && listMessage != null && listMessage[index - 1 ]['idFrom' ] != id) || index == 0 ) {
386
+ if ((index > 0 &&
387
+ listMessage != null &&
388
+ listMessage[index - 1 ]['idFrom' ] != id) ||
389
+ index == 0 ) {
355
390
return true ;
356
391
} else {
357
392
return false ;
@@ -500,7 +535,9 @@ class ChatScreenState extends State<ChatScreen> {
500
535
mainAxisAlignment: MainAxisAlignment .spaceEvenly,
501
536
),
502
537
decoration: new BoxDecoration (
503
- border: new Border (top: new BorderSide (color: greyColor2, width: 0.5 )), color: Colors .white),
538
+ border:
539
+ new Border (top: new BorderSide (color: greyColor2, width: 0.5 )),
540
+ color: Colors .white),
504
541
padding: EdgeInsets .all (5.0 ),
505
542
height: 180.0 ,
506
543
);
@@ -511,7 +548,8 @@ class ChatScreenState extends State<ChatScreen> {
511
548
child: isLoading
512
549
? Container (
513
550
child: Center (
514
- child: CircularProgressIndicator (valueColor: AlwaysStoppedAnimation <Color >(themeColor)),
551
+ child: CircularProgressIndicator (
552
+ valueColor: AlwaysStoppedAnimation <Color >(themeColor)),
515
553
),
516
554
color: Colors .white.withOpacity (0.8 ),
517
555
)
@@ -579,14 +617,18 @@ class ChatScreenState extends State<ChatScreen> {
579
617
width: double .infinity,
580
618
height: 50.0 ,
581
619
decoration: new BoxDecoration (
582
- border: new Border (top: new BorderSide (color: greyColor2, width: 0.5 )), color: Colors .white),
620
+ border:
621
+ new Border (top: new BorderSide (color: greyColor2, width: 0.5 )),
622
+ color: Colors .white),
583
623
);
584
624
}
585
625
586
626
Widget buildListMessage () {
587
627
return Flexible (
588
628
child: groupChatId == ''
589
- ? Center (child: CircularProgressIndicator (valueColor: AlwaysStoppedAnimation <Color >(themeColor)))
629
+ ? Center (
630
+ child: CircularProgressIndicator (
631
+ valueColor: AlwaysStoppedAnimation <Color >(themeColor)))
590
632
: StreamBuilder (
591
633
stream: Firestore .instance
592
634
.collection ('messages' )
@@ -598,12 +640,15 @@ class ChatScreenState extends State<ChatScreen> {
598
640
builder: (context, snapshot) {
599
641
if (! snapshot.hasData) {
600
642
return Center (
601
- child: CircularProgressIndicator (valueColor: AlwaysStoppedAnimation <Color >(themeColor)));
643
+ child: CircularProgressIndicator (
644
+ valueColor:
645
+ AlwaysStoppedAnimation <Color >(themeColor)));
602
646
} else {
603
647
listMessage = snapshot.data.documents;
604
648
return ListView .builder (
605
649
padding: EdgeInsets .all (10.0 ),
606
- itemBuilder: (context, index) => buildItem (index, snapshot.data.documents[index]),
650
+ itemBuilder: (context, index) =>
651
+ buildItem (index, snapshot.data.documents[index]),
607
652
itemCount: snapshot.data.documents.length,
608
653
reverse: true ,
609
654
controller: listScrollController,
0 commit comments