-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatRoom.dart
415 lines (384 loc) · 12.4 KB
/
chatRoom.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:rescue_code/style/chatRoomItems.dart';
import 'package:shared_preferences/shared_preferences.dart';
class ChatRoom extends StatefulWidget {
final String chatId;
final userUID;
final String name;
final type;
final firebaseToken;
ChatRoom(
{@required this.chatId,
@required this.userUID,
@required this.name,
this.type = 'user',
this.firebaseToken});
@override
_ChatRoomState createState() => _ChatRoomState();
}
class _ChatRoomState extends State<ChatRoom> {
final String serverToken = 'AIzaSyAiPEZR_LRpHZqvBmPHNdtHK0RWQw_IRgY';
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
Timer timer;
List messages;
bool _isComposingMessage = false;
TextEditingController messageController = TextEditingController();
final textController = TextEditingController();
var lat;
var lng;
_alertDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return BeautifulAlertDialog();
});
}
checkNsend() {
String msg;
msg = messageController.text.trim();
// Checking TextField.
if (msg.isEmpty) {
print('✖ Message wasn\'t sent ✖');
scrollToEnd();
_alertDialog(context);
} else if (msg.isNotEmpty && msg != "") {
print("Message was sent ✅");
scrollToEnd();
sendMessage(msg);
_isComposingMessage = false;
}
}
var profileImage;
getImage() async {
try {
StorageReference reference =
FirebaseStorage.instance.ref().child("images/${widget.userUID}.png");
var url = await reference.getDownloadURL();
setState(() {
profileImage = url;
});
} catch (e) {
print(e);
}
}
Future<Map<String, dynamic>> sendAndRetrieveMessage(
String title, String body) async {
await firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true),
);
await post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{'body': body, 'title': title},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': widget.firebaseToken,
},
),
).then((res) {
print(widget.firebaseToken);
print(res.body);
});
final Completer<Map<String, dynamic>> completer =
Completer<Map<String, dynamic>>();
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
completer.complete(message);
},
);
messageController.clear();
return completer.future;
}
Future<bool> sendMessage(String message) async {
// if (message.trim() != '') {
try {
SharedPreferences _prefs = await SharedPreferences.getInstance();
DocumentReference authorRef =
Firestore.instance.collection("users").document(widget.userUID);
DocumentReference chatroomRef =
Firestore.instance.collection("chats").document(widget.chatId);
Map<String, dynamic> serializedMessage = {
"name": _prefs.getString('name'),
"author": authorRef,
"timestamp": DateTime.now(),
"value": message,
"lat": "",
"lng": ""
};
await chatroomRef.updateData({
"messages": FieldValue.arrayUnion([serializedMessage])
});
sendAndRetrieveMessage(_prefs.getString('name'), message);
return true;
} catch (e) {
print('Error: $e');
return false;
}
}
Future<bool> sendLocation(String lat, String lng) async {
try {
SharedPreferences _prefs = await SharedPreferences.getInstance();
DocumentReference authorRef =
Firestore.instance.collection("users").document(widget.userUID);
DocumentReference chatroomRef =
Firestore.instance.collection("chats").document(widget.chatId);
Map<String, dynamic> serializedMessage = {
"name": _prefs.getString('name'),
"author": authorRef,
"timestamp": DateTime.now(),
"value": '',
"lat": lat.toString(),
"lng": lng.toString()
};
await chatroomRef.updateData({
"messages": FieldValue.arrayUnion([serializedMessage])
});
messageController.clear();
return true;
} catch (e) {
print('Error: $e');
return false;
}
}
void getMessages() async {
var res = await Firestore.instance
.collection("chats")
.document(widget.chatId)
.get();
setState(() {
messages = res.data['messages'];
});
}
ScrollController _scrollController = ScrollController();
scrollToEnd() {
_scrollController.animateTo(_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 500), curve: Curves.easeOut);
}
@override
void initState() {
super.initState();
Timer.periodic(Duration(seconds: 1), (t) {
setState(() {
timer = t;
});
getMessages();
});
getImage();
}
@override
void dispose() {
timer?.cancel();
super.dispose();
}
Future getLocation() async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
setState(() {
lat = position.latitude.toString();
lng = position.longitude.toString();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.name),
backgroundColor: Colors.red,
),
body: Container(
child: new Column(
children: <Widget>[
new Flexible(
child: ListView.builder(
controller: _scrollController,
itemCount: messages == null ? 0 : messages.length,
itemBuilder: (context, i) {
return ChatMessageListItem(
author: messages[i]['author'],
userUID: Firestore.instance
.collection("users")
.document(widget.userUID),
name: messages[i]['name'],
value: messages[i]['value'],
profileImage: profileImage,
lat: messages[i]['lat'] ?? '',
lng: messages[i]['lng'] ?? '',
type: 'user');
},
)),
new Divider(height: 1.0),
new Container(
decoration:
new BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
new Builder(builder: (BuildContext context) {
return new Container(width: 0.0, height: 0.0);
})
],
),
decoration: Theme.of(context).platform == TargetPlatform.iOS
? new BoxDecoration(
border: new Border(
top: new BorderSide(
color: Colors.grey[200],
)))
: null,
));
}
IconButton getDefaultSendButton() {
return new IconButton(
icon: new Icon(Icons.send),
onPressed: _isComposingMessage ? () => checkNsend() : null,
);
}
IconButton getLocationSendButton() {
return new IconButton(
icon: new Icon(Icons.add_location),
onPressed: () async {
await getLocation();
await sendLocation(lat, lng);
},
);
}
Widget _buildTextComposer() {
return new IconTheme(
data: new IconThemeData(
color: _isComposingMessage
? Colors.red
: Theme.of(context).disabledColor,
),
child: new Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: new Row(
children: <Widget>[
new Flexible(
child: new TextField(
controller: messageController,
onChanged: (String messageText) {
setState(() {
_isComposingMessage = messageText.length > 0;
});
},
onSubmitted: (message) => checkNsend(),
decoration:
new InputDecoration.collapsed(hintText: "Send a message"),
),
),
new Container(
margin: const EdgeInsets.symmetric(horizontal: 4.0),
child: getDefaultSendButton(),
),
new Container(
margin: const EdgeInsets.symmetric(horizontal: 4.0),
child: getLocationSendButton(),
),
],
),
));
}
}
class BeautifulAlertDialog extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Dialog(
elevation: 0,
backgroundColor: Colors.transparent,
child: Container(
padding: EdgeInsets.only(right: 16.0),
height: 150,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(75),
bottomLeft: Radius.circular(75),
topRight: Radius.circular(10),
bottomRight: Radius.circular(10))),
child: Row(
children: <Widget>[
SizedBox(width: 20.0),
CircleAvatar(
radius: 55,
backgroundColor: Colors.grey.shade200,
child: Center(
child: Icon(
Icons.error,
color: Colors.red,
size: 110,
)),
),
SizedBox(width: 20.0),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Wasn't sent!",
style: TextStyle(fontSize: 20),
),
SizedBox(height: 10.0),
Flexible(
child: Text(
"- Message Can't Be Empty",
style: TextStyle(fontSize: 14),
),
),
SizedBox(height: 10.0),
Row(
children: <Widget>[
// Expanded(
// child: RaisedButton(
// child: Text("No"),
// color: Colors.red,
// colorBrightness: Brightness.dark,
// onPressed: (){Navigator.pop(context);},
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
// ),
// ),
SizedBox(width: 10.0),
Expanded(
child: RaisedButton(
child: Text(
"Ok",
style: TextStyle(fontSize: 20),
),
color: Colors.green,
colorBrightness: Brightness.dark,
onPressed: () {
Navigator.pop(context);
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
),
),
],
)
],
),
)
],
),
),
),
);
}
}