Initial commit: Eventify frontend
This commit is contained in:
358
lib/screens/login_screen.dart
Normal file
358
lib/screens/login_screen.dart
Normal file
@@ -0,0 +1,358 @@
|
||||
// lib/screens/login_screen.dart
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../features/auth/services/auth_service.dart';
|
||||
import 'home_screen.dart';
|
||||
import '../core/app_decoration.dart';
|
||||
|
||||
class LoginScreen extends StatefulWidget {
|
||||
const LoginScreen({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<LoginScreen> createState() => _LoginScreenState();
|
||||
}
|
||||
|
||||
class _LoginScreenState extends State<LoginScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
final TextEditingController _emailCtrl = TextEditingController();
|
||||
final TextEditingController _passCtrl = TextEditingController();
|
||||
final FocusNode _emailFocus = FocusNode();
|
||||
final FocusNode _passFocus = FocusNode();
|
||||
|
||||
final AuthService _auth = AuthService();
|
||||
bool _loading = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailCtrl.dispose();
|
||||
_passCtrl.dispose();
|
||||
_emailFocus.dispose();
|
||||
_passFocus.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String? _emailValidator(String? v) {
|
||||
if (v == null || v.trim().isEmpty) return 'Enter email';
|
||||
final email = v.trim();
|
||||
// Basic email pattern check
|
||||
final emailRegex = RegExp(r"^[^@]+@[^@]+\.[^@]+");
|
||||
if (!emailRegex.hasMatch(email)) return 'Enter a valid email';
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _passwordValidator(String? v) {
|
||||
if (v == null || v.trim().isEmpty) return 'Enter password';
|
||||
if (v.length < 6) return 'Password must be at least 6 characters';
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> _performLogin() async {
|
||||
final form = _formKey.currentState;
|
||||
if (form == null) return;
|
||||
if (!form.validate()) return;
|
||||
|
||||
final email = _emailCtrl.text.trim();
|
||||
final password = _passCtrl.text;
|
||||
|
||||
setState(() => _loading = true);
|
||||
|
||||
try {
|
||||
// AuthService.login now returns a UserModel and also persists profile info.
|
||||
await _auth.login(email, password);
|
||||
|
||||
if (!mounted) return;
|
||||
// small delay for UX
|
||||
await Future.delayed(const Duration(milliseconds: 150));
|
||||
|
||||
Navigator.of(context).pushReplacement(PageRouteBuilder(
|
||||
pageBuilder: (context, a1, a2) => const HomeScreen(),
|
||||
transitionDuration: const Duration(milliseconds: 650),
|
||||
reverseTransitionDuration: const Duration(milliseconds: 350),
|
||||
transitionsBuilder: (context, animation, secondaryAnimation, child) {
|
||||
return FadeTransition(opacity: animation, child: child);
|
||||
},
|
||||
));
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
final message = e.toString().replaceAll('Exception: ', '');
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
void _openRegister() {
|
||||
Navigator.of(context).push(MaterialPageRoute(builder: (_) => const RegisterScreen(isDesktop: false)));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const primary = Color(0xFF0B63D6);
|
||||
final width = MediaQuery.of(context).size.width;
|
||||
|
||||
return Scaffold(
|
||||
// backgroundColor: primary,
|
||||
body: Container(
|
||||
decoration: AppDecoration.blueGradient,
|
||||
child: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 18),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: width < 720 ? width : 720),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const SizedBox(height: 6),
|
||||
const Text('Welcome', style: TextStyle(fontSize: 34, fontWeight: FontWeight.bold, color: Colors.white)),
|
||||
const SizedBox(height: 6),
|
||||
const Text('Sign in to continue', style: TextStyle(color: Colors.white70, fontSize: 16)),
|
||||
const SizedBox(height: 26),
|
||||
|
||||
// HERO card
|
||||
Hero(
|
||||
tag: 'headerCard',
|
||||
flightShuttleBuilder: (flightContext, animation, flightDirection, fromContext, toContext) {
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: ScaleTransition(
|
||||
scale: animation.drive(Tween(begin: 0.98, end: 1.0).chain(CurveTween(curve: Curves.easeOut))),
|
||||
child: fromContext.widget,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: Material(
|
||||
type: MaterialType.transparency,
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
decoration: AppDecoration.blueGradientRounded(20).copyWith(
|
||||
boxShadow: const [BoxShadow(color: Colors.black26, blurRadius: 18, offset: Offset(0, 8))],
|
||||
),
|
||||
padding: const EdgeInsets.fromLTRB(18, 18, 18, 18),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
const SizedBox(height: 8),
|
||||
const Text('Eventify', style: TextStyle(color: Colors.white, fontSize: 22, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// white card inside the blue card — now uses Form
|
||||
Form(
|
||||
key: _formKey,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(12)),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
|
||||
child: Column(
|
||||
children: [
|
||||
// Email
|
||||
TextFormField(
|
||||
controller: _emailCtrl,
|
||||
focusNode: _emailFocus,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
autofillHints: const [AutofillHints.email],
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
border: InputBorder.none,
|
||||
prefixIcon: Icon(Icons.email, color: primary),
|
||||
),
|
||||
validator: _emailValidator,
|
||||
textInputAction: TextInputAction.next,
|
||||
onFieldSubmitted: (_) {
|
||||
FocusScope.of(context).requestFocus(_passFocus);
|
||||
},
|
||||
),
|
||||
const Divider(),
|
||||
// Password
|
||||
TextFormField(
|
||||
controller: _passCtrl,
|
||||
focusNode: _passFocus,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Password',
|
||||
border: InputBorder.none,
|
||||
prefixIcon: Icon(Icons.lock, color: primary),
|
||||
),
|
||||
validator: _passwordValidator,
|
||||
textInputAction: TextInputAction.done,
|
||||
onFieldSubmitted: (_) => _performLogin(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 18),
|
||||
|
||||
// Login button
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _loading ? null : _performLogin,
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.white,
|
||||
foregroundColor: primary,
|
||||
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
child: _loading
|
||||
? SizedBox(height: 18, width: 18, child: CircularProgressIndicator(strokeWidth: 2, color: primary))
|
||||
: const Text('Login', style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600)),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 18),
|
||||
Center(
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: _openRegister,
|
||||
child: const Text("Don't have an account? Register", style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Register screen calls backend register endpoint via AuthService.register
|
||||
class RegisterScreen extends StatefulWidget {
|
||||
final bool isDesktop;
|
||||
const RegisterScreen({Key? key, this.isDesktop = false}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<RegisterScreen> createState() => _RegisterScreenState();
|
||||
}
|
||||
|
||||
class _RegisterScreenState extends State<RegisterScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final TextEditingController _emailCtrl = TextEditingController();
|
||||
final TextEditingController _phoneCtrl = TextEditingController();
|
||||
final TextEditingController _passCtrl = TextEditingController();
|
||||
final TextEditingController _confirmCtrl = TextEditingController();
|
||||
final AuthService _auth = AuthService();
|
||||
|
||||
bool _loading = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailCtrl.dispose();
|
||||
_phoneCtrl.dispose();
|
||||
_passCtrl.dispose();
|
||||
_confirmCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _performRegister() async {
|
||||
if (!(_formKey.currentState?.validate() ?? false)) return;
|
||||
|
||||
final email = _emailCtrl.text.trim();
|
||||
final phone = _phoneCtrl.text.trim();
|
||||
final pass = _passCtrl.text;
|
||||
final confirm = _confirmCtrl.text;
|
||||
|
||||
if (pass != confirm) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Passwords do not match')));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _loading = true);
|
||||
|
||||
try {
|
||||
await _auth.register(
|
||||
email: email,
|
||||
phoneNumber: phone,
|
||||
password: pass,
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) => const HomeScreen()));
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
final message = e.toString().replaceAll('Exception: ', '');
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
String? _emailValidator(String? v) {
|
||||
if (v == null || v.trim().isEmpty) return 'Enter email';
|
||||
final emailRegex = RegExp(r"^[^@]+@[^@]+\.[^@]+");
|
||||
if (!emailRegex.hasMatch(v.trim())) return 'Enter a valid email';
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _phoneValidator(String? v) {
|
||||
if (v == null || v.trim().isEmpty) return 'Enter phone number';
|
||||
if (v.trim().length < 7) return 'Enter a valid phone number';
|
||||
return null;
|
||||
}
|
||||
|
||||
String? _passwordValidator(String? v) {
|
||||
if (v == null || v.isEmpty) return 'Enter password';
|
||||
if (v.length < 6) return 'Password must be at least 6 characters';
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Register')),
|
||||
body: SafeArea(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 520),
|
||||
child: Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(18.0),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(controller: _emailCtrl, decoration: const InputDecoration(labelText: 'Email'), validator: _emailValidator, keyboardType: TextInputType.emailAddress),
|
||||
const SizedBox(height: 8),
|
||||
TextFormField(controller: _phoneCtrl, decoration: const InputDecoration(labelText: 'Phone'), validator: _phoneValidator, keyboardType: TextInputType.phone),
|
||||
const SizedBox(height: 8),
|
||||
TextFormField(controller: _passCtrl, obscureText: true, decoration: const InputDecoration(labelText: 'Password'), validator: _passwordValidator),
|
||||
const SizedBox(height: 8),
|
||||
TextFormField(controller: _confirmCtrl, obscureText: true, decoration: const InputDecoration(labelText: 'Confirm password'), validator: _passwordValidator),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: _loading ? null : _performRegister,
|
||||
child: _loading ? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2)) : const Text('Register'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user