cofounderella/lib/pages/login_page.dart

128 lines
3.6 KiB
Dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../components/my_button.dart';
import '../components/my_textfield.dart';
import '../constants.dart';
import '../utils/helper_dialogs.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;
}
// try login
try {
await AuthService().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: SafeArea(
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// logo and welcome message
Icon(
Icons.people_alt,
size: 60,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(height: 16),
Text(
'Welcome to ${Constants.appTitle}',
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(
'Don\'t have an account? ',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
GestureDetector(
onTap: onTap,
child: const Text(
'Register now',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
],
)
],
),
),
),
),
);
}
}