diff --git a/lib/components/message.dart b/lib/components/message.dart index 6de9542..680f0b1 100644 --- a/lib/components/message.dart +++ b/lib/components/message.dart @@ -1,8 +1,14 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; class Message extends StatelessWidget { - Message({Key? key, required String this.text, required String this.author}) + Message( + {Key? key, + required String this.text, + required String this.author, + required String this.authorId}) : super(key: key); + String authorId; String text; String author; @@ -11,13 +17,32 @@ class Message extends StatelessWidget { if (text == "") { return Container(); } - return Container( - margin: EdgeInsets.all(5), - child: Card( - child: ListTile( - title: Text(text), - subtitle: Text(author), + bool getCurrentUser() { + final uid = FirebaseAuth.instance.currentUser?.uid; + if (authorId == uid) { + return true; + } else { + return false; + } + } + + return Row( + mainAxisAlignment: + getCurrentUser() ? MainAxisAlignment.end : MainAxisAlignment.start, + children: [ + Container( + margin: EdgeInsets.all(5), + child: SizedBox( + width: 300, + child: Card( + child: ListTile( + title: Text(text), + subtitle: Text(author), + ), + ), ), - )); + ), + ], + ); } } diff --git a/lib/screens/chat.dart b/lib/screens/chat.dart index a59f81b..d3fa90a 100644 --- a/lib/screens/chat.dart +++ b/lib/screens/chat.dart @@ -22,6 +22,7 @@ class ChatScreen extends HookWidget { final ScrollController _scrollController = ScrollController(); void send() { final displayName = FirebaseAuth.instance.currentUser?.displayName; + final email = FirebaseAuth.instance.currentUser?.email; final userId = FirebaseAuth.instance.currentUser?.uid; FirebaseFirestore.instance .collection('Chats') @@ -30,7 +31,7 @@ class ChatScreen extends HookWidget { .add({ "at": Timestamp.now(), "text": messageController.value.text, - "author": displayName, + "author": displayName ?? email, "authorId": userId }); messageController.value = TextEditingValue.empty; @@ -80,8 +81,10 @@ class ChatScreen extends HookWidget { // ); return Message( - text: data['text'].toString(), - author: data['author'].toString()); + text: data['text'].toString(), + author: data['author'].toString(), + authorId: data['authorId'].toString(), + ); }).toList(), ); },