import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import '../components/my_button.dart'; import '../components/my_textfield.dart'; import '../utils/helper.dart'; import '../services/auth/auth_service.dart'; class LoginPage extends StatelessWidget { //text controllers final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); // tap to go to register page final void Function()? onTap; LoginPage({super.key, required this.onTap}); // login method void login(BuildContext context) async { if (_emailController.text.trim().isEmpty || _passwordController.text.trim().isEmpty) { showMsg( context, 'Missing information', 'Please enter your email and password', ); return; } // auth service final auth = AuthService(); // try login try { await auth.signInWithEmailPassword( _emailController.text, _passwordController.text, ); } on FirebaseAuthException catch (e) { if (context.mounted) { showMsg( context, e.code.isNotEmpty ? e.code : 'Login error', '${e.message}', ); } } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Theme.of(context).colorScheme.surface, body: Center( child: SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // logo Icon( Icons.people_alt, size: 60, color: Theme.of(context).colorScheme.primary, ), const SizedBox(height: 50), //welcome back message Text( "Welcome back, you've been missed", style: TextStyle( color: Theme.of(context).colorScheme.primary, fontSize: 16, ), ), const SizedBox(height: 25), // email textfield MyTextField( hintText: "E-Mail", obscureText: false, controller: _emailController, ), // password textfield MyTextField( hintText: "Password", obscureText: true, controller: _passwordController, ), const SizedBox(height: 25), // login button MyButton( text: "Login", onTap: () => login(context), ), const SizedBox(height: 25), // register now Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( "Not a member? ", style: TextStyle( color: Theme.of(context).colorScheme.primary, ), ), GestureDetector( onTap: onTap, child: const Text( "Register now", style: TextStyle( fontWeight: FontWeight.bold, ), ), ), ], ) ], ), ), ), ); } }