From 3e64316fa449d4c6d1b267af424daeb73ed9f6a3 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Thu, 29 Jul 2021 20:56:44 +0200 Subject: [PATCH 01/15] add routes --- .gitignore | 1 + lib/main.dart | 10 ++- lib/screens/chat_screen.dart | 3 + lib/screens/login_screen.dart | 2 + lib/screens/registration_screen.dart | 1 + lib/screens/welcome_screen.dart | 5 ++ pubspec.lock | 108 ++++++++------------------- 7 files changed, 53 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 07488ba61a..ad75132676 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages +ios/Flutter/flutter_export_environment.sh diff --git a/lib/main.dart b/lib/main.dart index 6ea23d095c..e5570e7b56 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -12,10 +12,16 @@ class FlashChat extends StatelessWidget { return MaterialApp( theme: ThemeData.dark().copyWith( textTheme: TextTheme( - body1: TextStyle(color: Colors.black54), + bodyText2: TextStyle(color: Colors.black54), ), ), - home: WelcomeScreen(), + initialRoute: WelcomeScreen.welcomId, + routes: { + WelcomeScreen.welcomId: (context) => WelcomeScreen(), + LoginScreen.loginId: (context) => LoginScreen(), + ChatScreen.chatId: (context) => ChatScreen(), + RegistrationScreen.regId: (context) => RegistrationScreen(), + }, ); } } diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 42d8b67b9b..cd1e6e461e 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -2,6 +2,9 @@ import 'package:flutter/material.dart'; import 'package:flash_chat/constants.dart'; class ChatScreen extends StatefulWidget { + + static const String chatId='chat'; + @override _ChatScreenState createState() => _ChatScreenState(); } diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index 852e116a19..31cd320c2f 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; class LoginScreen extends StatefulWidget { + static const String loginId='login'; + @override _LoginScreenState createState() => _LoginScreenState(); } diff --git a/lib/screens/registration_screen.dart b/lib/screens/registration_screen.dart index bbc0d5195e..81083dcabc 100644 --- a/lib/screens/registration_screen.dart +++ b/lib/screens/registration_screen.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; class RegistrationScreen extends StatefulWidget { + static const String regId='register'; @override _RegistrationScreenState createState() => _RegistrationScreenState(); } diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index 6270ccf476..eb930060fe 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -1,6 +1,9 @@ +import 'package:flash_chat/screens/login_screen.dart'; +import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flutter/material.dart'; class WelcomeScreen extends StatefulWidget { + static const String welcomId='welcom'; @override _WelcomeScreenState createState() => _WelcomeScreenState(); } @@ -43,6 +46,7 @@ class _WelcomeScreenState extends State { child: MaterialButton( onPressed: () { //Go to login screen. + Navigator.pushNamed(context, LoginScreen.loginId); }, minWidth: 200.0, height: 42.0, @@ -61,6 +65,7 @@ class _WelcomeScreenState extends State { child: MaterialButton( onPressed: () { //Go to registration screen. + Navigator.pushNamed(context, RegistrationScreen.regId); }, minWidth: 200.0, height: 42.0, diff --git a/pubspec.lock b/pubspec.lock index a48c6ef150..a7a3dad1c9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,48 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.11" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.5.2" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.0" + version: "2.6.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" - charcode: + version: "2.1.0" + characters: dependency: transitive description: - name: charcode + name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.2" - collection: + version: "1.1.0" + charcode: dependency: transitive description: - name: collection + name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.14.11" - convert: + version: "1.2.0" + clock: dependency: transitive description: - name: convert + name: clock url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.1.0" + collection: dependency: transitive description: - name: crypto + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.3" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -64,6 +50,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -74,55 +67,27 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.4" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.0+1" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.5" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -134,62 +99,55 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.5.5" + version: "1.8.1" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.11" + version: "0.3.0" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.5.0" + version: "2.1.0" sdks: - dart: ">=2.4.0 <3.0.0" + dart: ">=2.12.0 <3.0.0" From 0e9a962b1a883cb3d98738f6f70d7ac1912b8a46 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Fri, 30 Jul 2021 09:24:26 +0200 Subject: [PATCH 02/15] add hero animation --- lib/constants.dart | 2 ++ lib/screens/login_screen.dart | 12 ++++++++---- lib/screens/registration_screen.dart | 10 +++++++--- lib/screens/welcome_screen.dart | 10 +++++++--- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 25e5f5c22f..cd2d206098 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +const String logoTag='logo'; + const kSendButtonTextStyle = TextStyle( color: Colors.lightBlueAccent, fontWeight: FontWeight.bold, diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index 31cd320c2f..9b827e2715 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -1,8 +1,9 @@ +import 'package:flash_chat/constants.dart'; import 'package:flutter/material.dart'; class LoginScreen extends StatefulWidget { static const String loginId='login'; - + @override _LoginScreenState createState() => _LoginScreenState(); } @@ -18,9 +19,12 @@ class _LoginScreenState extends State { mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Container( - height: 200.0, - child: Image.asset('images/logo.png'), + Hero( + tag: logoTag, + child: Container( + height: 200.0, + child: Image.asset('images/logo.png'), + ), ), SizedBox( height: 48.0, diff --git a/lib/screens/registration_screen.dart b/lib/screens/registration_screen.dart index 81083dcabc..d9c9272dee 100644 --- a/lib/screens/registration_screen.dart +++ b/lib/screens/registration_screen.dart @@ -1,3 +1,4 @@ +import 'package:flash_chat/constants.dart'; import 'package:flutter/material.dart'; class RegistrationScreen extends StatefulWidget { @@ -17,9 +18,12 @@ class _RegistrationScreenState extends State { mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Container( - height: 200.0, - child: Image.asset('images/logo.png'), + Hero( + tag: logoTag, + child: Container( + height: 200.0, + child: Image.asset('images/logo.png'), + ), ), SizedBox( height: 48.0, diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index eb930060fe..84537da3c7 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -1,3 +1,4 @@ +import 'package:flash_chat/constants.dart'; import 'package:flash_chat/screens/login_screen.dart'; import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flutter/material.dart'; @@ -21,9 +22,12 @@ class _WelcomeScreenState extends State { children: [ Row( children: [ - Container( - child: Image.asset('images/logo.png'), - height: 60.0, + Hero( + tag: logoTag, + child: Container( + child: Image.asset('images/logo.png'), + height: 60.0, + ), ), Text( 'Flash Chat', From 8ebf6cf63b7376b9573322d6a5eeaba43645f994 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Fri, 30 Jul 2021 10:26:52 +0200 Subject: [PATCH 03/15] add animation to home screen --- lib/screens/welcome_screen.dart | 44 ++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index 84537da3c7..730fd32a11 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -4,16 +4,54 @@ import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flutter/material.dart'; class WelcomeScreen extends StatefulWidget { - static const String welcomId='welcom'; + static const String welcomId = 'welcom'; @override _WelcomeScreenState createState() => _WelcomeScreenState(); } -class _WelcomeScreenState extends State { +class _WelcomeScreenState extends State + with SingleTickerProviderStateMixin { + AnimationController controller; + Animation animation; + + @override + void initState() { + super.initState(); + controller = AnimationController( + duration: Duration(seconds: 2), + vsync: this, + // upperBound: 60.0, + ); + + //this is curved animation + // animation = CurvedAnimation(parent: controller, curve: Curves.bounceIn); + + // animation.addStatusListener((status) { + // if (animation.isCompleted) + // controller.reverse(from: 1.0); + // else if (animation.isDismissed) controller.forward(); + // }); + + animation=ColorTween(begin: Colors.blueGrey ,end: Colors.white).animate(controller); + + controller.forward(); + + controller.addListener(() { + setState(() {}); + // print(controller.value); + }); + } + + @override + void dispose() { + controller.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return Scaffold( - backgroundColor: Colors.white, + backgroundColor: animation.value, body: Padding( padding: EdgeInsets.symmetric(horizontal: 24.0), child: Column( From 0a2af3a751f034b1c333298b864aaaa09cb06855 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Fri, 30 Jul 2021 11:05:31 +0200 Subject: [PATCH 04/15] refactor button widget --- lib/components/ButtonWidget.dart | 30 +++++++++++++++++ lib/screens/welcome_screen.dart | 55 ++++++++++---------------------- 2 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 lib/components/ButtonWidget.dart diff --git a/lib/components/ButtonWidget.dart b/lib/components/ButtonWidget.dart new file mode 100644 index 0000000000..77915913fa --- /dev/null +++ b/lib/components/ButtonWidget.dart @@ -0,0 +1,30 @@ +import 'package:flutter/material.dart'; + +class ButtonWidget extends StatelessWidget { + + final Color color; + final String name; + final Function fun; + + ButtonWidget({this.color,this.fun,this.name}); + + @override + Widget build(BuildContext context) { + return Padding( + padding: EdgeInsets.symmetric(vertical: 16.0), + child: Material( + elevation: 5.0, + color: this.color, + borderRadius: BorderRadius.circular(30.0), + child: MaterialButton( + onPressed: fun, + minWidth: 200.0, + height: 42.0, + child: Text( + name, + ), + ), + ), + ); + } +} \ No newline at end of file diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index 730fd32a11..5e3303f262 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -2,6 +2,7 @@ import 'package:flash_chat/constants.dart'; import 'package:flash_chat/screens/login_screen.dart'; import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flutter/material.dart'; +import 'package:flash_chat/components/ButtonWidget.dart'; class WelcomeScreen extends StatefulWidget { static const String welcomId = 'welcom'; @@ -32,13 +33,13 @@ class _WelcomeScreenState extends State // else if (animation.isDismissed) controller.forward(); // }); - animation=ColorTween(begin: Colors.blueGrey ,end: Colors.white).animate(controller); + animation = ColorTween(begin: Colors.blueGrey, end: Colors.white) + .animate(controller); controller.forward(); controller.addListener(() { setState(() {}); - // print(controller.value); }); } @@ -79,43 +80,21 @@ class _WelcomeScreenState extends State SizedBox( height: 48.0, ), - Padding( - padding: EdgeInsets.symmetric(vertical: 16.0), - child: Material( - elevation: 5.0, - color: Colors.lightBlueAccent, - borderRadius: BorderRadius.circular(30.0), - child: MaterialButton( - onPressed: () { - //Go to login screen. - Navigator.pushNamed(context, LoginScreen.loginId); - }, - minWidth: 200.0, - height: 42.0, - child: Text( - 'Log In', - ), - ), - ), + ButtonWidget( + name: 'Log In', + color: Colors.lightBlueAccent, + fun: () { + //Go to login screen. + Navigator.pushNamed(context, LoginScreen.loginId); + }, ), - Padding( - padding: EdgeInsets.symmetric(vertical: 16.0), - child: Material( - color: Colors.blueAccent, - borderRadius: BorderRadius.circular(30.0), - elevation: 5.0, - child: MaterialButton( - onPressed: () { - //Go to registration screen. - Navigator.pushNamed(context, RegistrationScreen.regId); - }, - minWidth: 200.0, - height: 42.0, - child: Text( - 'Register', - ), - ), - ), + ButtonWidget( + color: Colors.blueAccent, + fun: () { + //Go to registration screen. + Navigator.pushNamed(context, RegistrationScreen.regId); + }, + name: 'Register', ), ], ), From 4e8526b48974da15b3851856a94b59041f7a41c1 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Fri, 30 Jul 2021 11:37:58 +0200 Subject: [PATCH 05/15] add animated text --- lib/screens/welcome_screen.dart | 15 ++++++++++----- pubspec.lock | 8 ++++++++ pubspec.yaml | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index 5e3303f262..d024ce106c 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -3,6 +3,7 @@ import 'package:flash_chat/screens/login_screen.dart'; import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/components/ButtonWidget.dart'; +import 'package:animated_text_kit/animated_text_kit.dart'; class WelcomeScreen extends StatefulWidget { static const String welcomId = 'welcom'; @@ -68,11 +69,15 @@ class _WelcomeScreenState extends State height: 60.0, ), ), - Text( - 'Flash Chat', - style: TextStyle( - fontSize: 45.0, - fontWeight: FontWeight.w900, + DefaultTextStyle( + style:TextStyle( + fontSize: 45.0, + fontWeight: FontWeight.w900, + color:Colors.blue[700], + ) , + child: AnimatedTextKit( + animatedTexts: [WavyAnimatedText('Flash Chat')], + displayFullTextOnTap:true, ), ), ], diff --git a/pubspec.lock b/pubspec.lock index a7a3dad1c9..cae1f5bd91 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,13 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + animated_text_kit: + dependency: "direct main" + description: + name: animated_text_kit + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.1" async: dependency: transitive description: @@ -151,3 +158,4 @@ packages: version: "2.1.0" sdks: dart: ">=2.12.0 <3.0.0" + flutter: ">=1.22.0" diff --git a/pubspec.yaml b/pubspec.yaml index e6d5ae3d17..e98dc0bdd6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,7 @@ dependencies: sdk: flutter cupertino_icons: ^0.1.2 + animated_text_kit: ^4.2.1 dev_dependencies: flutter_test: From 27031b8b4f6a38a1b3c711e33db4b9f6eee1f1e1 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Fri, 30 Jul 2021 12:04:23 +0200 Subject: [PATCH 06/15] refactoring to const --- lib/constants.dart | 18 ++++++++- lib/screens/login_screen.dart | 60 +++++----------------------- lib/screens/registration_screen.dart | 59 +++++---------------------- 3 files changed, 36 insertions(+), 101 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index cd2d206098..50d70c7771 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -const String logoTag='logo'; +const String logoTag = 'logo'; const kSendButtonTextStyle = TextStyle( color: Colors.lightBlueAccent, @@ -19,3 +19,19 @@ const kMessageContainerDecoration = BoxDecoration( top: BorderSide(color: Colors.lightBlueAccent, width: 2.0), ), ); + +const kInputTextDecoration = InputDecoration( + hintText: 'Text', + contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), + border: OutlineInputBorder( + borderRadius: BorderRadius.all(Radius.circular(32.0)), + ), + enabledBorder: OutlineInputBorder( + borderSide: BorderSide(color: Colors.lightBlueAccent, width: 1.0), + borderRadius: BorderRadius.all(Radius.circular(32.0)), + ), + focusedBorder: OutlineInputBorder( + borderSide: BorderSide(color: Colors.lightBlueAccent, width: 2.0), + borderRadius: BorderRadius.all(Radius.circular(32.0)), + ), +); diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index 9b827e2715..8066de1094 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -1,8 +1,9 @@ +import 'package:flash_chat/components/ButtonWidget.dart'; import 'package:flash_chat/constants.dart'; import 'package:flutter/material.dart'; class LoginScreen extends StatefulWidget { - static const String loginId='login'; + static const String loginId = 'login'; @override _LoginScreenState createState() => _LoginScreenState(); @@ -33,23 +34,8 @@ class _LoginScreenState extends State { onChanged: (value) { //Do something with the user input. }, - decoration: InputDecoration( - hintText: 'Enter your email', - contentPadding: - EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), - border: OutlineInputBorder( - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - enabledBorder: OutlineInputBorder( - borderSide: - BorderSide(color: Colors.lightBlueAccent, width: 1.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - focusedBorder: OutlineInputBorder( - borderSide: - BorderSide(color: Colors.lightBlueAccent, width: 2.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), + decoration: kInputTextDecoration.copyWith( + hintText: 'Enter your Email.', ), ), SizedBox( @@ -59,45 +45,17 @@ class _LoginScreenState extends State { onChanged: (value) { //Do something with the user input. }, - decoration: InputDecoration( + decoration: kInputTextDecoration.copyWith( hintText: 'Enter your password.', - contentPadding: - EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), - border: OutlineInputBorder( - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - enabledBorder: OutlineInputBorder( - borderSide: - BorderSide(color: Colors.lightBlueAccent, width: 1.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - focusedBorder: OutlineInputBorder( - borderSide: - BorderSide(color: Colors.lightBlueAccent, width: 2.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), ), ), SizedBox( height: 24.0, ), - Padding( - padding: EdgeInsets.symmetric(vertical: 16.0), - child: Material( - color: Colors.lightBlueAccent, - borderRadius: BorderRadius.all(Radius.circular(30.0)), - elevation: 5.0, - child: MaterialButton( - onPressed: () { - //Implement login functionality. - }, - minWidth: 200.0, - height: 42.0, - child: Text( - 'Log In', - ), - ), - ), + ButtonWidget( + color: Colors.lightBlueAccent, + fun: () {}, + name: 'Log In', ), ], ), diff --git a/lib/screens/registration_screen.dart b/lib/screens/registration_screen.dart index d9c9272dee..5facd0a8f9 100644 --- a/lib/screens/registration_screen.dart +++ b/lib/screens/registration_screen.dart @@ -1,8 +1,9 @@ +import 'package:flash_chat/components/ButtonWidget.dart'; import 'package:flash_chat/constants.dart'; import 'package:flutter/material.dart'; class RegistrationScreen extends StatefulWidget { - static const String regId='register'; + static const String regId = 'register'; @override _RegistrationScreenState createState() => _RegistrationScreenState(); } @@ -32,21 +33,8 @@ class _RegistrationScreenState extends State { onChanged: (value) { //Do something with the user input. }, - decoration: InputDecoration( - hintText: 'Enter your email', - contentPadding: - EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), - border: OutlineInputBorder( - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - enabledBorder: OutlineInputBorder( - borderSide: BorderSide(color: Colors.blueAccent, width: 1.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - focusedBorder: OutlineInputBorder( - borderSide: BorderSide(color: Colors.blueAccent, width: 2.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), + decoration: kInputTextDecoration.copyWith( + hintText: 'Enter your Email.', ), ), SizedBox( @@ -56,44 +44,17 @@ class _RegistrationScreenState extends State { onChanged: (value) { //Do something with the user input. }, - decoration: InputDecoration( - hintText: 'Enter your password', - contentPadding: - EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), - border: OutlineInputBorder( - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - enabledBorder: OutlineInputBorder( - borderSide: BorderSide(color: Colors.blueAccent, width: 1.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), - focusedBorder: OutlineInputBorder( - borderSide: BorderSide(color: Colors.blueAccent, width: 2.0), - borderRadius: BorderRadius.all(Radius.circular(32.0)), - ), + decoration: kInputTextDecoration.copyWith( + hintText: 'Enter your password.', ), ), SizedBox( height: 24.0, ), - Padding( - padding: EdgeInsets.symmetric(vertical: 16.0), - child: Material( - color: Colors.blueAccent, - borderRadius: BorderRadius.all(Radius.circular(30.0)), - elevation: 5.0, - child: MaterialButton( - onPressed: () { - //Implement registration functionality. - }, - minWidth: 200.0, - height: 42.0, - child: Text( - 'Register', - style: TextStyle(color: Colors.white), - ), - ), - ), + ButtonWidget( + name: 'Register', + color: Colors.blueAccent, + fun: () {}, ), ], ), From 37e224dbcae9e131c74ba4295204df79f1053b44 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 14:28:53 +0200 Subject: [PATCH 07/15] setup firebase --- .gitignore | 1 + android/app/build.gradle | 5 +- android/app/google-services.json | 39 +++++++++++++ android/build.gradle | 1 + pubspec.lock | 96 ++++++++++++++++++++++++++++++++ pubspec.yaml | 3 + 6 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 android/app/google-services.json diff --git a/.gitignore b/.gitignore index ad75132676..d67e947eea 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages ios/Flutter/flutter_export_environment.sh +.flutter-plugins-dependencies diff --git a/android/app/build.gradle b/android/app/build.gradle index e8d2b49ddc..d4763cfe2a 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -24,6 +24,7 @@ if (flutterVersionName == null) { apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" +apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 29 @@ -33,8 +34,7 @@ android { } defaultConfig { - // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "co.appbrewery.flash_chat" + applicationId "co.Amin.flash_chat" minSdkVersion 16 targetSdkVersion 29 versionCode flutterVersionCode.toInteger() @@ -57,4 +57,5 @@ flutter { dependencies { implementation 'androidx.multidex:multidex:2.0.0' + implementation platform('com.google.firebase:firebase-bom:28.3.0') } diff --git a/android/app/google-services.json b/android/app/google-services.json new file mode 100644 index 0000000000..4e1c821362 --- /dev/null +++ b/android/app/google-services.json @@ -0,0 +1,39 @@ +{ + "project_info": { + "project_number": "634133896248", + "project_id": "flash-chat-b53b8", + "storage_bucket": "flash-chat-b53b8.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:634133896248:android:b16dec15d1983a1bb69acd", + "android_client_info": { + "package_name": "co.Amin.flash_chat" + } + }, + "oauth_client": [ + { + "client_id": "634133896248-3ovprilllms8msddc4mcsr01tdd3q1vh.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAxyBWIgj76cpgVOKnqmw4ugHNUVbJNxMk" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "634133896248-3ovprilllms8msddc4mcsr01tdd3q1vh.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index 6de372893d..f82140cd49 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -6,6 +6,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.google.gms:google-services:4.3.8' } } diff --git a/pubspec.lock b/pubspec.lock index cae1f5bd91..4386108b5a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -43,6 +43,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.0" + cloud_firestore: + dependency: "direct main" + description: + name: cloud_firestore + url: "https://pub.dartlang.org" + source: hosted + version: "2.4.0" + cloud_firestore_platform_interface: + dependency: transitive + description: + name: cloud_firestore_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "5.3.0" + cloud_firestore_web: + dependency: transitive + description: + name: cloud_firestore_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.0" collection: dependency: transitive description: @@ -64,6 +85,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" + firebase_auth: + dependency: "direct main" + description: + name: firebase_auth + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + firebase_auth_platform_interface: + dependency: transitive + description: + name: firebase_auth_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.0" + firebase_auth_web: + dependency: transitive + description: + name: firebase_auth_web + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + firebase_core: + dependency: "direct main" + description: + name: firebase_core + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.0" + firebase_core_platform_interface: + dependency: transitive + description: + name: firebase_core_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.1" + firebase_core_web: + dependency: transitive + description: + name: firebase_core_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" flutter: dependency: "direct main" description: flutter @@ -74,6 +137,32 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" + intl: + dependency: transitive + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.3" matcher: dependency: transitive description: @@ -95,6 +184,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index e98dc0bdd6..ae20d36a48 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,6 +12,9 @@ dependencies: cupertino_icons: ^0.1.2 animated_text_kit: ^4.2.1 + firebase_core: ^1.4.0 + firebase_auth: ^3.0.1 + cloud_firestore: ^2.4.0 dev_dependencies: flutter_test: From bdd28cd88aa5751b3af42b716aafbaa3f3c763f6 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 15:24:49 +0200 Subject: [PATCH 08/15] register user --- lib/constants.dart | 3 +++ lib/main.dart | 7 +++++- lib/screens/chat_screen.dart | 21 +++++++++++++++++ lib/screens/registration_screen.dart | 34 +++++++++++++++++++++++++--- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 50d70c7771..302e85793e 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -22,6 +22,9 @@ const kMessageContainerDecoration = BoxDecoration( const kInputTextDecoration = InputDecoration( hintText: 'Text', + hintStyle: TextStyle( + color: Colors.black, + ), contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), border: OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(32.0)), diff --git a/lib/main.dart b/lib/main.dart index e5570e7b56..8efbcc594a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,10 +1,15 @@ +import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/screens/welcome_screen.dart'; import 'package:flash_chat/screens/login_screen.dart'; import 'package:flash_chat/screens/registration_screen.dart'; import 'package:flash_chat/screens/chat_screen.dart'; -void main() => runApp(FlashChat()); +void main() async{ + WidgetsFlutterBinding.ensureInitialized(); + await Firebase.initializeApp(); + return runApp(FlashChat()); +} class FlashChat extends StatelessWidget { @override diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index cd1e6e461e..5e995a2e00 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -1,3 +1,4 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/constants.dart'; @@ -10,6 +11,26 @@ class ChatScreen extends StatefulWidget { } class _ChatScreenState extends State { + + final _auth = FirebaseAuth.instance; + User loggedInUser; + + void getCurrentUser(){ + try { + final user = _auth.currentUser; + if (user != null) loggedInUser = user; + } catch (e) { + print(e); + } + } + + @override + void initState() { + super.initState(); + getCurrentUser(); + print(loggedInUser.email); + } + @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/screens/registration_screen.dart b/lib/screens/registration_screen.dart index 5facd0a8f9..cc1ced682c 100644 --- a/lib/screens/registration_screen.dart +++ b/lib/screens/registration_screen.dart @@ -1,6 +1,8 @@ import 'package:flash_chat/components/ButtonWidget.dart'; import 'package:flash_chat/constants.dart'; +import 'package:flash_chat/screens/chat_screen.dart'; import 'package:flutter/material.dart'; +import 'package:firebase_auth/firebase_auth.dart'; class RegistrationScreen extends StatefulWidget { static const String regId = 'register'; @@ -9,6 +11,10 @@ class RegistrationScreen extends StatefulWidget { } class _RegistrationScreenState extends State { + String mail; + String pass; + final _auth = FirebaseAuth.instance; + @override Widget build(BuildContext context) { return Scaffold( @@ -30,8 +36,13 @@ class _RegistrationScreenState extends State { height: 48.0, ), TextField( + style: TextStyle( + color: Colors.black, + ), + textAlign: TextAlign.center, + keyboardType: TextInputType.emailAddress, onChanged: (value) { - //Do something with the user input. + mail = value; }, decoration: kInputTextDecoration.copyWith( hintText: 'Enter your Email.', @@ -41,8 +52,13 @@ class _RegistrationScreenState extends State { height: 8.0, ), TextField( + style: TextStyle( + color: Colors.black, + ), + textAlign: TextAlign.center, + obscureText: true, onChanged: (value) { - //Do something with the user input. + pass = value; }, decoration: kInputTextDecoration.copyWith( hintText: 'Enter your password.', @@ -54,7 +70,19 @@ class _RegistrationScreenState extends State { ButtonWidget( name: 'Register', color: Colors.blueAccent, - fun: () {}, + fun: () async { + try { + final newUser = await _auth.createUserWithEmailAndPassword( + email: mail, + password: pass, + ); + if (newUser != null) { + Navigator.pushNamed(context, ChatScreen.chatId); + } + } catch (e) { + print(e); + } + }, ), ], ), From d22d40111c84d4c7e10b00a84bb7ac15298584ce Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 15:41:43 +0200 Subject: [PATCH 09/15] sign in / out --- lib/screens/chat_screen.dart | 3 ++- lib/screens/login_screen.dart | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 5e995a2e00..ab11aeaff2 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -40,7 +40,8 @@ class _ChatScreenState extends State { IconButton( icon: Icon(Icons.close), onPressed: () { - //Implement logout functionality + _auth.signOut(); + Navigator.pop(context); }), ], title: Text('⚡️Chat'), diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index 8066de1094..3085e13f36 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -1,5 +1,7 @@ +import 'package:firebase_auth/firebase_auth.dart'; import 'package:flash_chat/components/ButtonWidget.dart'; import 'package:flash_chat/constants.dart'; +import 'package:flash_chat/screens/chat_screen.dart'; import 'package:flutter/material.dart'; class LoginScreen extends StatefulWidget { @@ -10,6 +12,10 @@ class LoginScreen extends StatefulWidget { } class _LoginScreenState extends State { + final _auth = FirebaseAuth.instance; + String mail; + String pass; + @override Widget build(BuildContext context) { return Scaffold( @@ -31,8 +37,12 @@ class _LoginScreenState extends State { height: 48.0, ), TextField( + style: TextStyle( + color: Colors.black, + ), + textAlign: TextAlign.center, onChanged: (value) { - //Do something with the user input. + mail=value; }, decoration: kInputTextDecoration.copyWith( hintText: 'Enter your Email.', @@ -42,8 +52,13 @@ class _LoginScreenState extends State { height: 8.0, ), TextField( + style: TextStyle( + color: Colors.black, + ), + obscureText: true, + textAlign: TextAlign.center, onChanged: (value) { - //Do something with the user input. + pass=value; }, decoration: kInputTextDecoration.copyWith( hintText: 'Enter your password.', @@ -54,7 +69,18 @@ class _LoginScreenState extends State { ), ButtonWidget( color: Colors.lightBlueAccent, - fun: () {}, + fun: () async{ + try { + final user = await _auth.signInWithEmailAndPassword( + email: mail, + password: pass, + ); + if (user != null) + Navigator.pushNamed(context, ChatScreen.chatId); + } catch (e) { + print(e); + } + }, name: 'Log In', ), ], From 5e1442f9a04c1e79f473d10830102c0dcc874008 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 16:13:04 +0200 Subject: [PATCH 10/15] store data to fire store --- lib/constants.dart | 3 +++ lib/screens/chat_screen.dart | 21 ++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/constants.dart b/lib/constants.dart index 302e85793e..44d153177a 100644 --- a/lib/constants.dart +++ b/lib/constants.dart @@ -11,6 +11,9 @@ const kSendButtonTextStyle = TextStyle( const kMessageTextFieldDecoration = InputDecoration( contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0), hintText: 'Type your message here...', + hintStyle: TextStyle( + color: Colors.black, + ), border: InputBorder.none, ); diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index ab11aeaff2..c3a2a97f07 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -1,21 +1,23 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/constants.dart'; -class ChatScreen extends StatefulWidget { +final _fireStore = FirebaseFirestore.instance.collection('message'); - static const String chatId='chat'; +class ChatScreen extends StatefulWidget { + static const String chatId = 'chat'; @override _ChatScreenState createState() => _ChatScreenState(); } class _ChatScreenState extends State { - final _auth = FirebaseAuth.instance; User loggedInUser; + String messageText; - void getCurrentUser(){ + void getCurrentUser() { try { final user = _auth.currentUser; if (user != null) loggedInUser = user; @@ -28,12 +30,12 @@ class _ChatScreenState extends State { void initState() { super.initState(); getCurrentUser(); - print(loggedInUser.email); } - + @override Widget build(BuildContext context) { return Scaffold( + backgroundColor: Colors.white, appBar: AppBar( leading: null, actions: [ @@ -59,15 +61,16 @@ class _ChatScreenState extends State { children: [ Expanded( child: TextField( + style: TextStyle(color: Colors.black), onChanged: (value) { - //Do something with the user input. + messageText = value; }, decoration: kMessageTextFieldDecoration, ), ), - FlatButton( + TextButton( onPressed: () { - //Implement send functionality. + _fireStore.add({'sender':loggedInUser.email,'text':messageText}); }, child: Text( 'Send', From c705a54d297ec5bdfbe214a442e334e50af39b1d Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 16:32:39 +0200 Subject: [PATCH 11/15] retrive data from firebase --- lib/screens/chat_screen.dart | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index c3a2a97f07..32a6f86101 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -54,6 +54,30 @@ class _ChatScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ + Expanded( + child: StreamBuilder( + stream: _fireStore.snapshots(), + builder: (context, snapshot) { + if (!snapshot.hasData) { + return Text('Loading !'); + } + var messages = snapshot.requireData; + return ListView.builder( + padding: EdgeInsets.all(10.0), + itemCount: messages.size, + itemBuilder: (context, index) { + return Text( + '${messages.docs[index]['sender']} write ${messages.docs[index]['text']}', + style: TextStyle( + color: Colors.black, + fontSize: 20.0, + ), + ); + }, + ); + }, + ), + ), Container( decoration: kMessageContainerDecoration, child: Row( @@ -70,7 +94,8 @@ class _ChatScreenState extends State { ), TextButton( onPressed: () { - _fireStore.add({'sender':loggedInUser.email,'text':messageText}); + _fireStore.add( + {'sender': loggedInUser.email, 'text': messageText}); }, child: Text( 'Send', From 0d9417a8e9ec9cbe0a2073861d240ca443af2530 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 16:52:10 +0200 Subject: [PATCH 12/15] refactor stream widget & update mesaage ui --- lib/components/message_bubble.dart | 42 ++++++++++++++++++++++++++++++ lib/components/message_stream.dart | 35 +++++++++++++++++++++++++ lib/screens/chat_screen.dart | 26 +++--------------- 3 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 lib/components/message_bubble.dart create mode 100644 lib/components/message_stream.dart diff --git a/lib/components/message_bubble.dart b/lib/components/message_bubble.dart new file mode 100644 index 0000000000..0ddca2f35a --- /dev/null +++ b/lib/components/message_bubble.dart @@ -0,0 +1,42 @@ +import 'package:flutter/material.dart'; + +class MessageBubble extends StatelessWidget { + MessageBubble({this.sender, this.text}); + + final String text; + final String sender; + + @override + Widget build(BuildContext context) { + return Padding( + padding: EdgeInsets.all(10.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + sender, + style: TextStyle(fontSize: 12.0), + ), + Material( + borderRadius: BorderRadius.circular(30.0), + elevation: 5.0, + color: Colors.lightBlue, + child: Padding( + padding: EdgeInsets.symmetric( + vertical: 10.0, + horizontal: 20.0, + ), + child: Text( + text, + style: TextStyle( + color: Colors.white, + fontSize: 20.0, + ), + ), + ), + ), + ], + ), + ); + } +} diff --git a/lib/components/message_stream.dart b/lib/components/message_stream.dart new file mode 100644 index 0000000000..b00ade902c --- /dev/null +++ b/lib/components/message_stream.dart @@ -0,0 +1,35 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flash_chat/components/message_bubble.dart'; +import 'package:flutter/material.dart'; + +class MessageStream extends StatelessWidget { + MessageStream(this._fireStore); + + final CollectionReference> _fireStore; + + @override + Widget build(BuildContext context) { + return Expanded( + child: StreamBuilder( + stream: _fireStore.snapshots(), + builder: (context, snapshot) { + if (!snapshot.hasData) { + return Text('something went wrong !'); + } + var messages = snapshot.requireData; + return ListView.builder( + padding: EdgeInsets.all(10.0), + itemCount: messages.size, + itemBuilder: (context, index) { + return MessageBubble( + sender: messages.docs[index]['sender'], + text: messages.docs[index]['text'], + ); + }, + ); + }, + ), + ); + } +} + diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 32a6f86101..4a99be8393 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -1,5 +1,6 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flash_chat/components/message_stream.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/constants.dart'; @@ -54,29 +55,8 @@ class _ChatScreenState extends State { mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Expanded( - child: StreamBuilder( - stream: _fireStore.snapshots(), - builder: (context, snapshot) { - if (!snapshot.hasData) { - return Text('Loading !'); - } - var messages = snapshot.requireData; - return ListView.builder( - padding: EdgeInsets.all(10.0), - itemCount: messages.size, - itemBuilder: (context, index) { - return Text( - '${messages.docs[index]['sender']} write ${messages.docs[index]['text']}', - style: TextStyle( - color: Colors.black, - fontSize: 20.0, - ), - ); - }, - ); - }, - ), + MessageStream( + _fireStore, ), Container( decoration: kMessageContainerDecoration, From b29b2021f6de82b3f741ad8ebf421fe319b69117 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 16:56:21 +0200 Subject: [PATCH 13/15] make text field clear --- lib/screens/chat_screen.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 4a99be8393..043c90f717 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -17,6 +17,7 @@ class _ChatScreenState extends State { final _auth = FirebaseAuth.instance; User loggedInUser; String messageText; + final messageController = TextEditingController(); void getCurrentUser() { try { @@ -69,11 +70,13 @@ class _ChatScreenState extends State { onChanged: (value) { messageText = value; }, + controller: messageController, decoration: kMessageTextFieldDecoration, ), ), TextButton( onPressed: () { + messageController.clear(); _fireStore.add( {'sender': loggedInUser.email, 'text': messageText}); }, From 4c10955de9df6dfaa2697d9afcbadb95373cd075 Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 20:27:58 +0200 Subject: [PATCH 14/15] different ui for users --- lib/components/message_bubble.dart | 22 +++++++++++++++++----- lib/components/message_stream.dart | 7 +++++-- lib/screens/chat_screen.dart | 5 +++-- lib/screens/welcome_screen.dart | 12 +++++++----- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/components/message_bubble.dart b/lib/components/message_bubble.dart index 0ddca2f35a..6b136026e9 100644 --- a/lib/components/message_bubble.dart +++ b/lib/components/message_bubble.dart @@ -1,26 +1,38 @@ import 'package:flutter/material.dart'; class MessageBubble extends StatelessWidget { - MessageBubble({this.sender, this.text}); + MessageBubble({this.sender, this.text, this.isMe}); final String text; final String sender; + final bool isMe; @override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.all(10.0), child: Column( - crossAxisAlignment: CrossAxisAlignment.start, + crossAxisAlignment: + isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ Text( sender, style: TextStyle(fontSize: 12.0), ), Material( - borderRadius: BorderRadius.circular(30.0), + borderRadius: isMe + ? BorderRadius.only( + topLeft: Radius.circular(30.0), + bottomRight: Radius.circular(30.0), + bottomLeft: Radius.circular(30.0), + ) + : BorderRadius.only( + topRight: Radius.circular(30.0), + bottomRight: Radius.circular(30.0), + bottomLeft: Radius.circular(30.0), + ), elevation: 5.0, - color: Colors.lightBlue, + color: isMe ? Colors.white : Colors.lightBlue, child: Padding( padding: EdgeInsets.symmetric( vertical: 10.0, @@ -29,7 +41,7 @@ class MessageBubble extends StatelessWidget { child: Text( text, style: TextStyle( - color: Colors.white, + color: isMe ? Colors.black54 : Colors.white, fontSize: 20.0, ), ), diff --git a/lib/components/message_stream.dart b/lib/components/message_stream.dart index b00ade902c..57bd77c763 100644 --- a/lib/components/message_stream.dart +++ b/lib/components/message_stream.dart @@ -3,14 +3,16 @@ import 'package:flash_chat/components/message_bubble.dart'; import 'package:flutter/material.dart'; class MessageStream extends StatelessWidget { - MessageStream(this._fireStore); + MessageStream(this._fireStore, this.currentUser); final CollectionReference> _fireStore; + final currentUser; @override Widget build(BuildContext context) { return Expanded( child: StreamBuilder( + //.orderBy("time", descending: true) stream: _fireStore.snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) { @@ -18,12 +20,14 @@ class MessageStream extends StatelessWidget { } var messages = snapshot.requireData; return ListView.builder( + reverse: true, padding: EdgeInsets.all(10.0), itemCount: messages.size, itemBuilder: (context, index) { return MessageBubble( sender: messages.docs[index]['sender'], text: messages.docs[index]['text'], + isMe: currentUser == messages.docs[index]['sender'], ); }, ); @@ -32,4 +36,3 @@ class MessageStream extends StatelessWidget { ); } } - diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 043c90f717..16f571580b 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -4,8 +4,6 @@ import 'package:flash_chat/components/message_stream.dart'; import 'package:flutter/material.dart'; import 'package:flash_chat/constants.dart'; -final _fireStore = FirebaseFirestore.instance.collection('message'); - class ChatScreen extends StatefulWidget { static const String chatId = 'chat'; @@ -18,6 +16,8 @@ class _ChatScreenState extends State { User loggedInUser; String messageText; final messageController = TextEditingController(); + final _fireStore = FirebaseFirestore.instance.collection('message'); + void getCurrentUser() { try { @@ -58,6 +58,7 @@ class _ChatScreenState extends State { children: [ MessageStream( _fireStore, + loggedInUser.email, ), Container( decoration: kMessageContainerDecoration, diff --git a/lib/screens/welcome_screen.dart b/lib/screens/welcome_screen.dart index d024ce106c..c2b4d5113d 100644 --- a/lib/screens/welcome_screen.dart +++ b/lib/screens/welcome_screen.dart @@ -62,11 +62,13 @@ class _WelcomeScreenState extends State children: [ Row( children: [ - Hero( - tag: logoTag, - child: Container( - child: Image.asset('images/logo.png'), - height: 60.0, + Flexible( + child: Hero( + tag: logoTag, + child: Container( + child: Image.asset('images/logo.png'), + height: 60.0, + ), ), ), DefaultTextStyle( From c310255539ad355c7190430b28d1d8a18e7b905b Mon Sep 17 00:00:00 2001 From: m0stafaamin Date: Sun, 1 Aug 2021 20:46:29 +0200 Subject: [PATCH 15/15] order message by time sent --- lib/components/message_stream.dart | 2 +- lib/screens/chat_screen.dart | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/components/message_stream.dart b/lib/components/message_stream.dart index 57bd77c763..61512ff527 100644 --- a/lib/components/message_stream.dart +++ b/lib/components/message_stream.dart @@ -13,7 +13,7 @@ class MessageStream extends StatelessWidget { return Expanded( child: StreamBuilder( //.orderBy("time", descending: true) - stream: _fireStore.snapshots(), + stream: _fireStore.orderBy('time',descending: true).snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) { return Text('something went wrong !'); diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 16f571580b..a9723cd1f9 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -18,7 +18,6 @@ class _ChatScreenState extends State { final messageController = TextEditingController(); final _fireStore = FirebaseFirestore.instance.collection('message'); - void getCurrentUser() { try { final user = _auth.currentUser; @@ -78,8 +77,11 @@ class _ChatScreenState extends State { TextButton( onPressed: () { messageController.clear(); - _fireStore.add( - {'sender': loggedInUser.email, 'text': messageText}); + _fireStore.add({ + 'sender': loggedInUser.email, + 'text': messageText, + 'time': DateTime.now(), + }); }, child: Text( 'Send',