-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_screen.dart
353 lines (302 loc) · 9.26 KB
/
chat_screen.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
import 'package:flutter/material.dart';
import 'package:flash_chat/constants.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final FirebaseAuth _auth = FirebaseAuth.instance;
final _firestore = FirebaseFirestore.instance;
User loggedInUser;
class ChatScreen extends StatefulWidget {
static const String id = 'chat_screen';
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final messageTextController = TextEditingController();
String messageText;
@override
void initState() {
// TODO: implement initState
super.initState();
getCurrentUser();
}
void getCurrentUser() async {
try {
final User user = _auth.currentUser;
if(user!=null) {
loggedInUser = user;
print(loggedInUser.email);
}
}
catch(e) {
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: null,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
_auth.signOut();
Navigator.pop(context);
}),
],
title: Text('⚡️Chat'),
backgroundColor: Colors.lightBlueAccent,
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
MessagesStream(),
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
controller: messageTextController,
onChanged: (value) {
messageText=value;
},
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
onPressed: () {
messageTextController.clear();
_firestore.collection('messages').add({
'text': messageText,
'sender': loggedInUser.email,
'timestamp': FieldValue.serverTimestamp(),
});
},
child: Text(
'Send',
style: kSendButtonTextStyle,
),
),
],
),
),
],
),
),
);
}
}
class MessagesStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('messages').orderBy('timestamp', descending: false).snapshots(),
builder: (context,snapshot) {
if(!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
final messages = snapshot.data.docs.reversed;
List<MessageBubble> messageBubbles=[];
for(var message in messages) {
final messageText = message.data()['text'];
final messageSender = message.data()['sender'];
final currentUser = loggedInUser.email;
final messageBubble = MessageBubble(
sender: messageSender,
text: messageText,
isMe: currentUser==messageSender,
);
messageBubbles.add(messageBubble);
}
return Expanded(
child: ListView(
reverse: true,
padding: EdgeInsets.symmetric(horizontal: 10.0,vertical: 20.0),
children: messageBubbles,
),
);
},
);
}
}
class MessageBubble extends StatelessWidget {
MessageBubble({this.sender,this.text,this.isMe});
final String sender;
final String text;
final bool isMe;
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: isMe?CrossAxisAlignment.end:CrossAxisAlignment.start,
children: [
Text(sender,style: TextStyle(
fontSize: 11.0,
color: Colors.black54,
),),
Material(
borderRadius: isMe? BorderRadius.only(topLeft: Radius.circular(30.0),bottomLeft: Radius.circular(30.0),bottomRight: Radius.circular(30.0)):BorderRadius.only(topRight: Radius.circular(30.0),bottomLeft: Radius.circular(30.0),bottomRight: Radius.circular(30.0)),
elevation: 5.0,
color: isMe ? Colors.blue:Colors.lightGreen,
child: Padding(
padding: EdgeInsets.symmetric(vertical: 10.0,horizontal: 20.0),
child: Text(
text,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
),
),
),
),
],
),
);
}
}
//import 'package:flutter/material.dart';
//import 'package:flash_chat/constants.dart';
//import 'package:firebase_auth/firebase_auth.dart';
//import 'package:cloud_firestore/cloud_firestore.dart';
//
//
//class ChatScreen extends StatefulWidget {
// static String id = 'chat_screen';
// @override
// _ChatScreenState createState() => _ChatScreenState();
//}
//
//class _ChatScreenState extends State<ChatScreen> {
// final _firestore = FirebaseFirestore.instance;
// final FirebaseAuth _auth = FirebaseAuth.instance;
// User loggedInUser;
// String messageText;
//
//
//
// @override
// void initState() {
// // TODO: implement initState
// super.initState();
// getCurrentUser();
// }
// void getCurrentUser() async{
// try{
// final user = await _auth.currentUser;
// if(user != null){
// loggedInUser = user;
// print(loggedInUser.email);
// }}
// catch(e){
// print(e);
//
// }
// }
//
//
//// void getMessages() async{
//// final messages = await _firestore.collection('messages').getDocuments();
//// for (var message in messages.){
//// print(message.data);
//// }
//// }
//
// void messagesStream()async{
// await for (var snapshot in _firestore.collection('messages').snapshots()){
// for (var message in snapshot.docs){
// print(message.data);
// }
// }
// }
//
//
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(
// leading: null,
// actions: <Widget>[
// IconButton(
// icon: Icon(Icons.close),
// onPressed: () {
// messagesStream();
//// _auth.signOut();
//// Navigator.pop(context);
// //Implement logout functionality
// }),
// ],
// title: Text('⚡️Chat'),
// backgroundColor: Colors.lightBlueAccent,
// ),
// body: SafeArea(
// child: Column(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.stretch,
// children: <Widget>[
// StreamBuilder<QuerySnapshot>(
// stream: _firestore.collection('messages').snapshots(),
// builder: (context, snapshot){
// if(snapshot.hasData){
// final messages = snapshot.data.docs;
// List<Text> messageWidgets = [];
// for (var message in messages){
// final messageText = message.data['text'];
// final messageSender = message.data['sender'];
//
// final messageWidget = Text('$messageText from $messageSender');
// messageWidgets.add(messageWidget);
// }
//
// return Column(
// children: messageWidgets,
// );
// }
// },
// ),
// Container(
// decoration: kMessageContainerDecoration,
// child: Row(
// crossAxisAlignment: CrossAxisAlignment.center,
// children: <Widget>[
// Expanded(
// child: TextField(
// onChanged: (value) {
// messageText =value;
// //Do something with the user input.
// },
// decoration: kMessageTextFieldDecoration,
// ),
// ),
// FlatButton(
// onPressed: () {
// _firestore.collection('messages').add({
// 'text': messageText,
// 'sender': loggedInUser.email,
// });
//
// //Implement send functionality.
// },
// child: Text(
// 'Send',
// style: kSendButtonTextStyle,
// ),
// ),
// ],
// ),
// ),
// ],
// ),
// ),
// );
// }
//}
//