From a58b03ddbe9dc15bbf546fc0158ac638b082215d Mon Sep 17 00:00:00 2001 From: SumitPareek <2021pcecssumit165@poornima.org> Date: Sun, 19 May 2024 22:32:46 +0530 Subject: [PATCH] Feature of password visibility has fixed --- app/lib/screens/auth_screen/signup.dart | 171 ++++++++++++++---------- 1 file changed, 104 insertions(+), 67 deletions(-) diff --git a/app/lib/screens/auth_screen/signup.dart b/app/lib/screens/auth_screen/signup.dart index 9b51e82..36374ed 100644 --- a/app/lib/screens/auth_screen/signup.dart +++ b/app/lib/screens/auth_screen/signup.dart @@ -5,6 +5,7 @@ import 'package:examtime/services/ApiServices/api_services.dart.dart'; import 'package:flutter/material.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'signin.dart'; // Import your sign-in page here +import 'package:flutter/cupertino.dart'; class SignUpPage extends StatefulWidget { static const String routeName = '/signup'; @@ -16,12 +17,22 @@ class SignUpPage extends StatefulWidget { } class _SignUpPageState extends State { + late bool passwordVisible; + late bool passwordVisibleConfirm; TextEditingController name = TextEditingController(); TextEditingController email = TextEditingController(); TextEditingController password = TextEditingController(); final _formKey1 = GlobalKey(); final _formKey2 = GlobalKey(); final _formKey3 = GlobalKey(); + @override + void initState() { + // TODO: implement initState + super.initState(); + passwordVisible = false; + passwordVisibleConfirm = false; + } + @override Widget build(BuildContext context) { return Scaffold( @@ -43,31 +54,30 @@ class _SignUpPageState extends State { width: 200, height: 150, ), - Form( - key: _formKey1, - child: TextFormField( - controller: email, - keyboardType: TextInputType.emailAddress, - decoration: const InputDecoration( - filled: true, - fillColor: Colors.white, - hintText: 'Enter your email', - prefixIcon: Icon(Icons.email), + Form( + key: _formKey1, + child: TextFormField( + controller: email, + keyboardType: TextInputType.emailAddress, + decoration: const InputDecoration( + filled: true, + fillColor: Colors.white, + hintText: 'Enter your email', + prefixIcon: Icon(Icons.email), + ), + autovalidateMode: AutovalidateMode.onUserInteraction, + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter an email'; + } + final emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+'); + if (!emailRegex.hasMatch(value)) { + return 'Please enter a valid email'; + } + return null; + }, ), - - autovalidateMode: AutovalidateMode.onUserInteraction, - validator: (value) { - if (value == null || value.isEmpty) { - return 'Please enter an email'; - } - final emailRegex = RegExp(r'^[^@]+@[^@]+\.[^@]+'); - if (!emailRegex.hasMatch(value)) { - return 'Please enter a valid email'; - } - return null; - }, ), - ), const SizedBox(height: 10.0), TextField( controller: name, @@ -84,12 +94,26 @@ class _SignUpPageState extends State { autovalidateMode: AutovalidateMode.onUserInteraction, child: TextFormField( controller: password, - obscureText: true, // Set to true for password fields - decoration: const InputDecoration( + obscureText: !passwordVisible, + decoration: InputDecoration( filled: true, fillColor: Colors.white, hintText: 'Enter your password', - prefixIcon: Icon(Icons.lock), + prefixIcon: const Icon(Icons.lock), + suffixIcon: IconButton( + icon: Icon( + // Based on passwordVisible state choose the icon + passwordVisible + ? CupertinoIcons.eye_fill + : CupertinoIcons.eye_slash_fill, + ), + onPressed: () { + // Update the state i.e. toogle the state of passwordVisible variable + setState(() { + passwordVisible = !passwordVisible; + }); + }, + ), ), validator: (value) { if (value == null || value.isEmpty) { @@ -103,29 +127,45 @@ class _SignUpPageState extends State { ), ), const SizedBox(height: 10.0), - Form( - key: _formKey3, - autovalidateMode: AutovalidateMode.onUserInteraction, - child: TextFormField( - validator: (value) { - if(value!=password.text){ - return 'password should be match'; - } - return null; - }, - obscureText: true, - decoration: const InputDecoration( - filled: true, - fillColor: Colors.white, - hintText: 'Confirm Password', - prefixIcon: Icon(Icons.lock), + Form( + key: _formKey3, + autovalidateMode: AutovalidateMode.onUserInteraction, + child: TextFormField( + validator: (value) { + if (value != password.text) { + return 'password should be match'; + } + return null; + }, + obscureText: !passwordVisibleConfirm, + decoration: InputDecoration( + filled: true, + fillColor: Colors.white, + hintText: 'Confirm Password', + prefixIcon: const Icon(Icons.lock), + suffixIcon: IconButton( + icon: Icon( + // Based on passwordVisible state choose the icon + passwordVisibleConfirm + ? CupertinoIcons.eye_fill + : CupertinoIcons.eye_slash_fill, + ), + onPressed: () { + // Update the state i.e. toogle the state of passwordVisible variable + setState(() { + passwordVisibleConfirm = !passwordVisibleConfirm; + }); + }, + ), + ), ), ), - ), const SizedBox(height: 20.0), ElevatedButton( onPressed: () { - if(_formKey1.currentState!.validate() && _formKey2.currentState!.validate() && _formKey3.currentState!.validate()){ + if (_formKey1.currentState!.validate() && + _formKey2.currentState!.validate() && + _formKey3.currentState!.validate()) { Apiservices.signupUser( name: name.text, email: email.text, @@ -139,32 +179,30 @@ class _SignUpPageState extends State { context, value['token'], ); - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => OTPPage( - token: value['token'], + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => OTPPage( + token: value['token'], ), )); } }); - } - else{ + } else { showDialog( - context: context, - builder: (BuildContext context) { - return AlertDialog( - title: const Text("Credentials should not be empty"), - actions: [ - TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: const Text("OK"), - ), - ], - ); - } - ); + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: const Text( + "Credentials should not be empty"), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text("OK"), + ), + ], + ); + }); } Apiservices.signupUser( name: name.text, @@ -204,7 +242,6 @@ class _SignUpPageState extends State { onTap: () { Navigator.of(context).push( MaterialPageRoute( - builder: (context) => LoginPage(), ), ); @@ -222,4 +259,4 @@ class _SignUpPageState extends State { ), ); } -} \ No newline at end of file +}