From 7eb5103c91f6d82c61f4a0bde72de26039f352c6 Mon Sep 17 00:00:00 2001 From: suresha9 Date: Thu, 8 Dec 2022 13:41:36 +0000 Subject: [PATCH] Authentication and validation --- expense_tracker/lib/Components/navbar.dart | 3 --- expense_tracker/lib/Screens/Login.dart | 24 +++++++++++++------- expense_tracker/lib/Screens/addexpenses.dart | 6 +++++ expense_tracker/lib/Screens/profile.dart | 11 ++++++++- expense_tracker/lib/Screens/register.dart | 21 ++++------------- expense_tracker/lib/main.dart | 3 --- 6 files changed, 37 insertions(+), 31 deletions(-) diff --git a/expense_tracker/lib/Components/navbar.dart b/expense_tracker/lib/Components/navbar.dart index 6893dbd..efa1798 100644 --- a/expense_tracker/lib/Components/navbar.dart +++ b/expense_tracker/lib/Components/navbar.dart @@ -83,9 +83,6 @@ class DesktopNavbar extends StatelessWidget { "Profile", style: TextStyle(color: Colors.white), ), - const Text( - "Profile", - style: TextStyle(color: Colors.white), ), const SizedBox( width: 30, diff --git a/expense_tracker/lib/Screens/Login.dart b/expense_tracker/lib/Screens/Login.dart index 3dd79be..4f000f3 100644 --- a/expense_tracker/lib/Screens/Login.dart +++ b/expense_tracker/lib/Screens/Login.dart @@ -1,9 +1,8 @@ -import 'package:expense_tracker/Screens/myhomepage.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'ForgotPasswordPage.dart'; - +import 'package:email_validator/email_validator.dart'; import '../main.dart'; // import 'package:font_awesome_flutter/font_awesome_flutter.dart'; @@ -80,42 +79,51 @@ class LoginState extends State { const SizedBox(height: 20), Container( width: 250, - child: TextField( + child: TextFormField( controller: emailController, decoration: InputDecoration( labelText: 'Email Address', - // suffixIcon: Icon(FontAwesomeIcons.envelope), ), + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (email) => + !(email != null && EmailValidator.validate(email)) + ? 'Enter a valid Email' + : null, ), ), const SizedBox(height: 20), Container( width: 250, - child: TextField( + child: TextFormField( controller: passwordController, obscureText: true, decoration: InputDecoration( labelText: 'Password', - // suffixIcon: Icon(FontAwesomeIcons.envelope), ), + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) => value != null && value.length < 8 + ? 'Enter min 6 char' + : null, ), ), + const SizedBox(height: 20), RichText( text: TextSpan( style: TextStyle( color: Colors.orangeAccent[700], fontSize: 20), - text: 'No account', + text: 'No account?', children: [ TextSpan( recognizer: TapGestureRecognizer() ..onTap = widget.onClickedSignUp, - text: 'Sign up?', + text: 'Sign up', style: TextStyle( decoration: TextDecoration.underline, color: Colors.orangeAccent[700])), ], ), ), + const SizedBox(height: 20), GestureDetector( child: Text( 'Forgot Password?', diff --git a/expense_tracker/lib/Screens/addexpenses.dart b/expense_tracker/lib/Screens/addexpenses.dart index dad74c2..28debb8 100644 --- a/expense_tracker/lib/Screens/addexpenses.dart +++ b/expense_tracker/lib/Screens/addexpenses.dart @@ -17,6 +17,12 @@ class AddExpenses extends StatefulWidget { class _AddExpensesState extends State { String dropdownValue = list.first; + final entertainmentController = TextEditingController(); + final merchantnameController = TextEditingController(); + final descriptionController = TextEditingController(); + final dateController = TextEditingController(); + final documentController = TextEditingController(); + void openFiles() async { FilePickerResult? resultFile = await FilePicker.platform.pickFiles(allowMultiple: true); diff --git a/expense_tracker/lib/Screens/profile.dart b/expense_tracker/lib/Screens/profile.dart index 07a5565..3afe730 100644 --- a/expense_tracker/lib/Screens/profile.dart +++ b/expense_tracker/lib/Screens/profile.dart @@ -1,6 +1,7 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import '../Components/navbar.dart'; +import 'Login.dart'; class Profile extends StatefulWidget { const Profile({super.key}); @@ -88,7 +89,15 @@ class _ProfileState extends State { ], ), GestureDetector( - onTap: () => FirebaseAuth.instance.signOut(), + onTap: () { + FirebaseAuth.instance.signOut(); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => Login( + onClickedSignUp: () {}, + ))); + }, child: Container( alignment: Alignment.center, width: 250, diff --git a/expense_tracker/lib/Screens/register.dart b/expense_tracker/lib/Screens/register.dart index f429c68..cbb00dd 100644 --- a/expense_tracker/lib/Screens/register.dart +++ b/expense_tracker/lib/Screens/register.dart @@ -1,3 +1,4 @@ +import 'package:expense_tracker/Screens/myhomepage.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; @@ -98,7 +99,7 @@ class RegisterPageState extends State { ), autovalidateMode: AutovalidateMode.onUserInteraction, validator: (email) => - email != null && EmailValidator.validate(email) + !(email != null && EmailValidator.validate(email)) ? 'Enter a valid Email' : null, ), @@ -131,28 +132,16 @@ class RegisterPageState extends State { ), ), ), - Padding( - padding: const EdgeInsets.fromLTRB(20, 20, 40, 20), - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - Text( - 'Already have an account', - style: TextStyle(color: Colors.orangeAccent[700]), - ) - ], - ), - ), RichText( text: TextSpan( style: TextStyle( color: Colors.orangeAccent[700], fontSize: 20), - text: 'Have an account', + text: 'Have an account?', children: [ TextSpan( recognizer: TapGestureRecognizer() ..onTap = widget.onClickedSignIn, - text: 'Sign in?', + text: 'Sign in', style: TextStyle( decoration: TextDecoration.underline, color: Colors.orangeAccent[700])), @@ -199,7 +188,7 @@ class RegisterPageState extends State { Future signUp() async { final isValid = formKey.currentState!.validate(); - if (isValid) return; + if (!isValid) return; showDialog( context: context, barrierDismissible: false, diff --git a/expense_tracker/lib/main.dart b/expense_tracker/lib/main.dart index 8e2755f..7b02a78 100644 --- a/expense_tracker/lib/main.dart +++ b/expense_tracker/lib/main.dart @@ -1,8 +1,5 @@ - -import 'package:expense_tracker/Screens/Login.dart'; import 'package:expense_tracker/Screens/myhomepage.dart'; import 'package:firebase_auth/firebase_auth.dart'; -import 'package:expense_tracker/Screens/activity.dart'; import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart'; import 'package:flutter/material.dart';