tasktracker/lib/screens/login_screen.dart
2025-12-07 15:52:59 +05:30

267 lines
8.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../widgets/oauth_button.dart';
import '../widgets/custom_text_field.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({
required this.onLogin,
required this.onOAuthLogin,
super.key,
});
final Future<void> Function(String email, String password, bool rememberMe) onLogin;
final void Function(OAuthProvider) onOAuthLogin;
@override
State<LoginScreen> createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _isPasswordVisible = false;
bool _isLoading = false;
bool _rememberMe = false;
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
void _handleLogin() async {
if (_formKey.currentState!.validate()) {
setState(() {
_isLoading = true;
});
try {
await widget.onLogin(_emailController.text, _passwordController.text, _rememberMe);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Login successful!'),
backgroundColor: Colors.green,
),
);
}
} catch (e) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Login failed: ${e.toString()}'),
backgroundColor: Colors.red,
),
);
}
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
}
void _handleOAuthLogin(OAuthProvider provider) {
widget.onOAuthLogin(provider);
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(horizontal: 24),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Email Field
CustomTextField(
controller: _emailController,
labelText: 'Email',
hintText: 'Enter your email',
keyboardType: TextInputType.emailAddress,
prefixIcon: Icons.email_outlined,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
const SizedBox(height: 16),
// Password Field
CustomTextField(
controller: _passwordController,
labelText: 'Password',
hintText: 'Enter your password',
obscureText: !_isPasswordVisible,
prefixIcon: Icons.lock_outlined,
suffixIcon: IconButton(
icon: Icon(
_isPasswordVisible ? Icons.visibility_off : Icons.visibility,
),
onPressed: () {
setState(() {
_isPasswordVisible = !_isPasswordVisible;
});
},
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
const SizedBox(height: 8),
// Remember Me and Forgot Password Row
Row(
children: [
// Remember Me Checkbox
Checkbox(
value: _rememberMe,
onChanged: (value) {
setState(() {
_rememberMe = value ?? false;
});
},
activeColor: Theme.of(context).primaryColor,
),
Text(
'Remember me',
style: GoogleFonts.poppins(
color: Colors.grey[600],
fontSize: 14,
),
),
const Spacer(),
// Forgot Password
TextButton(
onPressed: () {
// TODO: Implement forgot password
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Forgot password functionality will be implemented')),
);
},
child: Text(
'Forgot Password?',
style: GoogleFonts.poppins(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.w500,
),
),
),
],
),
const SizedBox(height: 24),
// Login Button
ElevatedButton(
onPressed: _isLoading ? null : _handleLogin,
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
elevation: 2,
),
child: _isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
)
: Text(
'Login',
style: GoogleFonts.poppins(
fontSize: 16,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(height: 32),
// Divider
Row(
children: [
Expanded(
child: Divider(
color: Colors.grey[300],
thickness: 1,
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'Or continue with',
style: GoogleFonts.poppins(
color: Colors.grey[600],
fontSize: 14,
),
),
),
Expanded(
child: Divider(
color: Colors.grey[300],
thickness: 1,
),
),
],
),
const SizedBox(height: 24),
// OAuth Buttons
OAuthButton(
provider: 'Google',
icon: Icons.g_mobiledata,
onPressed: () => _handleOAuthLogin(OAuthProvider.google),
),
const SizedBox(height: 12),
// OAuthButton(
// provider: 'Facebook',
// icon: Icons.facebook,
// onPressed: () => _handleOAuthLogin(OAuthProvider.facebook),
// ),
// const SizedBox(height: 12),
OAuthButton(
provider: 'GitHub',
icon: Icons.code,
onPressed: () => _handleOAuthLogin(OAuthProvider.github),
),
],
),
),
);
}
}