This commit is contained in:
Phuoc Nguyen
2025-10-10 17:36:10 +07:00
parent 04f7042b8d
commit bdaf0b96c5
82 changed files with 4753 additions and 329 deletions

View File

@@ -1,8 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/auth_provider.dart';
import '../widgets/widgets.dart';
import '../utils/validators.dart';
import 'register_page.dart';
/// Login page for user authentication
/// Login page with email and password authentication
class LoginPage extends ConsumerStatefulWidget {
const LoginPage({super.key});
@@ -12,9 +15,9 @@ class LoginPage extends ConsumerStatefulWidget {
class _LoginPageState extends ConsumerState<LoginPage> {
final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
bool _obscurePassword = true;
final _emailController = TextEditingController(text: 'admin@retailpos.com');
final _passwordController = TextEditingController(text: 'Admin123!');
bool _rememberMe = false;
@override
void dispose() {
@@ -24,8 +27,15 @@ class _LoginPageState extends ConsumerState<LoginPage> {
}
Future<void> _handleLogin() async {
if (!_formKey.currentState!.validate()) return;
// Dismiss keyboard
FocusScope.of(context).unfocus();
// Validate form
if (!_formKey.currentState!.validate()) {
return;
}
// Attempt login
final success = await ref.read(authProvider.notifier).login(
email: _emailController.text.trim(),
password: _passwordController.text,
@@ -33,135 +43,196 @@ class _LoginPageState extends ConsumerState<LoginPage> {
if (!mounted) return;
if (success) {
// Navigate to home or show success
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Login successful!')),
);
// TODO: Navigate to home page
} else {
final errorMessage = ref.read(authProvider).errorMessage;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(errorMessage ?? 'Login failed'),
backgroundColor: Colors.red,
),
);
// Show error if login failed
if (!success) {
final authState = ref.read(authProvider);
if (authState.errorMessage != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(authState.errorMessage!),
backgroundColor: Theme.of(context).colorScheme.error,
behavior: SnackBarBehavior.floating,
action: SnackBarAction(
label: 'Dismiss',
textColor: Colors.white,
onPressed: () {},
),
),
);
}
}
// Navigation is handled by AuthWrapper
}
void _navigateToRegister() {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const RegisterPage(),
),
);
}
void _handleForgotPassword() {
// TODO: Implement forgot password functionality
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Forgot password feature coming soon!'),
behavior: SnackBarBehavior.floating,
),
);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final authState = ref.watch(authProvider);
final isLoading = authState.isLoading;
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Logo or app name
Icon(
Icons.shopping_cart,
size: 80,
color: Theme.of(context).primaryColor,
),
const SizedBox(height: 16),
Text(
'Retail POS',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 48),
// Email field
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
labelText: 'Email',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},
),
const SizedBox(height: 16),
// Password field
TextFormField(
controller: _passwordController,
obscureText: _obscurePassword,
decoration: InputDecoration(
labelText: 'Password',
prefixIcon: const Icon(Icons.lock),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscurePassword
? Icons.visibility
: Icons.visibility_off,
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Scaffold(
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Header with logo and title
const AuthHeader(
title: 'Retail POS',
subtitle: 'Welcome back! Please login to continue.',
),
onPressed: () {
setState(() {
_obscurePassword = !_obscurePassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 8) {
return 'Password must be at least 8 characters';
}
return null;
},
),
const SizedBox(height: 24),
const SizedBox(height: 48),
// Login button
FilledButton(
onPressed: authState.isLoading ? null : _handleLogin,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: authState.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
// Email field
AuthTextField(
controller: _emailController,
label: 'Email',
hint: 'Enter your email',
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
prefixIcon: Icons.email_outlined,
validator: AuthValidators.validateEmail,
enabled: !isLoading,
autofocus: true,
),
const SizedBox(height: 16),
// Password field
PasswordField(
controller: _passwordController,
label: 'Password',
hint: 'Enter your password',
textInputAction: TextInputAction.done,
validator: AuthValidators.validateLoginPassword,
onFieldSubmitted: (_) => _handleLogin(),
enabled: !isLoading,
),
const SizedBox(height: 8),
// Remember me and forgot password row
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// Remember me checkbox
Row(
children: [
Checkbox(
value: _rememberMe,
onChanged: isLoading
? null
: (value) {
setState(() {
_rememberMe = value ?? false;
});
},
),
Text(
'Remember me',
style: theme.textTheme.bodyMedium,
),
],
),
// Forgot password link
TextButton(
onPressed: isLoading ? null : _handleForgotPassword,
child: Text(
'Forgot Password?',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
)
: const Text('Login'),
),
],
),
const SizedBox(height: 24),
// Login button
AuthButton(
onPressed: _handleLogin,
text: 'Login',
isLoading: isLoading,
),
const SizedBox(height: 24),
// Divider
Row(
children: [
Expanded(
child: Divider(
color: theme.colorScheme.onSurface.withOpacity(0.2),
),
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
'OR',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurface.withOpacity(0.5),
),
),
),
Expanded(
child: Divider(
color: theme.colorScheme.onSurface.withOpacity(0.2),
),
),
],
),
const SizedBox(height: 24),
// Register link
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Don't have an account? ",
style: theme.textTheme.bodyMedium,
),
TextButton(
onPressed: isLoading ? null : _navigateToRegister,
child: Text(
'Register',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.bold,
),
),
),
],
),
],
),
),
const SizedBox(height: 16),
// Register link
TextButton(
onPressed: () {
// TODO: Navigate to register page
// Navigator.push(context, MaterialPageRoute(builder: (_) => const RegisterPage()));
},
child: const Text('Don\'t have an account? Register'),
),
],
),
),
),
),