Skip to content

Commit 3b419cb

Browse files
committed
Merge branch 'master_twentyeight' of https://github.com/AvinandanBose/FlashChat_Flutter_x_Firebase_Cloud_Firestore_Updates into master_twentynine
� Conflicts: � lib/screens/chat_screen.dart � lib/screens/login_screen.dart � lib/screens/registration_screen.dart
2 parents 090e19a + d8848ff commit 3b419cb

File tree

4 files changed

+353
-122
lines changed

4 files changed

+353
-122
lines changed

lib/screens/chat_screen.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
44
import 'package:flash_chat_flutter_firebase/constants.dart';
55

66
final _firestore = FirebaseFirestore.instance;
7+
User? loggedInUser;
78
class ChatScreen extends StatefulWidget {
89
static const String id = 'chat_screen';
910
@override
@@ -60,9 +61,9 @@ class _ChatScreenState extends State<ChatScreen> {
6061
IconButton(
6162
icon: Icon(Icons.close),
6263
onPressed: () {
63-
// _auth.signOut();
64-
// Navigator.pop(context);
65-
getMessages();
64+
_auth.signOut();
65+
Navigator.pop(context);
66+
6667
}),
6768
],
6869
title: Text('⚡️Chat'),
@@ -91,8 +92,8 @@ class _ChatScreenState extends State<ChatScreen> {
9192
),
9293
FlatButton(
9394
onPressed: () {
94-
//messageText + loggedInUser
9595
messageTextController.clear();
96+
//messageText + loggedInUser
9697
_firestore.collection('messages').add({
9798
'text' : messageText,
9899
'sender': loggedInUser?.email,
@@ -129,13 +130,19 @@ class MessagesStream extends StatelessWidget {
129130
),
130131
);
131132
}
132-
final messages = snapshot.data!.docs;
133+
final messages = snapshot.data!.docs.reversed;
133134
List<MessageBubble> messageWidgets = [];
134135
for (var message in messages) {
135136
final messageText = message.data()['text'];
136137
final messageSender = message.data()['sender'];
137138

138-
final messageWidget = MessageBubble(text: messageText,sender: messageSender);
139+
final currentUser = loggedInUser?.email;
140+
141+
if(currentUser == messageSender){
142+
143+
}
144+
145+
final messageWidget = MessageBubble(text: messageText,sender: messageSender ,isMe: currentUser==messageSender,);
139146

140147
messageWidgets.add(messageWidget);
141148
}
@@ -154,7 +161,8 @@ class MessagesStream extends StatelessWidget {
154161
class MessageBubble extends StatefulWidget {
155162
final String? sender;
156163
final String? text;
157-
const MessageBubble({Key? key, this.text,this.sender}) : super(key: key);
164+
final bool? isMe;
165+
const MessageBubble({Key? key, this.text,this.sender, this.isMe}) : super(key: key);
158166

159167
@override
160168
State<MessageBubble> createState() => _MessageBubbleState();
@@ -168,23 +176,23 @@ class _MessageBubbleState extends State<MessageBubble> {
168176
return Padding(
169177
padding: const EdgeInsets.all(8.0),
170178
child: Column(
171-
crossAxisAlignment: CrossAxisAlignment.end,
179+
crossAxisAlignment: widget.isMe! ? CrossAxisAlignment.end : CrossAxisAlignment.start,
172180
children: <Widget>[
173181
Text('${widget.sender}',
174182
style: const TextStyle(
175183
fontSize: 12.0,
176184
color: Colors.black54,
177185
),),
178186
Material(
179-
borderRadius: BorderRadius.circular(50.0),
187+
borderRadius: widget.isMe! ?const 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) ),
180188
elevation:5.0, //Drop Shadow to each text
181-
color: Colors.lightBlueAccent,
189+
color: widget.isMe! ? Colors.lightBlueAccent : Colors.white,
182190
child: Padding(
183191
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
184192
child: Text(
185193
'${widget.text}',
186194
style: TextStyle(
187-
color: Colors.white,
195+
color: widget.isMe! ? Colors.white : Colors.black54 ,
188196
fontSize: 15.0,
189197
),
190198
),

lib/screens/login_screen.dart

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
33
import 'package:flash_chat_flutter_firebase/components/rounded_button.dart';
44
import 'package:flash_chat_flutter_firebase/constants.dart';
55
import 'package:firebase_auth/firebase_auth.dart';
6-
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
6+
77
class LoginScreen extends StatefulWidget {
88
static const String id = 'login_screen';
99
@override
@@ -12,74 +12,68 @@ class LoginScreen extends StatefulWidget {
1212

1313
class _LoginScreenState extends State<LoginScreen> {
1414
final _auth = FirebaseAuth.instance;
15-
bool showSpinner = false;
1615
String? email;
1716
String? password;
1817
@override
1918
Widget build(BuildContext context) {
2019
return Scaffold(
2120
backgroundColor: Colors.white,
22-
body: ModalProgressHUD(
23-
inAsyncCall: showSpinner, ///Here we call the bool value (initially it is false)
24-
child: Padding(
25-
padding: EdgeInsets.symmetric(horizontal: 24.0),
26-
child: Column(
27-
mainAxisAlignment: MainAxisAlignment.center,
28-
crossAxisAlignment: CrossAxisAlignment.stretch,
29-
children: <Widget>[
30-
Flexible(
31-
child: Hero(
32-
tag: 'logo',
33-
child: Container(
34-
height: 200.0,
35-
child: Image.asset('images/logo.png'),
36-
),
21+
body: Padding(
22+
padding: EdgeInsets.symmetric(horizontal: 24.0),
23+
child: Column(
24+
mainAxisAlignment: MainAxisAlignment.center,
25+
crossAxisAlignment: CrossAxisAlignment.stretch,
26+
children: <Widget>[
27+
Flexible(
28+
child: Hero(
29+
tag: 'logo',
30+
child: Container(
31+
height: 200.0,
32+
child: Image.asset('images/logo.png'),
3733
),
3834
),
39-
const SizedBox(
40-
height: 48.0,
41-
),
42-
TextField(
43-
textAlign: TextAlign.center,
44-
onChanged: (value) {
45-
email = value;
46-
},
47-
decoration: kInputDecoration.copyWith(hintText: 'Enter your email'),
48-
),
49-
const SizedBox(
50-
height: 8.0,
51-
),
52-
TextField(
53-
textAlign: TextAlign.center,
54-
obscureText: true,
55-
onChanged: (value) {
56-
password = value;
57-
},
58-
decoration: kInputDecoration.copyWith(hintText: 'Enter your password'),
59-
),
60-
const SizedBox(
61-
height: 24.0,
62-
),
63-
RoundButton(title: 'Log In' , color: Colors.blueAccent, onPressed: (){
64-
setState((){ ///before signIn with Email and Password state changed and it spins
65-
showSpinner = true;
66-
});
67-
try {
68-
final user = _auth.signInWithEmailAndPassword(
69-
email: email!, password: password!);
70-
if (user != null) {
71-
Navigator.pushNamed(context, ChatScreen.id);
35+
),
36+
const SizedBox(
37+
height: 48.0,
38+
),
39+
TextField(
40+
textAlign: TextAlign.center,
41+
onChanged: (value) {
42+
email = value;
43+
},
44+
decoration:
45+
kInputDecoration.copyWith(hintText: 'Enter your email'),
46+
),
47+
const SizedBox(
48+
height: 8.0,
49+
),
50+
TextField(
51+
textAlign: TextAlign.center,
52+
obscureText: true,
53+
onChanged: (value) {
54+
password = value;
55+
},
56+
decoration:
57+
kInputDecoration.copyWith(hintText: 'Enter your password'),
58+
),
59+
const SizedBox(
60+
height: 24.0,
61+
),
62+
RoundButton(
63+
title: 'Log In',
64+
color: Colors.blueAccent,
65+
onPressed: () {
66+
try {
67+
final user = _auth.signInWithEmailAndPassword(
68+
email: email!, password: password!);
69+
if (user != null) {
70+
Navigator.pushNamed(context, ChatScreen.id);
71+
}
72+
} on Error catch (e) {
73+
print(e);
7274
}
73-
setState((){ ///After signIn with Email and Password state changed and it stops spins
74-
showSpinner = false;
75-
});
76-
}on Error catch(e){
77-
print(e);
78-
}
79-
80-
})
81-
],
82-
),
75+
})
76+
],
8377
),
8478
),
8579
);

lib/screens/registration_screen.dart

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import '../components/rounded_button.dart';
66
import 'package:firebase_auth/firebase_auth.dart';
77

88
import 'chat_screen.dart';
9-
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
109

1110
class RegistrationScreen extends StatefulWidget {
1211
static const String id = 'registration_screen';
@@ -16,77 +15,73 @@ class RegistrationScreen extends StatefulWidget {
1615

1716
class _RegistrationScreenState extends State<RegistrationScreen> {
1817
final _auth = FirebaseAuth.instance;
19-
bool showSpinner = false;
2018
String? email;
2119
String? password;
2220
@override
2321
Widget build(BuildContext context) {
2422
return Scaffold(
2523
backgroundColor: Colors.white,
26-
body: ModalProgressHUD(
27-
inAsyncCall: showSpinner, ///Here we call the bool value (initially it is false)
28-
child: Padding(
29-
padding: EdgeInsets.symmetric(horizontal: 24.0),
30-
child: Column(
31-
mainAxisAlignment: MainAxisAlignment.center,
32-
crossAxisAlignment: CrossAxisAlignment.stretch,
33-
children: <Widget>[
34-
Hero(
24+
body: Padding(
25+
padding: EdgeInsets.symmetric(horizontal: 24.0),
26+
child: Column(
27+
mainAxisAlignment: MainAxisAlignment.center,
28+
crossAxisAlignment: CrossAxisAlignment.stretch,
29+
children: <Widget>[
30+
Flexible(
31+
child: Hero(
3532
tag: 'logo',
3633
child: Container(
3734
height: 200.0,
3835
child: Image.asset('images/logo.png'),
3936
),
4037
),
41-
const SizedBox(
42-
height: 48.0,
43-
),
44-
Flexible(
45-
child: TextField(
46-
keyboardType: TextInputType.emailAddress,
47-
textAlign:TextAlign.center,
48-
onChanged: (value) {
49-
email = value;
50-
},
51-
decoration: kInputDecoration.copyWith(hintText: "Enter your Email"),
52-
),
53-
),
54-
const SizedBox(
55-
height: 8.0,
56-
),
57-
TextField(
58-
keyboardType: TextInputType.visiblePassword,
59-
textAlign:TextAlign.center,
60-
obscureText: true,
61-
onChanged: (value) {
62-
password = value;
63-
},
64-
decoration: kInputDecoration.copyWith(hintText: "Enter your password"),
65-
),
66-
const SizedBox(
67-
height: 24.0,
68-
),
69-
RoundButton(title:'Register', color: Colors.blueAccent, onPressed: ()async{
70-
setState((){ ///before creation of Email and Password state changed and it spins
71-
showSpinner = true;
72-
});
38+
),
39+
const SizedBox(
40+
height: 48.0,
41+
),
42+
TextField(
43+
keyboardType: TextInputType.emailAddress,
44+
textAlign: TextAlign.center,
45+
onChanged: (value) {
46+
email = value;
47+
},
48+
decoration:
49+
kInputDecoration.copyWith(hintText: "Enter your Email"),
50+
),
51+
const SizedBox(
52+
height: 8.0,
53+
),
54+
TextField(
55+
keyboardType: TextInputType.visiblePassword,
56+
textAlign: TextAlign.center,
57+
obscureText: true,
58+
onChanged: (value) {
59+
password = value;
60+
},
61+
decoration:
62+
kInputDecoration.copyWith(hintText: "Enter your password"),
63+
),
64+
const SizedBox(
65+
height: 24.0,
66+
),
67+
RoundButton(
68+
title: 'Register',
69+
color: Colors.blueAccent,
70+
onPressed: () async {
7371
try {
7472
final newUser = await _auth.createUserWithEmailAndPassword(
7573
email: email!, password: password!);
76-
if(newUser != null){
74+
if (newUser != null) {
7775
Navigator.pushNamed(context, ChatScreen.id);
7876
}
79-
setState((){ ///After creation of Email and Password state changed and it stops spins
80-
showSpinner = false;
81-
});
82-
}on Error catch(e){
77+
} on Error catch (e) {
8378
print(e);
8479
}
8580
// print(email);
8681
// print(password);
87-
},),
88-
],
89-
),
82+
},
83+
),
84+
],
9085
),
9186
),
9287
);

0 commit comments

Comments
 (0)