Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:decodesshoping/auth.dart'ك
- import 'package:decodesshoping/page.dart';
- import 'package:flutter/material.dart';
- import 'package:firebase_database/firebase_database.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:google_sign_in/google_sign_in.dart';
- final FirebaseAuth _auth = FirebaseAuth.instance;
- final GoogleSignIn _googleSignIn = new GoogleSignIn();
- Future<String> _testSignInWithGoogle() async {
- final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
- final GoogleSignInAuthentication googleAuth =
- await googleUser.authentication;
- final FirebaseUser user = await _auth.signInWithGoogle(
- accessToken: googleAuth.accessToken,
- idToken: googleAuth.idToken,
- );
- assert(user.email != null);
- assert(user.displayName != null);
- assert(!user.isAnonymous);
- assert(await user.getIdToken() != null);
- final FirebaseUser currentUser = await _auth.currentUser();
- assert(user.uid == currentUser.uid);
- return 'signInWithGoogle succeeded: $user';
- }
- class LoginPage extends StatefulWidget {
- LoginPage({this.auth,this.onSignedIn});
- final BaseAuth auth;
- final VoidCallback onSignedIn;
- @override
- _LoginPageState createState() => _LoginPageState();
- }
- enum FormType{
- login,
- register
- }
- class _LoginPageState extends State<LoginPage> {
- Future<Null> ProduitDone() async {
- return showDialog<Null>(
- context: context,
- barrierDismissible: false, // user must tap button!
- builder: (BuildContext context) {
- return new AlertDialog(
- title: new Text('Bravo ,bon travail'),
- content: new SingleChildScrollView(
- child: new ListBody(
- children: <Widget>[
- new Text(' votre compte a été enregistré avec succès, vous pouvez vous connecter maintenant'),
- ],
- ),
- ),
- actions: <Widget>[
- new FlatButton(
- child: new Text('Sortir', style: TextStyle(color: Colors.redAccent)),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ],
- );
- },
- );
- }
- FormType _formType = FormType.login;
- final formKey = new GlobalKey<FormState>();
- final mainKey = new GlobalKey<ScaffoldState>();
- bool validateAndSave()
- {
- final form = formKey.currentState;
- if(form.validate())
- {
- form.save();
- return true;
- } return false;
- }
- void validateAndSubmit() async {
- if(validateAndSave()) {
- try {
- if(_formType == FormType.login)
- {
- await firebaseAuth
- .signInWithEmailAndPassword(email: user, password: pass)
- .then((FirebaseUser user) {
- print('****${user.isEmailVerified}');
- }).catchError((e) => print('password or email false');'));
- Firestore.instance
- .runTransaction((Transaction transaction) async {
- DocumentReference reference =
- Firestore.instance.collection('users').document(_email);
- await reference
- .setData({"email":_usernameController.text });
- _usernameController.clear();
- _passwordController.clear();
- });
- var snackbar = SnackBar(
- content: Text('Se connecter avec succès'),
- duration: Duration(milliseconds: 2000),
- );
- mainKey.currentState.showSnackBar(snackbar);
- }
- else {
- String userId= await widget.auth.createUserWithEmailAndPassword(_email, _password);
- FirebaseUser user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: _email, password: _password);
- print('Registered User: $userId');
- ProduitDone();
- /* var snackbar = SnackBar(
- content: Text('enregistré avec succès'),
- duration: Duration(milliseconds: 2000),
- );
- mainKey.currentState.showSnackBar(snackbar); */
- }
- widget.onSignedIn();
- }
- catch(e) {
- print('error: $e');
- }
- }
- }
- final _usernameController = TextEditingController();
- final _passwordController = TextEditingController();
- /* final _phoneeController = TextEditingController();
- final _adressController = TextEditingController();
- final _cityController = TextEditingController();
- @override
- void dispose()
- {
- _phoneeController.dispose();
- _adressController.dispose();
- _cityController.dispose();
- super.dispose();
- } */
- String _email;
- String _password;
- void moveToRegister()
- {
- formKey.currentState.reset();
- setState ((){
- _formType = FormType.register;
- });
- }
- void moveToLogin()
- {
- formKey.currentState.reset();
- setState ((){
- _formType = FormType.login;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: mainKey,
- body: SafeArea(
- child: new Form(
- key: formKey,
- child: ListView(
- padding: EdgeInsets.symmetric(horizontal: 24.0),
- children: <Widget>[
- SizedBox(height: 60.0),
- Column(
- children: <Widget>[
- Image.asset('assets/er.png', height: 190.0, width: 190.0),
- Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: buildInputs()+ buildSubmitButtons(),
- )
- ],
- )
- ],
- ),
- ),
- ),
- );
- }
- List<Widget> buildInputs(){
- if(_formType == FormType.login)
- {
- return [
- new TextFormField(
- controller: _usernameController,
- decoration: InputDecoration(
- labelText: 'Email',
- ),
- validator: (value) => value.isEmpty ?'Email can\'t be empty':null,
- onSaved:(value) => _email = value,
- ),
- new TextFormField(
- controller: _passwordController,
- decoration: InputDecoration(
- labelText: 'Password',
- ),
- validator: (value) {
- if(value.isEmpty)
- return 'Password can\'t be empty';
- else if(value.length < 6)
- return 'Password too short.';
- },
- obscureText: true,
- onSaved:(value) => _password = value,
- ),
- ];}
- else {
- return[
- new TextFormField(
- controller: _usernameController,
- decoration: InputDecoration(
- labelText: 'Email',
- ),
- validator: (value) {
- if(value.isEmpty)
- return value.isEmpty ?'Email can\'t be empty':null;
- else if(!value.contains('@'))
- {
- return 'Not valid email';
- }
- },
- onSaved:(value) => _email = value,
- ),
- new TextFormField(
- controller: _passwordController,
- decoration: InputDecoration(
- labelText: 'Password',
- ),
- validator: (value) {
- if(value.isEmpty)
- return 'Password can\'t be empty';
- else if(value.length < 6)
- return 'Password too short.';
- },
- obscureText: true,
- onSaved:(value) => _password = value,
- ),
- /* new TextFormField(
- controller: _phoneeController,
- decoration: InputDecoration(
- labelText: 'Phone',
- ),
- validator: (value) => value.isEmpty ?'Phone can\'t be empty':null,
- onSaved:(value) => _phoneeController.text = value,
- ),
- new TextFormField(
- controller: _adressController,
- decoration: InputDecoration(
- labelText: 'Adresse',
- ),
- validator: (value) => value.isEmpty ?'Email can\'t be empty':null,
- onSaved:(value) => _adressController.text = value,
- ),
- new TextFormField(
- controller: _cityController,
- decoration: InputDecoration(
- labelText: 'city',
- ),
- validator: (value) => value.isEmpty ?'City can\'t be empty':null,
- onSaved:(value) => _cityController.text = value,
- ), */
- ];
- }
- }
- List<Widget> buildSubmitButtons()
- {
- if(_formType == FormType.login)
- {
- return [
- SizedBox(height: 20.0),
- new RaisedButton(
- child: Text('Se connecter',
- style: new TextStyle(
- color: Colors.white),),
- elevation: 4.0,
- color: Colors.red[400],
- shape: BeveledRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(7.0)),
- ),
- onPressed: validateAndSubmit ,
- ),
- SizedBox(height: 10.0),
- new RaisedButton(
- child: Text('SIGN UP'),
- elevation: 4.0,
- shape: BeveledRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(7.0)),
- ),
- onPressed: moveToRegister,
- /*() {
- Navigator.of(context).pushNamed('/signup');
- }, */
- ),
- ];
- }
- else {
- return [
- SizedBox(height: 20.0),
- new RaisedButton(
- child: Text('Register',
- style: new TextStyle(
- color: Colors.white),),
- elevation: 8.0,
- color: Colors.red[400],
- shape: BeveledRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(7.0)),
- ),
- onPressed: validateAndSubmit ,
- //() {
- // Navigator.of(context).pushNamed('/homeP');
- // },
- ),
- SizedBox(height: 10.0),
- new RaisedButton(
- child: Text('Have an Account? login'),
- elevation: 8.0,
- shape: BeveledRectangleBorder(
- borderRadius: BorderRadius.all(Radius.circular(7.0)),
- ),
- onPressed: moveToLogin,
- /*() {
- Navigator.of(context).pushNamed('/signup');
- }, */
- ),
- ];
- }
- }
- }
- class PrimaryColorOverride extends StatelessWidget {
- const PrimaryColorOverride({Key key, this.color, this.child})
- : super(key: key);
- final Color color;
- final Widget child;
- @override
- Widget build(BuildContext context) {
- return Theme(
- child: child,
- data: Theme.of(context).copyWith(primaryColor: Colors.redAccent),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement