Skip to content

Commit b54d90e

Browse files
committed
firebase auth part 03
1 parent 4b0cf29 commit b54d90e

File tree

3 files changed

+183
-48
lines changed

3 files changed

+183
-48
lines changed

lib/pages/HomePage.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class HomePage extends StatefulWidget {
4+
HomePage({Key key}) : super(key: key);
5+
6+
@override
7+
_HomePageState createState() => _HomePageState();
8+
}
9+
10+
class _HomePageState extends State<HomePage> {
11+
@override
12+
Widget build(BuildContext context) {
13+
return Scaffold();
14+
}
15+
}

lib/pages/SignInPage.dart

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import 'package:firebase_app_web/pages/SignUpPage.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_svg/flutter_svg.dart';
4+
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
5+
6+
import 'HomePage.dart';
37

48
class SignInPage extends StatefulWidget {
59
SignInPage({Key key}) : super(key: key);
@@ -9,6 +13,11 @@ class SignInPage extends StatefulWidget {
913
}
1014

1115
class _SignInPageState extends State<SignInPage> {
16+
firebase_auth.FirebaseAuth firebaseAuth = firebase_auth.FirebaseAuth.instance;
17+
TextEditingController _emailController = TextEditingController();
18+
TextEditingController _pwdController = TextEditingController();
19+
bool circular = false;
20+
1221
@override
1322
Widget build(BuildContext context) {
1423
return Scaffold(
@@ -46,11 +55,11 @@ class _SignInPageState extends State<SignInPage> {
4655
SizedBox(
4756
height: 18,
4857
),
49-
textItem("Email...."),
58+
textItem("Email....", _emailController, false),
5059
SizedBox(
5160
height: 15,
5261
),
53-
textItem("Password..."),
62+
textItem("Password...", _pwdController, true),
5463
SizedBox(
5564
height: 40,
5665
),
@@ -68,12 +77,20 @@ class _SignInPageState extends State<SignInPage> {
6877
fontSize: 16,
6978
),
7079
),
71-
Text(
72-
"SignUp",
73-
style: TextStyle(
74-
color: Colors.white,
75-
fontSize: 16,
76-
fontWeight: FontWeight.w600,
80+
InkWell(
81+
onTap: () {
82+
Navigator.pushAndRemoveUntil(
83+
context,
84+
MaterialPageRoute(builder: (builder) => SignUpPage()),
85+
(route) => false);
86+
},
87+
child: Text(
88+
"SignUp",
89+
style: TextStyle(
90+
color: Colors.white,
91+
fontSize: 16,
92+
fontWeight: FontWeight.w600,
93+
),
7794
),
7895
),
7996
],
@@ -97,21 +114,49 @@ class _SignInPageState extends State<SignInPage> {
97114
}
98115

99116
Widget colorButton() {
100-
return Container(
101-
width: MediaQuery.of(context).size.width - 100,
102-
height: 60,
103-
decoration: BoxDecoration(
104-
borderRadius: BorderRadius.circular(20),
105-
gradient: LinearGradient(
106-
colors: [Color(0xfffd746c), Color(0xffff9068), Color(0xfffd746c)]),
107-
),
108-
child: Center(
109-
child: Text(
110-
"Sign In",
111-
style: TextStyle(
112-
color: Colors.white,
113-
fontSize: 20,
114-
),
117+
return InkWell(
118+
onTap: () async {
119+
try {
120+
firebase_auth.UserCredential userCredential =
121+
await firebaseAuth.signInWithEmailAndPassword(
122+
email: _emailController.text, password: _pwdController.text);
123+
print(userCredential.user.email);
124+
setState(() {
125+
circular = false;
126+
});
127+
Navigator.pushAndRemoveUntil(
128+
context,
129+
MaterialPageRoute(builder: (builder) => HomePage()),
130+
(route) => false);
131+
} catch (e) {
132+
final snackbar = SnackBar(content: Text(e.toString()));
133+
ScaffoldMessenger.of(context).showSnackBar(snackbar);
134+
setState(() {
135+
circular = false;
136+
});
137+
}
138+
},
139+
child: Container(
140+
width: MediaQuery.of(context).size.width - 100,
141+
height: 60,
142+
decoration: BoxDecoration(
143+
borderRadius: BorderRadius.circular(20),
144+
gradient: LinearGradient(colors: [
145+
Color(0xfffd746c),
146+
Color(0xffff9068),
147+
Color(0xfffd746c)
148+
]),
149+
),
150+
child: Center(
151+
child: circular
152+
? CircularProgressIndicator()
153+
: Text(
154+
"Sign In",
155+
style: TextStyle(
156+
color: Colors.white,
157+
fontSize: 20,
158+
),
159+
),
115160
),
116161
),
117162
);
@@ -155,17 +200,31 @@ class _SignInPageState extends State<SignInPage> {
155200
);
156201
}
157202

158-
Widget textItem(String labeltext) {
203+
Widget textItem(
204+
String labeltext, TextEditingController controller, bool obscureText) {
159205
return Container(
160206
width: MediaQuery.of(context).size.width - 70,
161207
height: 55,
162208
child: TextFormField(
209+
controller: controller,
210+
obscureText: obscureText,
211+
style: TextStyle(
212+
fontSize: 17,
213+
color: Colors.white,
214+
),
163215
decoration: InputDecoration(
164216
labelText: labeltext,
165217
labelStyle: TextStyle(
166218
fontSize: 17,
167219
color: Colors.white,
168220
),
221+
focusedBorder: OutlineInputBorder(
222+
borderRadius: BorderRadius.circular(15),
223+
borderSide: BorderSide(
224+
width: 1.5,
225+
color: Colors.amber,
226+
),
227+
),
169228
enabledBorder: OutlineInputBorder(
170229
borderRadius: BorderRadius.circular(15),
171230
borderSide: BorderSide(

lib/pages/SignUpPage.dart

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import 'package:firebase_app_web/pages/HomePage.dart';
2+
import 'package:firebase_app_web/pages/SignInPage.dart';
13
import 'package:flutter/material.dart';
24
import 'package:flutter_svg/flutter_svg.dart';
5+
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
36

47
class SignUpPage extends StatefulWidget {
58
SignUpPage({Key key}) : super(key: key);
@@ -9,6 +12,11 @@ class SignUpPage extends StatefulWidget {
912
}
1013

1114
class _SignUpPageState extends State<SignUpPage> {
15+
firebase_auth.FirebaseAuth firebaseAuth = firebase_auth.FirebaseAuth.instance;
16+
TextEditingController _emailController = TextEditingController();
17+
TextEditingController _pwdController = TextEditingController();
18+
bool circular = false;
19+
1220
@override
1321
Widget build(BuildContext context) {
1422
return Scaffold(
@@ -46,11 +54,11 @@ class _SignUpPageState extends State<SignUpPage> {
4654
SizedBox(
4755
height: 18,
4856
),
49-
textItem("Email...."),
57+
textItem("Email....", _emailController, false),
5058
SizedBox(
5159
height: 15,
5260
),
53-
textItem("Password..."),
61+
textItem("Password...", _pwdController, true),
5462
SizedBox(
5563
height: 40,
5664
),
@@ -68,12 +76,20 @@ class _SignUpPageState extends State<SignUpPage> {
6876
fontSize: 16,
6977
),
7078
),
71-
Text(
72-
"Login",
73-
style: TextStyle(
74-
color: Colors.white,
75-
fontSize: 16,
76-
fontWeight: FontWeight.w600,
79+
InkWell(
80+
onTap: () {
81+
Navigator.pushAndRemoveUntil(
82+
context,
83+
MaterialPageRoute(builder: (builder) => SignInPage()),
84+
(route) => false);
85+
},
86+
child: Text(
87+
"Login",
88+
style: TextStyle(
89+
color: Colors.white,
90+
fontSize: 16,
91+
fontWeight: FontWeight.w600,
92+
),
7793
),
7894
),
7995
],
@@ -86,21 +102,52 @@ class _SignUpPageState extends State<SignUpPage> {
86102
}
87103

88104
Widget colorButton() {
89-
return Container(
90-
width: MediaQuery.of(context).size.width - 100,
91-
height: 60,
92-
decoration: BoxDecoration(
93-
borderRadius: BorderRadius.circular(20),
94-
gradient: LinearGradient(
95-
colors: [Color(0xfffd746c), Color(0xffff9068), Color(0xfffd746c)]),
96-
),
97-
child: Center(
98-
child: Text(
99-
"Sign Up",
100-
style: TextStyle(
101-
color: Colors.white,
102-
fontSize: 20,
103-
),
105+
return InkWell(
106+
onTap: () async {
107+
setState(() {
108+
circular = true;
109+
});
110+
try {
111+
firebase_auth.UserCredential userCredential =
112+
await firebaseAuth.createUserWithEmailAndPassword(
113+
email: _emailController.text, password: _pwdController.text);
114+
print(userCredential.user.email);
115+
setState(() {
116+
circular = false;
117+
});
118+
Navigator.pushAndRemoveUntil(
119+
context,
120+
MaterialPageRoute(builder: (builder) => HomePage()),
121+
(route) => false);
122+
} catch (e) {
123+
final snackbar = SnackBar(content: Text(e.toString()));
124+
ScaffoldMessenger.of(context).showSnackBar(snackbar);
125+
setState(() {
126+
circular = false;
127+
});
128+
}
129+
},
130+
child: Container(
131+
width: MediaQuery.of(context).size.width - 100,
132+
height: 60,
133+
decoration: BoxDecoration(
134+
borderRadius: BorderRadius.circular(20),
135+
gradient: LinearGradient(colors: [
136+
Color(0xfffd746c),
137+
Color(0xffff9068),
138+
Color(0xfffd746c)
139+
]),
140+
),
141+
child: Center(
142+
child: circular
143+
? CircularProgressIndicator()
144+
: Text(
145+
"Sign Up",
146+
style: TextStyle(
147+
color: Colors.white,
148+
fontSize: 20,
149+
),
150+
),
104151
),
105152
),
106153
);
@@ -144,17 +191,31 @@ class _SignUpPageState extends State<SignUpPage> {
144191
);
145192
}
146193

147-
Widget textItem(String labeltext) {
194+
Widget textItem(
195+
String labeltext, TextEditingController controller, bool obscureText) {
148196
return Container(
149197
width: MediaQuery.of(context).size.width - 70,
150198
height: 55,
151199
child: TextFormField(
200+
controller: controller,
201+
obscureText: obscureText,
202+
style: TextStyle(
203+
fontSize: 17,
204+
color: Colors.white,
205+
),
152206
decoration: InputDecoration(
153207
labelText: labeltext,
154208
labelStyle: TextStyle(
155209
fontSize: 17,
156210
color: Colors.white,
157211
),
212+
focusedBorder: OutlineInputBorder(
213+
borderRadius: BorderRadius.circular(15),
214+
borderSide: BorderSide(
215+
width: 1.5,
216+
color: Colors.amber,
217+
),
218+
),
158219
enabledBorder: OutlineInputBorder(
159220
borderRadius: BorderRadius.circular(15),
160221
borderSide: BorderSide(

0 commit comments

Comments
 (0)