chitfund/luckychit/lib/features/auth/views/signup_screen.dart

303 lines
13 KiB
Dart

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../core/services/auth_service.dart';
import '../../../core/utils/snackbar_util.dart';
import '../../../app.dart';
import '../../../core/design_system/app_components/auth_shell.dart';
import '../../../core/design_system/app_components/app_card.dart';
import '../../../core/design_system/app_spacing.dart';
import '../../../core/design_system/app_text.dart';
class SignupScreen extends StatefulWidget {
const SignupScreen({super.key});
@override
State<SignupScreen> createState() => _SignupScreenState();
}
class _SignupScreenState extends State<SignupScreen> {
final _formKey = GlobalKey<FormState>();
final _mobileController = TextEditingController();
final _fullNameController = TextEditingController();
final _passwordController = TextEditingController();
final _confirmPasswordController = TextEditingController();
final _emailController = TextEditingController();
final _addressController = TextEditingController();
final _emergencyContactController = TextEditingController();
final _authService = Get.find<AuthService>();
bool _isPasswordVisible = false;
bool _isConfirmPasswordVisible = false;
@override
void dispose() {
_mobileController.dispose();
_fullNameController.dispose();
_passwordController.dispose();
_confirmPasswordController.dispose();
_emailController.dispose();
_addressController.dispose();
_emergencyContactController.dispose();
super.dispose();
}
Future<void> _handleSignup() async {
if (_formKey.currentState!.validate()) {
final success = await _authService.signup(
_mobileController.text.trim(),
_fullNameController.text.trim(),
_passwordController.text,
email: _emailController.text.trim().isEmpty ? null : _emailController.text.trim(),
address: _addressController.text.trim().isEmpty ? null : _addressController.text.trim(),
emergencyContact: _emergencyContactController.text.trim().isEmpty ? null : _emergencyContactController.text.trim(),
);
if (success) {
SnackbarUtil.showSuccess(
'Account created successfully! Welcome to LuckyChit.',
title: 'Success',
);
// Navigate to home - App widget will auto-route based on role
// Use offAll to remove all previous routes (can't go back to signup)
Get.offAll(() => const App());
} else {
SnackbarUtil.showError(
'Failed to create account. Please try again.',
title: 'Signup Failed',
);
}
}
}
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Scaffold(
body: AuthShell(
child: SingleChildScrollView(
child: AppCard(
padding: AppSpacing.all(AppSpacing.lg),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.account_balance_wallet_rounded,
size: 56.w,
color: scheme.primary,
),
SizedBox(height: 16.h),
Text('Create account', style: AppText.headline(context), textAlign: TextAlign.center),
SizedBox(height: 6.h),
Text(
'Set up your profile in under a minute.',
style: AppText.bodyMuted(context),
textAlign: TextAlign.center,
),
SizedBox(height: 24.h),
// Mobile Number Field
TextFormField(
controller: _mobileController,
keyboardType: TextInputType.phone,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Mobile number *',
prefixIcon: Icon(Icons.phone_rounded),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter mobile number';
}
if (value.length != 10) {
return 'Mobile number must be 10 digits';
}
if (!RegExp(r'^[0-9]+$').hasMatch(value)) {
return 'Mobile number must contain only digits';
}
return null;
},
),
SizedBox(height: 20.h),
// Full Name Field
TextFormField(
controller: _fullNameController,
keyboardType: TextInputType.name,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Full name *',
prefixIcon: Icon(Icons.person_rounded),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your full name';
}
return null;
},
),
SizedBox(height: 20.h),
// Email Field (Optional)
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Email (optional)',
prefixIcon: Icon(Icons.email_rounded),
),
validator: (value) {
if (value != null && value.isNotEmpty) {
if (!RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(value)) {
return 'Please enter a valid email address';
}
}
return null;
},
),
SizedBox(height: 20.h),
// Address Field (Optional)
TextFormField(
controller: _addressController,
keyboardType: TextInputType.streetAddress,
maxLines: 2,
textInputAction: TextInputAction.newline,
decoration: const InputDecoration(
labelText: 'Address (optional)',
prefixIcon: Icon(Icons.home_rounded),
),
),
SizedBox(height: 20.h),
// Emergency Contact Field (Optional)
TextFormField(
controller: _emergencyContactController,
keyboardType: TextInputType.phone,
textInputAction: TextInputAction.next,
decoration: const InputDecoration(
labelText: 'Emergency contact (optional)',
prefixIcon: Icon(Icons.contact_phone_rounded),
),
validator: (value) {
if (value != null && value.isNotEmpty) {
if (value.length != 10) {
return 'Emergency contact must be 10 digits';
}
if (!RegExp(r'^[0-9]+$').hasMatch(value)) {
return 'Emergency contact must contain only digits';
}
}
return null;
},
),
SizedBox(height: 20.h),
// Password Field
TextFormField(
controller: _passwordController,
obscureText: !_isPasswordVisible,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Password *',
prefixIcon: const Icon(Icons.lock_rounded),
suffixIcon: IconButton(
tooltip: _isPasswordVisible ? 'Hide password' : 'Show password',
icon: Icon(_isPasswordVisible ? Icons.visibility : Icons.visibility_off),
onPressed: () => setState(() => _isPasswordVisible = !_isPasswordVisible),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
SizedBox(height: 20.h),
// Confirm Password Field
TextFormField(
controller: _confirmPasswordController,
obscureText: !_isConfirmPasswordVisible,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelText: 'Confirm password *',
prefixIcon: const Icon(Icons.lock_outline_rounded),
suffixIcon: IconButton(
tooltip: _isConfirmPasswordVisible ? 'Hide password' : 'Show password',
icon: Icon(_isConfirmPasswordVisible ? Icons.visibility : Icons.visibility_off),
onPressed: () => setState(() => _isConfirmPasswordVisible = !_isConfirmPasswordVisible),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please confirm password';
}
if (value != _passwordController.text) {
return 'Passwords do not match';
}
return null;
},
),
SizedBox(height: 28.h),
// Signup Button
Obx(() => SizedBox(
width: double.infinity,
height: 52.h,
child: FilledButton(
onPressed: _authService.isLoading.value
? null
: _handleSignup,
child: _authService.isLoading.value
? SizedBox(
height: 24.h,
width: 24.w,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(scheme.onPrimary),
),
)
: const Text('Create account'),
),
)),
SizedBox(height: 20.h),
// Login Link
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Already have an account? ',
style: AppText.bodyMuted(context),
),
TextButton(
onPressed: () {
Get.back();
},
child: Text(
'Login',
style: AppText.label(context).copyWith(color: scheme.primary),
),
),
],
),
],
),
),
),
),
),
);
}
}