@@ -22,141 +22,167 @@ class _HomeScreenState extends State<HomeScreen> {
2222 final chatHandler = ChatHandler ();
2323 final authHandler = AuthHandeler ();
2424 late user.User ? currentUser;
25+ bool _isLoading = true ;
2526
2627 @override
2728 void initState () {
2829 super .initState ();
30+
2931 authHandler.getData (email: _auth.currentUser! .email! ).then ((value) {
3032 setState (() {
3133 currentUser = value;
34+ _isLoading = false ;
3235 });
3336 });
3437 }
3538
3639 @override
3740 Widget build (BuildContext context) {
38- return Scaffold (
39- appBar: AppBar (
40- automaticallyImplyLeading: false ,
41- title: Text ('Welcome ${currentUser !.username } !' ),
42- actions: [
43- TextButton (
44- onPressed: () {
45- _auth.signOut ();
46- Navigator .pushNamed (context, 'Login' );
47- },
48- child: const Text (
49- 'Logout' ,
50- style: TextStyle (color: Colors .white),
51- )),
52- ],
53- ),
54- body: Column (
55- mainAxisSize: MainAxisSize .min,
56- mainAxisAlignment: MainAxisAlignment .end,
57- crossAxisAlignment: CrossAxisAlignment .end,
58- children: [
59- Expanded (
60- child: StreamBuilder (
61- stream: FirebaseFirestore .instance
62- .collection ('chats' )
63- .orderBy ('createdAt' , descending: false )
64- .snapshots (),
65- builder: (context, snapshot) {
66- if (snapshot.connectionState == ConnectionState .waiting) {
67- return const Center (
68- child: CircularProgressIndicator (),
69- );
70- } else if (snapshot.hasError) {
71- return Text ("Something Happned Wrong!" );
72- } else if (snapshot.hasData) {
73- final List <Chat > chats = List .from (snapshot.data! .docs
74- .map ((e) => Chat .fromMap (e.data ()))
75- .toList ());
76- return Column (
77- mainAxisAlignment: MainAxisAlignment .end,
78- crossAxisAlignment: CrossAxisAlignment .center,
79- children: [
80- Expanded (
81- child: ListView .builder (
82- shrinkWrap: true ,
83- itemCount: chats.length,
84- itemBuilder: (context, index) {
85- return Padding (
86- padding: const EdgeInsets .all (8.0 ),
87- child: Column (
88- mainAxisAlignment: MainAxisAlignment .end,
89- crossAxisAlignment: CrossAxisAlignment .end,
90- children: [
91- Text (chats[index].username! ),
92- SizedBox (
93- height: 10 ,
94- ),
95- BubbleSpecialThree (
96- text: chats[index].message! ,
97- color: Color (0xFF1B97F3 ),
98- tail: true ,
99- textStyle: TextStyle (
100- color: Colors .white, fontSize: 16 ),
101- ),
102- ],
41+ return _isLoading
42+ ? Scaffold (
43+ body: Center (
44+ child: Column (
45+ mainAxisAlignment: MainAxisAlignment .center,
46+ crossAxisAlignment: CrossAxisAlignment .center,
47+ children: const [
48+ SizedBox (
49+ height: 50 ,
50+ ),
51+ CircularProgressIndicator (),
52+ SizedBox (
53+ height: 50 ,
54+ ),
55+ Text ('Fetching Current Messsages' ),
56+ ],
57+ ),
58+ ),
59+ )
60+ : Scaffold (
61+ appBar: AppBar (
62+ automaticallyImplyLeading: false ,
63+ title: Text ('Welcome ${currentUser !.username } !' ),
64+ actions: [
65+ TextButton (
66+ onPressed: () {
67+ _auth.signOut ();
68+ Navigator .pushNamed (context, 'Login' );
69+ },
70+ child: const Text (
71+ 'Logout' ,
72+ style: TextStyle (color: Colors .white),
73+ )),
74+ ],
75+ ),
76+ body: Column (
77+ mainAxisSize: MainAxisSize .min,
78+ mainAxisAlignment: MainAxisAlignment .end,
79+ crossAxisAlignment: CrossAxisAlignment .end,
80+ children: [
81+ Expanded (
82+ child: StreamBuilder (
83+ stream: FirebaseFirestore .instance
84+ .collection ('chats' )
85+ .orderBy ('createdAt' , descending: false )
86+ .snapshots (),
87+ builder: (context, snapshot) {
88+ if (snapshot.connectionState == ConnectionState .waiting) {
89+ return const Center (
90+ child: CircularProgressIndicator (),
91+ );
92+ } else if (snapshot.hasError) {
93+ return Text ("Something Happned Wrong!" );
94+ } else if (snapshot.hasData) {
95+ final List <Chat > chats = List .from (snapshot.data! .docs
96+ .map ((e) => Chat .fromMap (e.data ()))
97+ .toList ());
98+ return Column (
99+ mainAxisAlignment: MainAxisAlignment .end,
100+ crossAxisAlignment: CrossAxisAlignment .center,
101+ children: [
102+ Expanded (
103+ child: ListView .builder (
104+ shrinkWrap: true ,
105+ itemCount: chats.length,
106+ itemBuilder: (context, index) {
107+ return Padding (
108+ padding: const EdgeInsets .all (8.0 ),
109+ child: Column (
110+ mainAxisAlignment: MainAxisAlignment .end,
111+ crossAxisAlignment:
112+ CrossAxisAlignment .end,
113+ children: [
114+ Text (chats[index].username! ),
115+ SizedBox (
116+ height: 10 ,
117+ ),
118+ BubbleSpecialThree (
119+ text: chats[index].message! ,
120+ color: Color (0xFF1B97F3 ),
121+ tail: true ,
122+ textStyle: TextStyle (
123+ color: Colors .white,
124+ fontSize: 16 ),
125+ ),
126+ ],
127+ ),
128+ );
129+ },
103130 ),
104- );
105- },
131+ ),
132+ SizedBox (
133+ height: 10 ,
134+ ),
135+ ],
136+ );
137+ } else {
138+ return Text ("Something Happned Wrong!" );
139+ }
140+ },
141+ ),
142+ ),
143+ Padding (
144+ padding:
145+ const EdgeInsets .symmetric (horizontal: 8.0 , vertical: 8 ),
146+ child: Row (
147+ mainAxisAlignment: MainAxisAlignment .spaceBetween,
148+ children: [
149+ Container (
150+ width: 320 ,
151+ decoration: BoxDecoration (
152+ border: Border .all (color: Colors .black, width: 1 ),
153+ borderRadius: BorderRadius .circular (16 )),
154+ child: Padding (
155+ padding: const EdgeInsets .all (8.0 ),
156+ child: TextFormField (
157+ controller: _msgController,
158+ decoration: InputDecoration (
159+ hintText: 'Type Your Msg' ,
160+ border: InputBorder .none),
161+ ),
106162 ),
107163 ),
108- SizedBox (
109- height: 10 ,
110- ),
164+ IconButton (
165+ onPressed: () {
166+ if (_msgController.text == '' ) {
167+ return ;
168+ } else {
169+ chatHandler.sendMessage (
170+ chat: Chat (
171+ message: _msgController.text,
172+ username: currentUser! .username,
173+ createdAt: DateTime .now ().toLocal ().toString (),
174+ ));
175+ _msgController.clear ();
176+ }
177+ },
178+ icon: Icon (Icons .send, color: Colors .blueAccent),
179+ iconSize: 50 ,
180+ )
111181 ],
112- );
113- } else {
114- return Text ("Something Happned Wrong!" );
115- }
116- },
117- ),
118- ),
119- Padding (
120- padding: const EdgeInsets .symmetric (horizontal: 8.0 , vertical: 8 ),
121- child: Row (
122- mainAxisAlignment: MainAxisAlignment .spaceBetween,
123- children: [
124- Container (
125- width: 300 ,
126- decoration: BoxDecoration (
127- border: Border .all (color: Colors .black, width: 1 ),
128- borderRadius: BorderRadius .circular (16 )),
129- child: Padding (
130- padding: const EdgeInsets .all (8.0 ),
131- child: TextFormField (
132- controller: _msgController,
133- decoration: InputDecoration (
134- hintText: 'Type Your Msg' , border: InputBorder .none),
135- ),
136182 ),
137183 ),
138- IconButton (
139- onPressed: () {
140- if (_msgController.text == '' ) {
141- return ;
142- } else {
143- chatHandler.sendMessage (
144- chat: Chat (
145- message: _msgController.text,
146- username: currentUser! .username,
147- createdAt: DateTime .now ().toLocal ().toString (),
148- ));
149- _msgController.clear ();
150- }
151- },
152- icon: Icon (Icons .send),
153- iconSize: 50 ,
154- )
155184 ],
156185 ),
157- ),
158- ],
159- ),
160- );
186+ );
161187 }
162188}
0 commit comments