Skip to content

Commit d8848ff

Browse files
committed
Update
1 parent 627b746 commit d8848ff

File tree

3 files changed

+220
-101
lines changed

3 files changed

+220
-101
lines changed

lib/screens/chat_screen.dart

Lines changed: 100 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+
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 _firestore = FirebaseFirestore.instance;
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,6 +73,34 @@ class _ChatScreenState extends State<ChatScreen> {
5073
mainAxisAlignment: MainAxisAlignment.spaceBetween,
5174
crossAxisAlignment: CrossAxisAlignment.stretch,
5275
children: <Widget>[
76+
StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
77+
stream: _firestore.collection('messages').snapshots(),
78+
builder: (context, snapshot) {
79+
if (!snapshot.hasData) {
80+
return const Center(
81+
child: CircularProgressIndicator(
82+
backgroundColor: Colors.lightBlueAccent,
83+
),
84+
);
85+
}
86+
final messages = snapshot.data!.docs;
87+
List<MessageBubble> messageWidgets = [];
88+
for (var message in messages) {
89+
final messageText = message.data()['text'];
90+
final messageSender = message.data()['sender'];
91+
92+
final messageWidget = MessageBubble(text: messageText,sender: messageSender);
93+
94+
messageWidgets.add(messageWidget);
95+
}
96+
return Expanded(
97+
child: ListView(
98+
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
99+
children:messageWidgets,
100+
)
101+
);
102+
},
103+
),
53104
Container(
54105
decoration: kMessageContainerDecoration,
55106
child: Row(
@@ -58,14 +109,20 @@ class _ChatScreenState extends State<ChatScreen> {
58109
Expanded(
59110
child: TextField(
60111
onChanged: (value) {
61-
//Do something with the user input.
112+
messageText = value;
113+
62114
},
63115
decoration: kMessageTextFieldDecoration,
64116
),
65117
),
66118
FlatButton(
67119
onPressed: () {
68-
//Implement send functionality.
120+
//messageText + loggedInUser
121+
_firestore.collection('messages').add({
122+
'text' : messageText,
123+
'sender': loggedInUser?.email,
124+
125+
});
69126
},
70127
child: Text(
71128
'Send',
@@ -81,3 +138,43 @@ class _ChatScreenState extends State<ChatScreen> {
81138
);
82139
}
83140
}
141+
142+
class MessageBubble extends StatefulWidget {
143+
final String? sender;
144+
final String? text;
145+
const MessageBubble({Key? key, this.text,this.sender}) : super(key: key);
146+
147+
@override
148+
State<MessageBubble> createState() => _MessageBubbleState();
149+
}
150+
151+
class _MessageBubbleState extends State<MessageBubble> {
152+
153+
154+
@override
155+
Widget build(BuildContext context) {
156+
return Padding(
157+
padding: const EdgeInsets.all(8.0),
158+
child: Column(
159+
children: <Widget>[
160+
Text('${widget.sender}'),
161+
Material(
162+
borderRadius: BorderRadius.circular(50.0),
163+
elevation:5.0, //Drop Shadow to each text
164+
color: Colors.lightBlueAccent,
165+
child: Padding(
166+
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
167+
child: Text(
168+
'${widget.text}',
169+
style: TextStyle(
170+
color: Colors.white,
171+
fontSize: 15.0,
172+
),
173+
),
174+
),
175+
),
176+
],
177+
),
178+
);
179+
}
180+
}

lib/screens/login_screen.dart

Lines changed: 58 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,72 @@ 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+
Hero(
31+
tag: 'logo',
32+
child: Container(
33+
height: 200.0,
34+
child: Image.asset('images/logo.png'),
35+
),
3136
),
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);
37+
const SizedBox(
38+
height: 48.0,
39+
),
40+
TextField(
41+
textAlign: TextAlign.center,
42+
onChanged: (value) {
43+
email = value;
44+
},
45+
decoration: 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: kInputDecoration.copyWith(hintText: 'Enter your password'),
57+
),
58+
const SizedBox(
59+
height: 24.0,
60+
),
61+
RoundButton(title: 'Log In' , color: Colors.blueAccent, onPressed: (){
62+
setState((){ ///before signIn with Email and Password state changed and it spins
63+
showSpinner = true;
64+
});
65+
try {
66+
final user = _auth.signInWithEmailAndPassword(
67+
email: email!, password: password!);
68+
if (user != null) {
69+
Navigator.pushNamed(context, ChatScreen.id);
70+
}
71+
setState((){ ///After signIn with Email and Password state changed and it stops spins
72+
showSpinner = false;
73+
});
74+
}on Error catch(e){
75+
print(e);
6376
}
64-
}on Error catch(e){
65-
print(e);
66-
}
6777

68-
})
69-
],
78+
})
79+
],
80+
),
7081
),
7182
),
7283
);

0 commit comments

Comments
 (0)