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'),
),
],
),
),
),
),

View File

@@ -0,0 +1,7 @@
/// Export all auth presentation pages
///
/// Contains all screens related to authentication
library;
export 'login_page.dart';
export 'register_page.dart';

View File

@@ -1,8 +1,10 @@
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';
/// Register page for new user registration
/// Registration page for new users
class RegisterPage extends ConsumerStatefulWidget {
const RegisterPage({super.key});
@@ -16,8 +18,7 @@ class _RegisterPageState extends ConsumerState<RegisterPage> {
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
bool _obscurePassword = true;
bool _obscureConfirmPassword = true;
bool _acceptTerms = false;
@override
void dispose() {
@@ -29,8 +30,27 @@ class _RegisterPageState extends ConsumerState<RegisterPage> {
}
Future<void> _handleRegister() async {
if (!_formKey.currentState!.validate()) return;
// Dismiss keyboard
FocusScope.of(context).unfocus();
// Validate form
if (!_formKey.currentState!.validate()) {
return;
}
// Check terms acceptance
if (!_acceptTerms) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Please accept the terms and conditions'),
backgroundColor: Theme.of(context).colorScheme.error,
behavior: SnackBarBehavior.floating,
),
);
return;
}
// Attempt registration
final success = await ref.read(authProvider.notifier).register(
name: _nameController.text.trim(),
email: _emailController.text.trim(),
@@ -40,191 +60,241 @@ class _RegisterPageState extends ConsumerState<RegisterPage> {
if (!mounted) return;
if (success) {
// Navigate to home or show success
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Registration successful!')),
);
// TODO: Navigate to home page
} else {
final errorMessage = ref.read(authProvider).errorMessage;
// Show success message
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(errorMessage ?? 'Registration failed'),
backgroundColor: Colors.red,
content: const Text('Registration successful!'),
backgroundColor: Theme.of(context).colorScheme.primary,
behavior: SnackBarBehavior.floating,
),
);
// Navigation is handled by AuthWrapper
} else {
// Show error message
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: () {},
),
),
);
}
}
}
void _navigateBackToLogin() {
Navigator.of(context).pop();
}
@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('Register'),
),
body: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 24),
// Logo or app name
Icon(
Icons.shopping_cart,
size: 80,
color: Theme.of(context).primaryColor,
),
const SizedBox(height: 16),
Text(
'Create Account',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineMedium,
),
const SizedBox(height: 48),
// Name field
TextFormField(
controller: _nameController,
decoration: const InputDecoration(
labelText: 'Full Name',
prefixIcon: Icon(Icons.person),
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your name';
}
if (value.length < 2) {
return 'Name must be at least 2 characters';
}
return null;
},
),
const SizedBox(height: 16),
// 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(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: isLoading ? null : _navigateBackToLogin,
),
),
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
const AuthHeader(
title: 'Create Account',
subtitle: 'Join us and start managing your retail business.',
),
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';
}
// Check for uppercase, lowercase, and number
if (!RegExp(r'(?=.*[a-z])(?=.*[A-Z])(?=.*\d)').hasMatch(value)) {
return 'Password must contain uppercase, lowercase, and number';
}
return null;
},
),
const SizedBox(height: 16),
const SizedBox(height: 40),
// Confirm password field
TextFormField(
controller: _confirmPasswordController,
obscureText: _obscureConfirmPassword,
decoration: InputDecoration(
labelText: 'Confirm Password',
prefixIcon: const Icon(Icons.lock_outline),
border: const OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_obscureConfirmPassword
? Icons.visibility
: Icons.visibility_off,
// Name field
AuthTextField(
controller: _nameController,
label: 'Full Name',
hint: 'Enter your full name',
keyboardType: TextInputType.name,
textInputAction: TextInputAction.next,
prefixIcon: Icons.person_outline,
validator: AuthValidators.validateName,
enabled: !isLoading,
autofocus: true,
),
onPressed: () {
setState(() {
_obscureConfirmPassword = !_obscureConfirmPassword;
});
},
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please confirm your password';
}
if (value != _passwordController.text) {
return 'Passwords do not match';
}
return null;
},
),
const SizedBox(height: 24),
const SizedBox(height: 16),
// Register button
FilledButton(
onPressed: authState.isLoading ? null : _handleRegister,
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,
),
const SizedBox(height: 16),
// Password field
PasswordField(
controller: _passwordController,
label: 'Password',
hint: 'Create a strong password',
textInputAction: TextInputAction.next,
validator: AuthValidators.validatePassword,
enabled: !isLoading,
),
const SizedBox(height: 16),
// Confirm password field
PasswordField(
controller: _confirmPasswordController,
label: 'Confirm Password',
hint: 'Re-enter your password',
textInputAction: TextInputAction.done,
validator: (value) => AuthValidators.validateConfirmPassword(
value,
_passwordController.text,
),
onFieldSubmitted: (_) => _handleRegister(),
enabled: !isLoading,
),
const SizedBox(height: 16),
// Terms and conditions checkbox
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Checkbox(
value: _acceptTerms,
onChanged: isLoading
? null
: (value) {
setState(() {
_acceptTerms = value ?? false;
});
},
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 12),
child: GestureDetector(
onTap: isLoading
? null
: () {
setState(() {
_acceptTerms = !_acceptTerms;
});
},
child: Text.rich(
TextSpan(
text: 'I agree to the ',
style: theme.textTheme.bodyMedium,
children: [
TextSpan(
text: 'Terms and Conditions',
style: TextStyle(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
const TextSpan(text: ' and '),
TextSpan(
text: 'Privacy Policy',
style: TextStyle(
color: theme.colorScheme.primary,
fontWeight: FontWeight.w600,
),
),
],
),
),
),
),
)
: const Text('Register'),
),
],
),
const SizedBox(height: 24),
// Register button
AuthButton(
onPressed: _handleRegister,
text: 'Create Account',
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),
// Login link
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Already have an account? ',
style: theme.textTheme.bodyMedium,
),
TextButton(
onPressed: isLoading ? null : _navigateBackToLogin,
child: Text(
'Login',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.primary,
fontWeight: FontWeight.bold,
),
),
),
],
),
],
),
),
const SizedBox(height: 16),
// Login link
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Already have an account? Login'),
),
],
),
),
),
),