Guest User

Untitled

a guest
Feb 2nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. dependencies:
  2. http: ^0.12.0
  3. flutter:
  4. sdk: flutter
  5. location: ^1.4.1
  6. firebase_core: ^0.2.5
  7. firebase_auth: ^0.5.20
  8. google_sign_in: ^3.2.2
  9.  
  10. import 'package:flutter/material.dart';
  11. import 'package:logining/home_screen/home_screen.dart';
  12. import 'package:firebase_auth/firebase_auth.dart';
  13. import 'package:google_sign_in/google_sign_in.dart';
  14.  
  15.  
  16. class LoginScreen extends StatefulWidget {
  17. @override
  18. LoginScreenState createState() {
  19. return new LoginScreenState();
  20. }
  21. }
  22.  
  23. class LoginScreenState extends State<LoginScreen> {
  24. String _email, _password;
  25. bool _obscureText = true;
  26.  
  27.  
  28.  
  29.  
  30. final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  31. final FirebaseAuth _auth = FirebaseAuth.instance;
  32. final GoogleSignIn googleSignIn = GoogleSignIn();
  33.  
  34. Future<FirebaseUser> _signInGoogle() async{
  35. GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
  36. GoogleSignInAuthentication gSa =await googleSignInAccount.authentication;
  37.  
  38. FirebaseUser user = await _auth.signInWithGoogle(
  39. idToken: gSa.idToken,
  40. accessToken: gSa.accessToken
  41. );
  42. print('User Name : ${user.displayName}');
  43. return user;
  44. }
  45.  
  46. @override
  47. Widget build(BuildContext context) {
  48. return MaterialApp(
  49. home: Scaffold(
  50. appBar: AppBar(
  51. title: Text('Login'),
  52. ),
  53. resizeToAvoidBottomPadding: false,
  54. body: ListView(children: <Widget>[
  55. Container(
  56. child: Form(
  57. key: _formKey,
  58. autovalidate: true,
  59. child: Column(
  60. children: <Widget>[
  61. SizedBox(
  62. height: 50,
  63. ),
  64. Padding(
  65. child: Image.asset(
  66. 'images/logo.png',
  67. width: 100.0,
  68. height: 100.0,
  69. ),
  70. padding: EdgeInsets.fromLTRB(50, 0, 50, 40),
  71. ),
  72. Padding(
  73. padding: EdgeInsets.fromLTRB(25, 0, 50, 10),
  74. child: TextFormField(
  75. validator: (email) {
  76. if (email.isEmpty) {
  77. return 'Provide an Email';
  78. }
  79. },
  80. style: TextStyle(
  81. color: Color(0xFF01579B),
  82. fontSize: 18.0,
  83. ),
  84. decoration: InputDecoration(
  85. border: OutlineInputBorder(
  86. borderRadius: BorderRadius.circular(30),
  87. ),
  88. hintText: 'Enter your Email',
  89. labelText: "Email",
  90. icon: Padding(
  91. padding: EdgeInsets.only(top: 20.0),
  92. child: Icon(Icons.email),
  93. )),
  94. onSaved: (email) => _email = email,
  95. ),
  96. ),
  97. Padding(
  98. padding: EdgeInsets.fromLTRB(25, 0, 50, 10),
  99. child: TextFormField(
  100. validator: (password) {
  101. if (password.isEmpty) {
  102. return 'Provide an password';
  103. }
  104. },
  105. obscureText: _obscureText,
  106. style: TextStyle(
  107. color: Color(0xFF01579B),
  108. fontSize: 18.0,
  109. ),
  110. decoration: InputDecoration(
  111. border: OutlineInputBorder(
  112. borderRadius: BorderRadius.circular(30),
  113. ),
  114. hintText: 'Enter your password',
  115. labelText: "Password",
  116. icon: Padding(
  117. padding: EdgeInsets.only(top: 15.0),
  118. child: Icon(Icons.lock),
  119. ),
  120. suffixIcon: GestureDetector(
  121. onTap: () {
  122. setState(() {
  123. _obscureText = !_obscureText;
  124. });
  125. },
  126. child: Icon(
  127. _obscureText
  128. ? Icons.visibility
  129. : Icons.visibility_off,
  130. semanticLabel: _obscureText
  131. ? 'show password'
  132. : 'hide password',
  133. ),
  134. ),
  135. ),
  136. onSaved: (password) => _password = password,
  137. ),
  138. ),
  139. Row(
  140. mainAxisAlignment: MainAxisAlignment.center,
  141. children: <Widget>[
  142. Padding(
  143. padding: EdgeInsets.symmetric(horizontal: 10),
  144. child: RaisedButton(
  145. color: Color(0xFFD50000),
  146. textColor: Color(0xFFFFFFFF),
  147. child: Text('Login with Google'),
  148. onPressed:(){ _signInGoogle().then((FirebaseUser user){
  149. print(user);
  150. }).catchError((onError){
  151. print(onError);
  152. });
  153.  
  154. }
  155. ),
  156.  
  157. ),
  158. Padding(
  159. padding: EdgeInsets.symmetric(horizontal: 10),
  160. child: RaisedButton(
  161. color: Color(0xFF448AFF),
  162. textColor: Color(0xFFFFFFFF),
  163. child: Text('Login'),
  164. onPressed: signIn,
  165. ),
  166. ),
  167. ]),
  168. Padding(
  169. padding:
  170. EdgeInsets.symmetric(horizontal: 10, vertical: 5),
  171. child: FlatButton(
  172. textColor: Color(0xFF448AFF),
  173. child: Text('Forgot Password'),
  174. onPressed: () {
  175. print('onPressed');
  176. },
  177. ),
  178. ),
  179. Row(
  180. mainAxisAlignment: MainAxisAlignment.center,
  181. children: <Widget>[
  182. Padding(
  183. padding: EdgeInsets.fromLTRB(50, 70, 0, 10),
  184. child: Text(
  185. 'Still do not have an account ',
  186. style: TextStyle(color: Color(0xFF9E9E9E)),
  187. ),
  188. ),
  189. Padding(
  190. padding: EdgeInsets.fromLTRB(0, 70, 30, 10),
  191. child: FlatButton(
  192. textColor: Color(0xFF448AFF),
  193. child: Text('registration'),
  194. onPressed: () {
  195. Navigator.of(context).pushNamedAndRemoveUntil(
  196. '/registration', (Route<dynamic> route) => false);
  197. },
  198. ),
  199. ),
  200. ],
  201. ),
  202. ],
  203. ),
  204. ),
  205. ),
  206. ]),
  207. ),
  208. );
  209. }
  210.  
  211. void signIn() async {
  212. if(_formKey.currentState.validate()){
  213. _formKey.currentState.save();
  214. try{
  215. FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
  216. Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen(user: user)));
  217. }catch(e){
  218. print(e.message);
  219. }
  220. }
  221. }
  222.  
  223. }
Add Comment
Please, Sign In to add comment