login_page.dart (5159B)
1 import 'package:firebase_auth/firebase_auth.dart'; 2 import 'package:flutter/material.dart'; 3 import 'package:penny/components/my_button.dart'; 4 import 'package:penny/components/my_textfield.dart'; 5 import 'package:penny/components/square_tile.dart'; 6 import 'package:penny/services/auth_service.dart'; 7 8 class LoginPage extends StatefulWidget { 9 final Function()? onTap; 10 const LoginPage({super.key, required this.onTap}); 11 12 @override 13 State<LoginPage> createState() => _LoginPageState(); 14 } 15 16 class _LoginPageState extends State<LoginPage> { 17 final emailController = TextEditingController(); 18 19 final passwordController = TextEditingController(); 20 21 // Sign user in method 22 void signUserIn() async { 23 try { 24 await FirebaseAuth.instance.signInWithEmailAndPassword( 25 email: emailController.text, 26 password: passwordController.text, 27 ); 28 } on FirebaseAuthException catch (e) { 29 SnackBar( 30 content: Text('Sign in failed: ${e.message}'), 31 ); 32 } 33 } 34 35 @override 36 Widget build(BuildContext context) { 37 return Scaffold( 38 backgroundColor: const Color(0xfffffc00), 39 body: SafeArea( 40 child: Center( 41 child: SingleChildScrollView( 42 child: Column( 43 children: [ 44 // Logo 45 const Text( 46 'Penny', 47 style: TextStyle( 48 color: Colors.black, 49 fontSize: 32, 50 fontWeight: FontWeight.bold, 51 ), 52 ), 53 const SizedBox(height: 25), 54 55 // Username textfield 56 MyTextField( 57 controller: emailController, 58 hintText: 'Username', 59 obscureText: false, 60 ), 61 62 const SizedBox(height: 10), 63 64 // Password textfield 65 MyTextField( 66 controller: passwordController, 67 hintText: 'Password', 68 obscureText: true, 69 ), 70 const SizedBox(height: 10), 71 72 // Forgot password? FUTURE UPDATE! 73 // const Padding( 74 // padding: EdgeInsets.symmetric(horizontal: 25.0), 75 // child: Row( 76 // mainAxisAlignment: MainAxisAlignment.end, 77 // children: [ 78 // Text( 79 // 'Forgot Password?', 80 // style: TextStyle(color: Colors.black), 81 // ), 82 // ], 83 // ), 84 // ), 85 86 const SizedBox(height: 25), 87 88 // Sign in button 89 MyButton( 90 text: "Sign In", 91 onTap: signUserIn, 92 ), 93 94 const SizedBox(height: 25), 95 96 // or continue with 97 const Padding( 98 padding: EdgeInsets.symmetric(horizontal: 25.0), 99 child: Row( 100 children: [ 101 Expanded( 102 child: Divider( 103 thickness: 0.5, 104 color: Colors.black, 105 ), 106 ), 107 Padding( 108 padding: EdgeInsets.symmetric(horizontal: 10.0), 109 child: Text( 110 'Or continue with', 111 style: TextStyle(color: Colors.black), 112 ), 113 ), 114 Expanded( 115 child: Divider( 116 thickness: 0.5, 117 color: Colors.black, 118 ), 119 ), 120 ], 121 ), 122 ), 123 124 const SizedBox(height: 50), 125 126 // Google sign in button 127 Row( 128 mainAxisAlignment: MainAxisAlignment.center, 129 children: [ 130 SquareTile( 131 onTap: () => AuthService().signInWithGoogle(), 132 imagePath: 'lib/images/google.png'), 133 ], 134 ), 135 136 const SizedBox(height: 50), 137 138 // Not a member? Register now 139 Row( 140 mainAxisAlignment: MainAxisAlignment.center, 141 children: [ 142 const Text( 143 'Not a member?', 144 style: TextStyle(color: Colors.black), 145 ), 146 const SizedBox(width: 4), 147 GestureDetector( 148 onTap: widget.onTap, 149 child: const Text( 150 'Register now', 151 style: TextStyle( 152 color: Colors.blue, 153 fontWeight: FontWeight.bold, 154 ), 155 ), 156 ), 157 ], 158 ) 159 ], 160 ), 161 ), 162 ), 163 ), 164 ); 165 } 166 }