Skip to content

Commit 090e19a

Browse files
committed
Update
1 parent 627b746 commit 090e19a

File tree

3 files changed

+241
-101
lines changed

3 files changed

+241
-101
lines changed

lib/screens/chat_screen.dart

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
12
import 'package:firebase_auth/firebase_auth.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flash_chat_flutter_firebase/constants.dart';
45

6+
final _firestore = FirebaseFirestore.instance;
57
class ChatScreen extends StatefulWidget {
68
static const String id = 'chat_screen';
79
@override
810
_ChatScreenState createState() => _ChatScreenState();
911
}
1012

1113
class _ChatScreenState extends State<ChatScreen> {
14+
15+
final messageTextController = TextEditingController();
1216
final _auth = FirebaseAuth.instance;
1317
User? loggedInUser;
18+
String? messageText;
1419

1520
void getCurrentUser() async {
1621
try {
@@ -24,6 +29,22 @@ class _ChatScreenState extends State<ChatScreen> {
2429
print(e);
2530
}
2631
}
32+
33+
// void getMessages() async{
34+
// final messages = await _firestore.collection('messages').get();
35+
// for(var message in messages.docs){
36+
// print(message.data());
37+
// }
38+
// }
39+
void getMessages() async {
40+
await for (var snapshot in _firestore.collection('messages').snapshots()) {
41+
for (var messages in snapshot.docs) {
42+
print(messages.data());
43+
}
44+
}
45+
}
46+
47+
2748
@override
2849
void initState() {
2950
super.initState();
@@ -39,7 +60,9 @@ class _ChatScreenState extends State<ChatScreen> {
3960
IconButton(
4061
icon: Icon(Icons.close),
4162
onPressed: () {
42-
//Implement logout functionality
63+
// _auth.signOut();
64+
// Navigator.pop(context);
65+
getMessages();
4366
}),
4467
],
4568
title: Text('⚡️Chat'),
@@ -50,22 +73,31 @@ class _ChatScreenState extends State<ChatScreen> {
5073
mainAxisAlignment: MainAxisAlignment.spaceBetween,
5174
crossAxisAlignment: CrossAxisAlignment.stretch,
5275
children: <Widget>[
76+
MessagesStream(),
5377
Container(
5478
decoration: kMessageContainerDecoration,
5579
child: Row(
5680
crossAxisAlignment: CrossAxisAlignment.center,
5781
children: <Widget>[
5882
Expanded(
5983
child: TextField(
84+
controller: messageTextController,
6085
onChanged: (value) {
61-
//Do something with the user input.
86+
messageText = value;
87+
6288
},
6389
decoration: kMessageTextFieldDecoration,
6490
),
6591
),
6692
FlatButton(
6793
onPressed: () {
68-
//Implement send functionality.
94+
//messageText + loggedInUser
95+
messageTextController.clear();
96+
_firestore.collection('messages').add({
97+
'text' : messageText,
98+
'sender': loggedInUser?.email,
99+
100+
});
69101
},
70102
child: Text(
71103
'Send',
@@ -81,3 +113,85 @@ class _ChatScreenState extends State<ChatScreen> {
81113
);
82114
}
83115
}
116+
117+
class MessagesStream extends StatelessWidget {
118+
const MessagesStream({Key? key}) : super(key: key);
119+
120+
@override
121+
Widget build(BuildContext context) {
122+
return StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
123+
stream: _firestore.collection('messages').snapshots(),
124+
builder: (context, snapshot) {
125+
if (!snapshot.hasData) {
126+
return const Center(
127+
child: CircularProgressIndicator(
128+
backgroundColor: Colors.lightBlueAccent,
129+
),
130+
);
131+
}
132+
final messages = snapshot.data!.docs;
133+
List<MessageBubble> messageWidgets = [];
134+
for (var message in messages) {
135+
final messageText = message.data()['text'];
136+
final messageSender = message.data()['sender'];
137+
138+
final messageWidget = MessageBubble(text: messageText,sender: messageSender);
139+
140+
messageWidgets.add(messageWidget);
141+
}
142+
return Expanded(
143+
child: ListView(
144+
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
145+
children:messageWidgets,
146+
)
147+
);
148+
},
149+
);
150+
}
151+
}
152+
153+
154+
class MessageBubble extends StatefulWidget {
155+
final String? sender;
156+
final String? text;
157+
const MessageBubble({Key? key, this.text,this.sender}) : super(key: key);
158+
159+
@override
160+
State<MessageBubble> createState() => _MessageBubbleState();
161+
}
162+
163+
class _MessageBubbleState extends State<MessageBubble> {
164+
165+
166+
@override
167+
Widget build(BuildContext context) {
168+
return Padding(
169+
padding: const EdgeInsets.all(8.0),
170+
child: Column(
171+
crossAxisAlignment: CrossAxisAlignment.end,
172+
children: <Widget>[
173+
Text('${widget.sender}',
174+
style: const TextStyle(
175+
fontSize: 12.0,
176+
color: Colors.black54,
177+
),),
178+
Material(
179+
borderRadius: BorderRadius.circular(50.0),
180+
elevation:5.0, //Drop Shadow to each text
181+
color: Colors.lightBlueAccent,
182+
child: Padding(
183+
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
184+
child: Text(
185+
'${widget.text}',
186+
style: TextStyle(
187+
color: Colors.white,
188+
fontSize: 15.0,
189+
),
190+
),
191+
),
192+
),
193+
],
194+
),
195+
);
196+
}
197+
}

lib/screens/login_screen.dart

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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';
67
class LoginScreen extends StatefulWidget {
78
static const String id = 'login_screen';
89
@override
@@ -11,62 +12,74 @@ class LoginScreen extends StatefulWidget {
1112

1213
class _LoginScreenState extends State<LoginScreen> {
1314
final _auth = FirebaseAuth.instance;
15+
bool showSpinner = false;
1416
String? email;
1517
String? password;
1618
@override
1719
Widget build(BuildContext context) {
1820
return Scaffold(
1921
backgroundColor: Colors.white,
20-
body: Padding(
21-
padding: EdgeInsets.symmetric(horizontal: 24.0),
22-
child: Column(
23-
mainAxisAlignment: MainAxisAlignment.center,
24-
crossAxisAlignment: CrossAxisAlignment.stretch,
25-
children: <Widget>[
26-
Hero(
27-
tag: 'logo',
28-
child: Container(
29-
height: 200.0,
30-
child: Image.asset('images/logo.png'),
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+
),
37+
),
3138
),
32-
),
33-
const SizedBox(
34-
height: 48.0,
35-
),
36-
TextField(
37-
textAlign: TextAlign.center,
38-
onChanged: (value) {
39-
email = value;
40-
},
41-
decoration: kInputDecoration.copyWith(hintText: 'Enter your email'),
42-
),
43-
const SizedBox(
44-
height: 8.0,
45-
),
46-
TextField(
47-
textAlign: TextAlign.center,
48-
obscureText: true,
49-
onChanged: (value) {
50-
password = value;
51-
},
52-
decoration: kInputDecoration.copyWith(hintText: 'Enter your password'),
53-
),
54-
const SizedBox(
55-
height: 24.0,
56-
),
57-
RoundButton(title: 'Log In' , color: Colors.blueAccent, onPressed: (){
58-
try {
59-
final user = _auth.signInWithEmailAndPassword(
60-
email: email!, password: password!);
61-
if (user != null) {
62-
Navigator.pushNamed(context, ChatScreen.id);
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);
72+
}
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);
6378
}
64-
}on Error catch(e){
65-
print(e);
66-
}
6779

68-
})
69-
],
80+
})
81+
],
82+
),
7083
),
7184
),
7285
);

0 commit comments

Comments
 (0)