Skip to content

Commit 19aa15b

Browse files
committed
your messages and other ones differ
1 parent a6291ff commit 19aa15b

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

lib/components/message.dart

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import 'package:firebase_auth/firebase_auth.dart';
12
import 'package:flutter/material.dart';
23

34
class Message extends StatelessWidget {
4-
Message({Key? key, required String this.text, required String this.author})
5+
Message(
6+
{Key? key,
7+
required String this.text,
8+
required String this.author,
9+
required String this.authorId})
510
: super(key: key);
11+
String authorId;
612
String text;
713
String author;
814

@@ -11,13 +17,32 @@ class Message extends StatelessWidget {
1117
if (text == "") {
1218
return Container();
1319
}
14-
return Container(
15-
margin: EdgeInsets.all(5),
16-
child: Card(
17-
child: ListTile(
18-
title: Text(text),
19-
subtitle: Text(author),
20+
bool getCurrentUser() {
21+
final uid = FirebaseAuth.instance.currentUser?.uid;
22+
if (authorId == uid) {
23+
return true;
24+
} else {
25+
return false;
26+
}
27+
}
28+
29+
return Row(
30+
mainAxisAlignment:
31+
getCurrentUser() ? MainAxisAlignment.end : MainAxisAlignment.start,
32+
children: [
33+
Container(
34+
margin: EdgeInsets.all(5),
35+
child: SizedBox(
36+
width: 300,
37+
child: Card(
38+
child: ListTile(
39+
title: Text(text),
40+
subtitle: Text(author),
41+
),
42+
),
2043
),
21-
));
44+
),
45+
],
46+
);
2247
}
2348
}

lib/screens/chat.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ChatScreen extends HookWidget {
2222
final ScrollController _scrollController = ScrollController();
2323
void send() {
2424
final displayName = FirebaseAuth.instance.currentUser?.displayName;
25+
final email = FirebaseAuth.instance.currentUser?.email;
2526
final userId = FirebaseAuth.instance.currentUser?.uid;
2627
FirebaseFirestore.instance
2728
.collection('Chats')
@@ -30,7 +31,7 @@ class ChatScreen extends HookWidget {
3031
.add({
3132
"at": Timestamp.now(),
3233
"text": messageController.value.text,
33-
"author": displayName,
34+
"author": displayName ?? email,
3435
"authorId": userId
3536
});
3637
messageController.value = TextEditingValue.empty;
@@ -80,8 +81,10 @@ class ChatScreen extends HookWidget {
8081
// );
8182

8283
return Message(
83-
text: data['text'].toString(),
84-
author: data['author'].toString());
84+
text: data['text'].toString(),
85+
author: data['author'].toString(),
86+
authorId: data['authorId'].toString(),
87+
);
8588
}).toList(),
8689
);
8790
},

0 commit comments

Comments
 (0)