|
| 1 | +// ignore_for_file: avoid_print |
| 2 | + |
| 3 | +import 'package:firebase_auth/firebase_auth.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:fluttertoast/fluttertoast.dart'; |
| 6 | +import '/authentication/common/widgets/button_widget.dart'; |
| 7 | +import '/authentication/common/widgets/input_field_widget.dart'; |
| 8 | +import '/authentication/signup/screens/signup_screen.dart'; |
| 9 | +import '/screens/home_screen.dart'; |
| 10 | + |
| 11 | +class LoginScreen extends StatefulWidget { |
| 12 | + const LoginScreen({Key? key}) : super(key: key); |
| 13 | + |
| 14 | + @override |
| 15 | + State<LoginScreen> createState() => _LoginScreenState(); |
| 16 | +} |
| 17 | + |
| 18 | +class _LoginScreenState extends State<LoginScreen> { |
| 19 | + // form key |
| 20 | + final _formKey = GlobalKey<FormState>(); |
| 21 | + |
| 22 | + // editing controller |
| 23 | + |
| 24 | + final TextEditingController _emailController = TextEditingController(); |
| 25 | + final TextEditingController _passwordController = TextEditingController(); |
| 26 | + |
| 27 | + // firebase |
| 28 | + |
| 29 | + final _auth = FirebaseAuth.instance; |
| 30 | + |
| 31 | + // string for displaying the error Message |
| 32 | + |
| 33 | + String? errorMessage; |
| 34 | + |
| 35 | + @override |
| 36 | + Widget build(BuildContext context) { |
| 37 | + //email field |
| 38 | + |
| 39 | + // final emailField = TextFormField( |
| 40 | + // autofocus: false, |
| 41 | + // controller: _emailController, |
| 42 | + // keyboardType: TextInputType.emailAddress, |
| 43 | + // validator: (value) { |
| 44 | + // if (value!.isEmpty) { |
| 45 | + // return ("Please Enter Your Email"); |
| 46 | + // } |
| 47 | + |
| 48 | + // if (!RegExp("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]").hasMatch(value)) { |
| 49 | + // return ("Please Enter a valid email"); |
| 50 | + // } |
| 51 | + // return null; |
| 52 | + // }, |
| 53 | + // onSaved: (value) { |
| 54 | + // _emailController.text = value!; |
| 55 | + // }, |
| 56 | + // textInputAction: TextInputAction.next, |
| 57 | + // decoration: InputDecoration( |
| 58 | + // prefixIcon: const Icon(Icons.mail), |
| 59 | + // contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15), |
| 60 | + // hintText: "Email", |
| 61 | + // border: OutlineInputBorder( |
| 62 | + // borderRadius: BorderRadius.circular(10), |
| 63 | + // ), |
| 64 | + // ), |
| 65 | + // ); |
| 66 | + |
| 67 | + //password field |
| 68 | + |
| 69 | + // final passwordField = TextFormField( |
| 70 | + // autofocus: false, |
| 71 | + // controller: _passwordController, |
| 72 | + // obscureText: true, |
| 73 | + // validator: (value) { |
| 74 | + // RegExp regex = new RegExp(r'^.{6,}$'); |
| 75 | + // if (value!.isEmpty) { |
| 76 | + // return ("Password is required for login"); |
| 77 | + // } |
| 78 | + // if (!regex.hasMatch(value)) { |
| 79 | + // return ("Enter Valid Password(Min. 6 Character)"); |
| 80 | + // } |
| 81 | + // return null; |
| 82 | + // }, |
| 83 | + // onSaved: (value) { |
| 84 | + // _passwordController.text = value!; |
| 85 | + // }, |
| 86 | + // textInputAction: TextInputAction.done, |
| 87 | + // decoration: InputDecoration( |
| 88 | + // prefixIcon: const Icon(Icons.vpn_key), |
| 89 | + // contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15), |
| 90 | + // hintText: "Password", |
| 91 | + // border: OutlineInputBorder( |
| 92 | + // borderRadius: BorderRadius.circular(10), |
| 93 | + // ), |
| 94 | + // ), |
| 95 | + // ); |
| 96 | + |
| 97 | + // button field |
| 98 | + |
| 99 | + // final loginButton = Material( |
| 100 | + // elevation: 5, |
| 101 | + // borderRadius: BorderRadius.circular(30), |
| 102 | + // color: Colors.redAccent, |
| 103 | + // child: MaterialButton( |
| 104 | + // padding: const EdgeInsets.fromLTRB(20, 15, 20, 15), |
| 105 | + // minWidth: MediaQuery.of(context).size.width, |
| 106 | + // onPressed: () { |
| 107 | + // signIn(emailController.text, passwordController.text); |
| 108 | + // }, |
| 109 | + // child: const Text( |
| 110 | + // "Login", |
| 111 | + // textAlign: TextAlign.center, |
| 112 | + // style: TextStyle( |
| 113 | + // fontSize: 20, |
| 114 | + // color: Colors.white, |
| 115 | + // fontWeight: FontWeight.bold, |
| 116 | + // ), |
| 117 | + // ), |
| 118 | + // ), |
| 119 | + // ); |
| 120 | + |
| 121 | + return Scaffold( |
| 122 | + backgroundColor: Colors.white, |
| 123 | + body: Center( |
| 124 | + child: SingleChildScrollView( |
| 125 | + child: Container( |
| 126 | + color: Colors.white, |
| 127 | + child: Padding( |
| 128 | + padding: const EdgeInsets.all(36), |
| 129 | + child: Form( |
| 130 | + key: _formKey, |
| 131 | + child: Column( |
| 132 | + mainAxisAlignment: MainAxisAlignment.center, |
| 133 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 134 | + children: [ |
| 135 | + SizedBox( |
| 136 | + height: 200, |
| 137 | + child: Image.asset( |
| 138 | + "assets/images/logo.png", |
| 139 | + fit: BoxFit.contain, |
| 140 | + ), |
| 141 | + ), |
| 142 | + const SizedBox(height: 45), |
| 143 | + |
| 144 | + // email field & its control |
| 145 | + |
| 146 | + TextInputField( |
| 147 | + inputController: _emailController, |
| 148 | + formValidator: (value) { |
| 149 | + if (value!.isEmpty) { |
| 150 | + return ("Please Enter Your Email"); |
| 151 | + } |
| 152 | + |
| 153 | + if (!RegExp("^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+.[a-z]") |
| 154 | + .hasMatch(value)) { |
| 155 | + return ("Please Enter a valid email"); |
| 156 | + } |
| 157 | + return null; |
| 158 | + }, |
| 159 | + onSaved: (value) { |
| 160 | + _emailController.text = value!; |
| 161 | + }, |
| 162 | + textInputAction: TextInputAction.next, |
| 163 | + icon: Icons.mail, |
| 164 | + textName: 'Email', |
| 165 | + ), |
| 166 | + const SizedBox(height: 25), |
| 167 | + |
| 168 | + // password field & its control |
| 169 | + |
| 170 | + TextInputField( |
| 171 | + inputController: _passwordController, |
| 172 | + toHide: true, |
| 173 | + formValidator: (value) { |
| 174 | + RegExp regex = RegExp(r'^.{6,}$'); |
| 175 | + if (value!.isEmpty) { |
| 176 | + return ("Password is required for login"); |
| 177 | + } |
| 178 | + if (!regex.hasMatch(value)) { |
| 179 | + return ("Enter Valid Password(Min. 6 Character)"); |
| 180 | + } |
| 181 | + return null; |
| 182 | + }, |
| 183 | + onSaved: (value) { |
| 184 | + _passwordController.text = value!; |
| 185 | + }, |
| 186 | + textInputAction: TextInputAction.done, |
| 187 | + icon: Icons.vpn_key, |
| 188 | + textName: 'Password', |
| 189 | + ), |
| 190 | + const SizedBox(height: 35), |
| 191 | + |
| 192 | + // signIn button control |
| 193 | + |
| 194 | + Button( |
| 195 | + onPressed: () { |
| 196 | + signIn( |
| 197 | + _emailController.text, |
| 198 | + _passwordController.text, |
| 199 | + ); |
| 200 | + }, |
| 201 | + btnName: 'Sign In', |
| 202 | + ), |
| 203 | + const SizedBox(height: 15), |
| 204 | + Row( |
| 205 | + mainAxisAlignment: MainAxisAlignment.center, |
| 206 | + children: [ |
| 207 | + const Text("Don't have an account? "), |
| 208 | + GestureDetector( |
| 209 | + onTap: () { |
| 210 | + Navigator.push( |
| 211 | + context, |
| 212 | + MaterialPageRoute( |
| 213 | + builder: (context) => |
| 214 | + const RegistrationScreen(), |
| 215 | + ), |
| 216 | + ); |
| 217 | + }, |
| 218 | + child: const Text( |
| 219 | + "SignUp", |
| 220 | + style: TextStyle( |
| 221 | + color: Colors.redAccent, |
| 222 | + fontWeight: FontWeight.bold, |
| 223 | + fontSize: 15, |
| 224 | + ), |
| 225 | + ), |
| 226 | + ), |
| 227 | + ], |
| 228 | + ), |
| 229 | + ], |
| 230 | + ), |
| 231 | + ), |
| 232 | + ), |
| 233 | + ), |
| 234 | + ), |
| 235 | + ), |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + // signIn function |
| 240 | + |
| 241 | + void signIn(String email, String password) async { |
| 242 | + if (_formKey.currentState!.validate()) { |
| 243 | + try { |
| 244 | + await _auth |
| 245 | + .signInWithEmailAndPassword(email: email, password: password) |
| 246 | + .then( |
| 247 | + (uid) => { |
| 248 | + Fluttertoast.showToast(msg: "Login Successful"), |
| 249 | + Navigator.of(context).pushReplacement( |
| 250 | + MaterialPageRoute( |
| 251 | + builder: (context) => const HomeScreen(), |
| 252 | + ), |
| 253 | + ), |
| 254 | + }, |
| 255 | + ); |
| 256 | + } on FirebaseAuthException catch (error) { |
| 257 | + switch (error.code) { |
| 258 | + case "invalid-email": |
| 259 | + errorMessage = "Your email address appears to be malformed."; |
| 260 | + break; |
| 261 | + case "wrong-password": |
| 262 | + errorMessage = "Your password is wrong."; |
| 263 | + break; |
| 264 | + case "user-not-found": |
| 265 | + errorMessage = "User with this email doesn't exist."; |
| 266 | + break; |
| 267 | + case "user-disabled": |
| 268 | + errorMessage = "User with this email has been disabled."; |
| 269 | + break; |
| 270 | + case "too-many-requests": |
| 271 | + errorMessage = "Too many requests"; |
| 272 | + break; |
| 273 | + case "operation-not-allowed": |
| 274 | + errorMessage = "Signing in with Email and Password is not enabled."; |
| 275 | + break; |
| 276 | + default: |
| 277 | + errorMessage = "An undefined Error happened."; |
| 278 | + } |
| 279 | + Fluttertoast.showToast(msg: errorMessage!); |
| 280 | + print(error.code); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments