Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store #43

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
ios/Flutter/flutter_export_environment.sh
.flutter-plugins-dependencies
5 changes: 3 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -57,4 +57,5 @@ flutter {

dependencies {
implementation 'androidx.multidex:multidex:2.0.0'
implementation platform('com.google.firebase:firebase-bom:28.3.0')
}
39 changes: 39 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.google.gms:google-services:4.3.8'
}
}

Expand Down
30 changes: 30 additions & 0 deletions lib/components/ButtonWidget.dart
Original file line number Diff line number Diff line change
@@ -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,
),
),
),
);
}
}
54 changes: 54 additions & 0 deletions lib/components/message_bubble.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/material.dart';

class MessageBubble extends StatelessWidget {
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:
isMe ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: [
Text(
sender,
style: TextStyle(fontSize: 12.0),
),
Material(
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: isMe ? Colors.white : Colors.lightBlue,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 10.0,
horizontal: 20.0,
),
child: Text(
text,
style: TextStyle(
color: isMe ? Colors.black54 : Colors.white,
fontSize: 20.0,
),
),
),
),
],
),
);
}
}
38 changes: 38 additions & 0 deletions lib/components/message_stream.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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, this.currentUser);

final CollectionReference<Map<String, dynamic>> _fireStore;
final currentUser;

@override
Widget build(BuildContext context) {
return Expanded(
child: StreamBuilder(
//.orderBy("time", descending: true)
stream: _fireStore.orderBy('time',descending: true).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Text('something went wrong !');
}
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'],
);
},
);
},
),
);
}
}
24 changes: 24 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';

const String logoTag = 'logo';

const kSendButtonTextStyle = TextStyle(
color: Colors.lightBlueAccent,
fontWeight: FontWeight.bold,
Expand All @@ -9,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,
);

Expand All @@ -17,3 +22,22 @@ const kMessageContainerDecoration = BoxDecoration(
top: BorderSide(color: Colors.lightBlueAccent, width: 2.0),
),
);

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)),
),
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)),
),
);
17 changes: 14 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
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
Widget build(BuildContext context) {
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(),
},
);
}
}
47 changes: 43 additions & 4 deletions lib/screens/chat_screen.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
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';

class ChatScreen extends StatefulWidget {
static const String chatId = 'chat';

@override
_ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
final _auth = FirebaseAuth.instance;
User loggedInUser;
String messageText;
final messageController = TextEditingController();
final _fireStore = FirebaseFirestore.instance.collection('message');

void getCurrentUser() {
try {
final user = _auth.currentUser;
if (user != null) loggedInUser = user;
} catch (e) {
print(e);
}
}

@override
void initState() {
super.initState();
getCurrentUser();
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
leading: null,
actions: <Widget>[
IconButton(
icon: Icon(Icons.close),
onPressed: () {
//Implement logout functionality
_auth.signOut();
Navigator.pop(context);
}),
],
title: Text('⚡️Chat'),
Expand All @@ -27,22 +55,33 @@ class _ChatScreenState extends State<ChatScreen> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
MessageStream(
_fireStore,
loggedInUser.email,
),
Container(
decoration: kMessageContainerDecoration,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: TextField(
style: TextStyle(color: Colors.black),
onChanged: (value) {
//Do something with the user input.
messageText = value;
},
controller: messageController,
decoration: kMessageTextFieldDecoration,
),
),
FlatButton(
TextButton(
onPressed: () {
//Implement send functionality.
messageController.clear();
_fireStore.add({
'sender': loggedInUser.email,
'text': messageText,
'time': DateTime.now(),
});
},
child: Text(
'Send',
Expand Down
Loading