Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.57 KB | None | 0 0
  1. import 'dart:async';
  2.  
  3. import 'package:cloud_firestore/cloud_firestore.dart';
  4. import 'package:decodesshoping/auth.dart'ك
  5. import 'package:decodesshoping/page.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:firebase_database/firebase_database.dart';
  8. import 'package:firebase_auth/firebase_auth.dart';
  9. import 'package:google_sign_in/google_sign_in.dart';
  10.  
  11. final FirebaseAuth _auth = FirebaseAuth.instance;
  12. final GoogleSignIn _googleSignIn = new GoogleSignIn();
  13.  
  14. Future<String> _testSignInWithGoogle() async {
  15. final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  16. final GoogleSignInAuthentication googleAuth =
  17. await googleUser.authentication;
  18. final FirebaseUser user = await _auth.signInWithGoogle(
  19. accessToken: googleAuth.accessToken,
  20. idToken: googleAuth.idToken,
  21. );
  22.  
  23.  
  24. assert(user.email != null);
  25. assert(user.displayName != null);
  26. assert(!user.isAnonymous);
  27. assert(await user.getIdToken() != null);
  28.  
  29. final FirebaseUser currentUser = await _auth.currentUser();
  30. assert(user.uid == currentUser.uid);
  31.  
  32. return 'signInWithGoogle succeeded: $user';
  33.  
  34. }
  35.  
  36.  
  37. class LoginPage extends StatefulWidget {
  38. LoginPage({this.auth,this.onSignedIn});
  39. final BaseAuth auth;
  40. final VoidCallback onSignedIn;
  41. @override
  42. _LoginPageState createState() => _LoginPageState();
  43. }
  44. enum FormType{
  45. login,
  46. register
  47. }
  48. class _LoginPageState extends State<LoginPage> {
  49.  
  50. Future<Null> ProduitDone() async {
  51. return showDialog<Null>(
  52. context: context,
  53. barrierDismissible: false, // user must tap button!
  54. builder: (BuildContext context) {
  55. return new AlertDialog(
  56. title: new Text('Bravo ,bon travail'),
  57. content: new SingleChildScrollView(
  58. child: new ListBody(
  59. children: <Widget>[
  60. new Text(' votre compte a été enregistré avec succès, vous pouvez vous connecter maintenant'),
  61. ],
  62. ),
  63. ),
  64. actions: <Widget>[
  65.  
  66. new FlatButton(
  67. child: new Text('Sortir', style: TextStyle(color: Colors.redAccent)),
  68. onPressed: () {
  69. Navigator.of(context).pop();
  70. },
  71. ),
  72. ],
  73. );
  74. },
  75. );
  76. }
  77. FormType _formType = FormType.login;
  78. final formKey = new GlobalKey<FormState>();
  79. final mainKey = new GlobalKey<ScaffoldState>();
  80.  
  81. bool validateAndSave()
  82. {
  83. final form = formKey.currentState;
  84. if(form.validate())
  85. {
  86. form.save();
  87. return true;
  88. } return false;
  89. }
  90. void validateAndSubmit() async {
  91. if(validateAndSave()) {
  92. try {
  93. if(_formType == FormType.login)
  94. {
  95. await firebaseAuth
  96. .signInWithEmailAndPassword(email: user, password: pass)
  97. .then((FirebaseUser user) {
  98. print('****${user.isEmailVerified}');
  99. }).catchError((e) => print('password or email false');'));
  100.  
  101. Firestore.instance
  102. .runTransaction((Transaction transaction) async {
  103. DocumentReference reference =
  104. Firestore.instance.collection('users').document(_email);
  105. await reference
  106. .setData({"email":_usernameController.text });
  107. _usernameController.clear();
  108. _passwordController.clear();
  109.  
  110. });
  111. var snackbar = SnackBar(
  112. content: Text('Se connecter avec succès'),
  113. duration: Duration(milliseconds: 2000),
  114. );
  115. mainKey.currentState.showSnackBar(snackbar);
  116.  
  117. }
  118. else {
  119. String userId= await widget.auth.createUserWithEmailAndPassword(_email, _password);
  120. FirebaseUser user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
  121. print('Registered User: $userId');
  122. ProduitDone();
  123. /* var snackbar = SnackBar(
  124. content: Text('enregistré avec succès'),
  125. duration: Duration(milliseconds: 2000),
  126. );
  127. mainKey.currentState.showSnackBar(snackbar); */
  128.  
  129. }
  130. widget.onSignedIn();
  131. }
  132. catch(e) {
  133. print('error: $e');
  134. }
  135. }
  136.  
  137.  
  138. }
  139.  
  140. final _usernameController = TextEditingController();
  141. final _passwordController = TextEditingController();
  142. /* final _phoneeController = TextEditingController();
  143. final _adressController = TextEditingController();
  144. final _cityController = TextEditingController();
  145. @override
  146. void dispose()
  147. {
  148. _phoneeController.dispose();
  149. _adressController.dispose();
  150. _cityController.dispose();
  151. super.dispose();
  152. } */
  153. String _email;
  154. String _password;
  155. void moveToRegister()
  156. {
  157. formKey.currentState.reset();
  158. setState ((){
  159.  
  160. _formType = FormType.register;
  161. });
  162. }
  163. void moveToLogin()
  164. {
  165.  
  166. formKey.currentState.reset();
  167. setState ((){
  168. _formType = FormType.login;
  169. });
  170. }
  171. @override
  172. Widget build(BuildContext context) {
  173. return Scaffold(
  174. key: mainKey,
  175. body: SafeArea(
  176. child: new Form(
  177. key: formKey,
  178. child: ListView(
  179. padding: EdgeInsets.symmetric(horizontal: 24.0),
  180.  
  181. children: <Widget>[
  182. SizedBox(height: 60.0),
  183.  
  184. Column(
  185. children: <Widget>[
  186. Image.asset('assets/er.png', height: 190.0, width: 190.0),
  187. Column(
  188. crossAxisAlignment: CrossAxisAlignment.stretch,
  189. children: buildInputs()+ buildSubmitButtons(),
  190. )
  191.  
  192. ],
  193. )
  194.  
  195.  
  196. ],
  197. ),
  198. ),
  199. ),
  200. );
  201. }
  202. List<Widget> buildInputs(){
  203. if(_formType == FormType.login)
  204. {
  205. return [
  206. new TextFormField(
  207. controller: _usernameController,
  208. decoration: InputDecoration(
  209. labelText: 'Email',
  210. ),
  211. validator: (value) => value.isEmpty ?'Email can\'t be empty':null,
  212. onSaved:(value) => _email = value,
  213. ),
  214.  
  215. new TextFormField(
  216. controller: _passwordController,
  217. decoration: InputDecoration(
  218. labelText: 'Password',
  219. ),
  220. validator: (value) {
  221. if(value.isEmpty)
  222. return 'Password can\'t be empty';
  223. else if(value.length < 6)
  224. return 'Password too short.';
  225.  
  226. },
  227. obscureText: true,
  228. onSaved:(value) => _password = value,
  229. ),
  230.  
  231.  
  232. ];}
  233. else {
  234. return[
  235. new TextFormField(
  236. controller: _usernameController,
  237. decoration: InputDecoration(
  238. labelText: 'Email',
  239. ),
  240. validator: (value) {
  241. if(value.isEmpty)
  242. return value.isEmpty ?'Email can\'t be empty':null;
  243. else if(!value.contains('@'))
  244. {
  245. return 'Not valid email';
  246. }
  247. },
  248. onSaved:(value) => _email = value,
  249. ),
  250.  
  251. new TextFormField(
  252. controller: _passwordController,
  253. decoration: InputDecoration(
  254. labelText: 'Password',
  255. ),
  256. validator: (value) {
  257. if(value.isEmpty)
  258. return 'Password can\'t be empty';
  259. else if(value.length < 6)
  260. return 'Password too short.';
  261. },
  262. obscureText: true,
  263. onSaved:(value) => _password = value,
  264. ),
  265. /* new TextFormField(
  266. controller: _phoneeController,
  267. decoration: InputDecoration(
  268. labelText: 'Phone',
  269. ),
  270. validator: (value) => value.isEmpty ?'Phone can\'t be empty':null,
  271. onSaved:(value) => _phoneeController.text = value,
  272. ),
  273. new TextFormField(
  274. controller: _adressController,
  275. decoration: InputDecoration(
  276. labelText: 'Adresse',
  277. ),
  278. validator: (value) => value.isEmpty ?'Email can\'t be empty':null,
  279. onSaved:(value) => _adressController.text = value,
  280. ),
  281. new TextFormField(
  282. controller: _cityController,
  283. decoration: InputDecoration(
  284. labelText: 'city',
  285. ),
  286. validator: (value) => value.isEmpty ?'City can\'t be empty':null,
  287. onSaved:(value) => _cityController.text = value,
  288. ), */
  289. ];
  290. }
  291. }
  292. List<Widget> buildSubmitButtons()
  293. {
  294. if(_formType == FormType.login)
  295. {
  296.  
  297. return [
  298. SizedBox(height: 20.0),
  299. new RaisedButton(
  300.  
  301.  
  302. child: Text('Se connecter',
  303. style: new TextStyle(
  304. color: Colors.white),),
  305. elevation: 4.0,
  306. color: Colors.red[400],
  307. shape: BeveledRectangleBorder(
  308. borderRadius: BorderRadius.all(Radius.circular(7.0)),
  309. ),
  310. onPressed: validateAndSubmit ,
  311.  
  312. ),
  313. SizedBox(height: 10.0),
  314. new RaisedButton(
  315. child: Text('SIGN UP'),
  316. elevation: 4.0,
  317. shape: BeveledRectangleBorder(
  318. borderRadius: BorderRadius.all(Radius.circular(7.0)),
  319. ),
  320. onPressed: moveToRegister,
  321. /*() {
  322. Navigator.of(context).pushNamed('/signup');
  323. }, */
  324. ),
  325.  
  326.  
  327. ];
  328. }
  329. else {
  330. return [
  331. SizedBox(height: 20.0),
  332. new RaisedButton(
  333. child: Text('Register',
  334. style: new TextStyle(
  335. color: Colors.white),),
  336. elevation: 8.0,
  337. color: Colors.red[400],
  338. shape: BeveledRectangleBorder(
  339. borderRadius: BorderRadius.all(Radius.circular(7.0)),
  340. ),
  341. onPressed: validateAndSubmit ,
  342. //() {
  343. // Navigator.of(context).pushNamed('/homeP');
  344. // },
  345.  
  346. ),
  347. SizedBox(height: 10.0),
  348. new RaisedButton(
  349. child: Text('Have an Account? login'),
  350. elevation: 8.0,
  351. shape: BeveledRectangleBorder(
  352. borderRadius: BorderRadius.all(Radius.circular(7.0)),
  353. ),
  354. onPressed: moveToLogin,
  355. /*() {
  356. Navigator.of(context).pushNamed('/signup');
  357. }, */
  358. ),
  359. ];
  360. }
  361.  
  362. }
  363. }
  364.  
  365. class PrimaryColorOverride extends StatelessWidget {
  366. const PrimaryColorOverride({Key key, this.color, this.child})
  367. : super(key: key);
  368.  
  369. final Color color;
  370. final Widget child;
  371.  
  372. @override
  373. Widget build(BuildContext context) {
  374. return Theme(
  375. child: child,
  376. data: Theme.of(context).copyWith(primaryColor: Colors.redAccent),
  377. );
  378. }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement