Files
Kifi/kifi-app/lib/features/auth/presentation/auth_screen.dart

247 lines
11 KiB
Dart

import 'package:flutter/material.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/auth_provider.dart';
import 'otp_screen.dart';
import 'forgot_password_screen.dart';
import '../../dashboard/presentation/dashboard_screen.dart';
import '../../../../core/network/dio_client.dart';
import 'setup_wizard_screen.dart';
class AuthScreen extends ConsumerStatefulWidget {
const AuthScreen({super.key});
@override
ConsumerState<AuthScreen> createState() => _AuthScreenState();
}
class _AuthScreenState extends ConsumerState<AuthScreen> with SingleTickerProviderStateMixin {
bool isLogin = true;
final emailController = TextEditingController(text: 'maddy23285@gmail.com');
final passwordController = TextEditingController(text: 'Algorithm@123');
void toggleMode(bool login) {
if (isLogin == login) return;
setState(() {
isLogin = login;
});
}
Future<void> submit() async {
final email = emailController.text.trim();
final password = passwordController.text;
if (email.isEmpty || password.isEmpty) return;
if (isLogin) {
final success = await ref.read(authControllerProvider.notifier).login(email, password);
if (success && mounted) {
try {
final dio = DioClient().dio;
final res = await dio.get('/account/setup/status');
final status = res.data['status'];
if (status == 'COMPLETED' && mounted) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => const DashboardScreen()));
} else if (mounted) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => const SetupWizardScreen()));
}
} catch(e) {
if (mounted) Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => const SetupWizardScreen()));
}
}
} else {
final success = await ref.read(authControllerProvider.notifier).signup(email, password);
if (success && mounted) {
Navigator.push(context, MaterialPageRoute(builder: (_) => OtpScreen(email: email)));
}
}
}
InputDecoration _buildInputDecoration(String label, IconData icon) {
return InputDecoration(
labelText: label,
labelStyle: TextStyle(color: Colors.grey.shade700),
prefixIcon: Icon(icon, color: Colors.grey.shade500),
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.grey.shade300),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Colors.grey.shade300),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
borderSide: BorderSide(color: Theme.of(context).primaryColor, width: 2),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 18),
);
}
@override
Widget build(BuildContext context) {
ref.listen<AsyncValue<void>>(authControllerProvider, (previous, next) {
if (next.hasError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('${next.error}')),
);
}
});
final state = ref.watch(authControllerProvider);
final isLoading = state.isLoading;
return Scaffold(
backgroundColor: Colors.grey.shade50,
body: SafeArea(
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 400),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: 80, height: 80,
decoration: const BoxDecoration(
color: Colors.transparent,
shape: BoxShape.circle,
),
child: Center(child: Image.asset('assets/logo.png', width: 80, height: 80, fit: BoxFit.contain)),
),
const SizedBox(height: 32),
Text(
'Welcome to Kifi',
style: Theme.of(context).textTheme.headlineMedium?.copyWith(fontWeight: FontWeight.bold, color: Colors.black87),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
'Manage your inventory and expenses securely.',
style: Theme.of(context).textTheme.bodyMedium?.copyWith(color: Colors.grey.shade600),
textAlign: TextAlign.center,
),
const SizedBox(height: 32),
Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: Colors.grey.shade200,
borderRadius: BorderRadius.circular(16),
),
child: Row(
children: [
Expanded(
child: InkWell(
onTap: () => toggleMode(true),
borderRadius: BorderRadius.circular(12),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: isLogin ? Colors.white : Colors.transparent,
borderRadius: BorderRadius.circular(12),
boxShadow: isLogin ? [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 4, offset: const Offset(0, 2))] : [],
),
child: Center(child: Text('Login', style: TextStyle(fontWeight: FontWeight.w600, color: isLogin ? Colors.black87 : Colors.grey.shade600))),
),
),
),
Expanded(
child: InkWell(
onTap: () => toggleMode(false),
borderRadius: BorderRadius.circular(12),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
padding: const EdgeInsets.symmetric(vertical: 12),
decoration: BoxDecoration(
color: !isLogin ? Colors.white : Colors.transparent,
borderRadius: BorderRadius.circular(12),
boxShadow: !isLogin ? [BoxShadow(color: Colors.black.withOpacity(0.05), blurRadius: 4, offset: const Offset(0, 2))] : [],
),
child: Center(child: Text('Sign Up', style: TextStyle(fontWeight: FontWeight.w600, color: !isLogin ? Colors.black87 : Colors.grey.shade600))),
),
),
),
],
),
),
const SizedBox(height: 32),
AnimatedSize(
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
alignment: Alignment.topCenter,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(fontWeight: FontWeight.w500, color: Colors.black87),
textInputAction: TextInputAction.next,
decoration: _buildInputDecoration(isLogin ? 'Email or Username' : 'Email Address', LucideIcons.mail),
),
const SizedBox(height: 16),
TextField(
controller: passwordController,
obscureText: true,
style: const TextStyle(fontWeight: FontWeight.w500, color: Colors.black87),
textInputAction: TextInputAction.done,
onSubmitted: (_) => submit(),
decoration: _buildInputDecoration('Password', LucideIcons.lock),
),
AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: isLogin
? Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(top: 12),
child: TextButton(
onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (_) => const ForgotPasswordScreen())),
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).primaryColor,
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: const Text('Forgot Password?', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 13)),
),
),
)
: const SizedBox(height: 12),
),
],
),
),
const SizedBox(height: 32),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: isLoading ? null : submit,
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 20),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
elevation: 2,
),
child: isLoading
? const SizedBox(height: 24, width: 24, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
: Text(isLogin ? 'Log In' : 'Create Account', style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold, letterSpacing: 0.5)),
),
),
],
),
),
),
),
),
);
}
}