Skip to content

Commit 19ae588

Browse files
committed
update
1 parent d4a00ee commit 19ae588

File tree

10 files changed

+299
-121
lines changed

10 files changed

+299
-121
lines changed

.idea/libraries/Dart_Packages.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Flutter_Plugins.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 111 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/chat.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'dart:async';
2+
import 'dart:io';
3+
import 'package:flutter/material.dart';
4+
import 'package:cloud_firestore/cloud_firestore.dart';
5+
import 'package:flutter_chat_demo/const.dart';
6+
import 'package:image_picker/image_picker.dart';
7+
8+
class Chat extends StatelessWidget {
9+
@override
10+
Widget build(BuildContext context) {
11+
return new Scaffold(
12+
appBar: new AppBar(
13+
title: new Text(
14+
'CHAT',
15+
style: TextStyle(color: primaryColor, fontWeight: FontWeight.bold),
16+
),
17+
centerTitle: true,
18+
),
19+
body: new ChatScreen(),
20+
);
21+
}
22+
}
23+
24+
class ChatScreen extends StatefulWidget {
25+
@override
26+
State createState() => new ChatScreenState();
27+
}
28+
29+
class ChatScreenState extends State<ChatScreen> {
30+
@override
31+
Widget build(BuildContext context) {
32+
return Column(
33+
children: <Widget>[
34+
35+
],
36+
);
37+
}
38+
}

lib/const.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import 'dart:ui';
22

33
final themeColor = new Color(0xfff5a623);
44
final primaryColor = new Color(0xff203152);
5-
final greyColor = new Color(0xffaeaeae);
5+
final greyColor = new Color(0xffaeaeae);

lib/login.dart

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter_chat_demo/main.dart';
66
import 'package:google_sign_in/google_sign_in.dart';
77
import 'package:cloud_firestore/cloud_firestore.dart';
88
import 'package:fluttertoast/fluttertoast.dart';
9+
import 'package:shared_preferences/shared_preferences.dart';
910

1011
void main() => runApp(new MyApp());
1112

@@ -35,40 +36,59 @@ class LoginScreen extends StatefulWidget {
3536
class LoginScreenState extends State<LoginScreen> {
3637
final GoogleSignIn googleSignIn = new GoogleSignIn();
3738
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
39+
SharedPreferences prefs;
3840

3941
bool isLoading = false;
42+
FirebaseUser currentUser;
4043

4144
Future<Null> handleSignIn() async {
42-
this.setState(() {
43-
isLoading = true;
44-
});
45-
GoogleSignInAccount googleUser = await googleSignIn.signIn();
46-
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
47-
FirebaseUser firebaseUser = await firebaseAuth.signInWithGoogle(
48-
accessToken: googleAuth.accessToken,
49-
idToken: googleAuth.idToken,
50-
);
51-
if (firebaseUser != null) {
52-
// Update data to server
53-
Firestore.instance
54-
.collection('users')
55-
.document(firebaseUser.uid)
56-
.setData({'displayName': firebaseUser.displayName, 'photoUrl': firebaseUser.photoUrl});
57-
58-
Fluttertoast.showToast(msg: "Sign in success");
59-
this.setState(() {
60-
isLoading = false;
61-
});
45+
prefs = await SharedPreferences.getInstance();
6246

47+
if (prefs.getString('id') != null) {
6348
Navigator.push(context, MaterialPageRoute(builder: (context) => MainScreen()));
6449
} else {
65-
Fluttertoast.showToast(msg: "Sign in fail");
6650
this.setState(() {
67-
isLoading = false;
51+
isLoading = true;
6852
});
53+
54+
GoogleSignInAccount googleUser = await googleSignIn.signIn();
55+
GoogleSignInAuthentication googleAuth = await googleUser.authentication;
56+
FirebaseUser firebaseUser = await firebaseAuth.signInWithGoogle(
57+
accessToken: googleAuth.accessToken,
58+
idToken: googleAuth.idToken,
59+
);
60+
if (firebaseUser != null) {
61+
// Update data to local
62+
currentUser = firebaseUser;
63+
saveLocal();
64+
65+
// Update data to server
66+
Firestore.instance
67+
.collection('users')
68+
.document(firebaseUser.uid)
69+
.setData({'nickname': firebaseUser.displayName, 'photoUrl': firebaseUser.photoUrl});
70+
71+
Fluttertoast.showToast(msg: "Sign in success");
72+
this.setState(() {
73+
isLoading = false;
74+
});
75+
76+
Navigator.push(context, MaterialPageRoute(builder: (context) => MainScreen()));
77+
} else {
78+
Fluttertoast.showToast(msg: "Sign in fail");
79+
this.setState(() {
80+
isLoading = false;
81+
});
82+
}
6983
}
7084
}
7185

86+
void saveLocal() async {
87+
await prefs.setString('id', currentUser.uid);
88+
await prefs.setString('nickname', currentUser.displayName);
89+
await prefs.setString('photoUrl', currentUser.photoUrl);
90+
}
91+
7292
@override
7393
Widget build(BuildContext context) {
7494
return Scaffold(
@@ -90,6 +110,7 @@ class LoginScreenState extends State<LoginScreen> {
90110
),
91111
color: Color(0xffdd4b39),
92112
highlightColor: Color(0xffff7f7f),
113+
splashColor: Colors.transparent,
93114
textColor: Colors.white,
94115
padding: EdgeInsets.fromLTRB(30.0, 15.0, 30.0, 15.0)),
95116
),

lib/main.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:cloud_firestore/cloud_firestore.dart';
55
import 'package:flutter_chat_demo/const.dart';
66
import 'package:flutter_chat_demo/settings.dart';
77
import 'package:google_sign_in/google_sign_in.dart';
8+
import 'package:flutter_chat_demo/chat.dart';
89

910
class Choice {
1011
const Choice({this.title, this.icon});
@@ -133,12 +134,14 @@ class MainScreenState extends State<MainScreen> {
133134
),
134135
),
135136
Container(
136-
child: Text(document['displayName']),
137+
child: Text(document['nickname']),
137138
margin: EdgeInsets.only(left: 20.0),
138139
),
139140
],
140141
),
141-
onPressed: () {},
142+
onPressed: () {
143+
Navigator.push(context, new MaterialPageRoute(builder: (context) => new Chat()));
144+
},
142145
color: Colors.grey.withOpacity(0.1),
143146
padding: EdgeInsets.fromLTRB(20.0, 8.0, 20.0, 8.0),
144147
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),

