import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:lucide_icons/lucide_icons.dart'; import '../providers/auth_provider.dart'; import '../../dashboard/presentation/dashboard_screen.dart'; import 'setup_wizard_screen.dart'; import '../../../../core/network/dio_client.dart'; class OtpScreen extends ConsumerStatefulWidget { final String email; const OtpScreen({super.key, required this.email}); @override ConsumerState createState() => _OtpScreenState(); } class _OtpScreenState extends ConsumerState { final otpController = TextEditingController(); Future verify() async { final otp = otpController.text.trim(); if (otp.isEmpty) return; final success = await ref .read(authControllerProvider.notifier) .verifyOtp(widget.email, otp); if (success && mounted) { try { await Future.delayed(const Duration(milliseconds: 300)); final res = await DioClient().dio.get('/account/setup/status'); final status = res.data['status']; if (status == 'COMPLETED' && mounted) { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => const DashboardScreen()), (route) => false, ); } else if (mounted) { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => const SetupWizardScreen()), (route) => false, ); } } catch (_) { if (mounted) { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => const SetupWizardScreen()), (route) => false, ); } } } } @override Widget build(BuildContext context) { ref.listen>(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 GestureDetector( onTap: () => FocusScope.of(context).unfocus(), child: Scaffold( backgroundColor: Colors.grey.shade50, appBar: AppBar( elevation: 0, backgroundColor: Colors.transparent, leading: Padding( padding: const EdgeInsets.only(left: 16.0), child: IconButton( onPressed: () => Navigator.pop(context), icon: const Icon(LucideIcons.chevronLeft, size: 28), color: Colors.black87, ), ), ), body: SafeArea( child: Center( child: SingleChildScrollView( padding: const EdgeInsets.all(24.0), child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Container( width: 80, height: 80, decoration: BoxDecoration( color: Theme.of(context).primaryColor.withOpacity(0.1), shape: BoxShape.circle, ), child: Center( child: Icon( LucideIcons.mailCheck, size: 40, color: Theme.of(context).primaryColor, ), ), ), const SizedBox(height: 32), Text( 'Verify Email', style: Theme.of(context).textTheme.headlineMedium ?.copyWith( fontWeight: FontWeight.bold, color: Colors.black87, ), textAlign: TextAlign.center, ), const SizedBox(height: 8), Text( 'Enter the 6-digit OTP sent to\n${widget.email}', style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Colors.grey.shade600, height: 1.5, ), textAlign: TextAlign.center, ), const SizedBox(height: 48), TextField( controller: otpController, keyboardType: TextInputType.number, textAlign: TextAlign.center, maxLength: 6, style: const TextStyle( fontSize: 24, letterSpacing: 16, fontWeight: FontWeight.bold, ), decoration: InputDecoration( hintText: '000000', hintStyle: TextStyle( letterSpacing: 16, color: Colors.grey.shade300, ), counterText: '', filled: true, fillColor: Colors.white, contentPadding: const EdgeInsets.symmetric( horizontal: 16, vertical: 20, ), 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, ), ), ), ), const SizedBox(height: 32), SizedBox( width: double.infinity, child: ElevatedButton( onPressed: isLoading ? null : verify, 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, ), ) : const Text( 'Verify Account', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, letterSpacing: 0.5, ), ), ), ), ], ), ), ), ), ), ), ); } }