lib/settings.dart

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import 'dart:async';
22
import 'dart:io';
3-
import 'package:flutter/material.dart';
3+
44
import 'package:cloud_firestore/cloud_firestore.dart';
5+
import 'package:firebase_auth/firebase_auth.dart';
6+
import 'package:flutter/material.dart';
57
import 'package:flutter_chat_demo/const.dart';
68
import 'package:image_picker/image_picker.dart';
9+
import 'package:shared_preferences/shared_preferences.dart';
710

811
class Settings extends StatelessWidget {
912
@override
@@ -27,9 +30,36 @@ class SettingsScreen extends StatefulWidget {
2730
}
2831

2932
class SettingsScreenState extends State<SettingsScreen> {
33+
TextEditingController controllerNickname;
34+
TextEditingController controllerAboutMe;
35+
36+
SharedPreferences prefs;
37+
38+
String id = '';
39+
String nickname = '';
40+
String aboutMe = '';
41+
3042
bool isLoading = false;
3143
File avatarImageFile;
3244

45+
@override
46+
void initState() {
47+
readLocal();
48+
}
49+
50+
void readLocal() async {
51+
prefs = await SharedPreferences.getInstance();
52+
id = prefs.getString('id') ?? '';
53+
nickname = prefs.getString('nickname') ?? '';
54+
aboutMe = prefs.getString('aboutMe') ?? '';
55+
56+
controllerNickname = new TextEditingController(text: nickname);
57+
controllerAboutMe = new TextEditingController(text: aboutMe);
58+
59+
// Force refresh input
60+
setState(() {});
61+
}
62+
3363
Future getImage() async {
3464
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
3565

@@ -40,13 +70,21 @@ class SettingsScreenState extends State<SettingsScreen> {
4070
}
4171
}
4272

73+
void handleUpdateData() async {
74+
Firestore.instance.collection('users').document(id).updateData({'nickname': nickname, 'aboutMe': aboutMe});
75+
76+
await prefs.setString('nickname', nickname);
77+
await prefs.setString('aboutMe', aboutMe);
78+
}
79+
4380
@override
4481
Widget build(BuildContext context) {
4582
return Stack(
4683
children: <Widget>[
4784
SingleChildScrollView(
4885
child: Column(
4986
children: <Widget>[
87+
// Avatar
5088
Container(
5189
child: Center(
5290
child: Stack(
@@ -83,23 +121,32 @@ class SettingsScreenState extends State<SettingsScreen> {
83121
width: double.infinity,
84122
margin: EdgeInsets.all(20.0),
85123
),
124+
125+
// Input
86126
Column(
87127
children: <Widget>[
88128
// Username
89129
Container(
90130
child: Text(
91131
'Nickname',
92-
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, color: primaryColor),
132+
style: TextStyle(fontStyle: FontStyle.italic, color: primaryColor),
93133
),
94134
margin: EdgeInsets.only(left: 10.0, bottom: 5.0, top: 10.0),
95135
),
96136
Container(
97-
child: TextFormField(
98-
decoration: InputDecoration(
137+
child: Theme(
138+
data: Theme.of(context).copyWith(primaryColor: primaryColor),
139+
child: TextField(
140+
decoration: InputDecoration(
99141
hintText: 'Sweetie',
100-
border: UnderlineInputBorder(),
101142
contentPadding: new EdgeInsets.all(5.0),
102-
hintStyle: TextStyle(color: greyColor)),
143+
hintStyle: TextStyle(color: greyColor),
144+
),
145+
controller: controllerNickname,
146+
onChanged: (value) {
147+
nickname = value;
148+
},
149+
),
103150
),
104151
margin: EdgeInsets.only(left: 30.0, right: 30.0),
105152
),
@@ -108,26 +155,50 @@ class SettingsScreenState extends State<SettingsScreen> {
108155
Container(
109156
child: Text(
110157
'About me',
111-
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, color: primaryColor),
158+
style: TextStyle(fontStyle: FontStyle.italic, color: primaryColor),
112159
),
113160
margin: EdgeInsets.only(left: 10.0, top: 30.0, bottom: 5.0),
114161
),
115162
Container(
116-
child: TextFormField(
117-
decoration: InputDecoration(
163+
child: Theme(
164+
data: Theme.of(context).copyWith(primaryColor: primaryColor),
165+
child: TextField(
166+
decoration: InputDecoration(
118167
hintText: 'Fun, like travel and play PES...',
119-
border: UnderlineInputBorder(),
120168
contentPadding: EdgeInsets.all(5.0),
121-
hintStyle: TextStyle(color: greyColor)),
169+
hintStyle: TextStyle(color: greyColor),
170+
),
171+
controller: controllerAboutMe,
172+
onChanged: (value) {
173+
aboutMe = value;
174+
},
175+
),
122176
),
123177
margin: EdgeInsets.only(left: 30.0, right: 30.0),
124178
),
125179
],
126180
crossAxisAlignment: CrossAxisAlignment.start,
127-
)
181+
),
182+
183+
// Button
184+
Container(
185+
child: FlatButton(
186+
onPressed: handleUpdateData,
187+
child: Text(
188+
'UPDATE',
189+
style: TextStyle(fontSize: 16.0),
190+
),
191+
color: primaryColor,
192+
highlightColor: new Color(0xff8d93a0),
193+
splashColor: Colors.transparent,
194+
textColor: Colors.white,
195+
padding: EdgeInsets.fromLTRB(30.0, 10.0, 30.0, 10.0),
196+
),
197+
margin: EdgeInsets.only(top: 50.0, bottom: 50.0),
198+
),
128199
],
129200
),
130-
padding: EdgeInsets.only(bottom: 20.0),
201+
padding: EdgeInsets.only(left: 15.0, right: 15.0),
131202
)
132203
],
133204
);

pubspec.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ packages:
277277
url: "https://pub.dartlang.org"
278278
source: hosted
279279
version: "0.29.0+1"
280+
shared_preferences:
281+
dependency: "direct main"
282+
description:
283+
name: shared_preferences
284+
url: "https://pub.dartlang.org"
285+
source: hosted
286+
version: "0.4.2"
280287
shelf:
281288
dependency: transitive
282289
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies:
1313
cloud_firestore: ^0.7.4
1414
fluttertoast: ^2.0.7
1515
image_picker: ^0.4.6
16+
shared_preferences: ^0.4.2
1617

1718
dev_dependencies:
1819
flutter_test:

0 commit comments

Comments
 (0)