import 'package:flutter/material.dart'; import '../services/supabase_service.dart'; import '../constants/app_colors.dart'; import '../screens/logado_screen.dart'; class RegistrarSheet extends StatefulWidget { const RegistrarSheet({super.key}); @override State createState() => _RegistrarSheetState(); } class _RegistrarSheetState extends State { final _formKey = GlobalKey(); final _nameController = TextEditingController(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _confirmPasswordController = TextEditingController(); bool _isLoading = false; Future _handleRegister() async { if (_formKey.currentState!.validate()) { setState(() => _isLoading = true); print( 'DEBUG: Dados do formulário - Nome: ${_nameController.text}, Email: ${_emailController.text}', ); try { await SupabaseService.signUp( email: _emailController.text.trim(), password: _passwordController.text, name: _nameController.text.trim(), ); if (mounted) { // Close the bottom sheet first Navigator.of(context).pop(); // Show success message above the sheet ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Conta criada com sucesso! Verifique seu email.'), backgroundColor: Colors.green, ), ); // Navigate to LogadoScreen Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (context) => const LogadoScreen()), ); } } catch (e) { print('DEBUG: Erro no registro: $e'); if (mounted) { // Close the bottom sheet first Navigator.of(context).pop(); // Show error message above the sheet ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(e.toString()), backgroundColor: Colors.red), ); } } finally { if (mounted) { setState(() => _isLoading = false); } } } } @override void dispose() { _nameController.dispose(); _emailController.dispose(); _passwordController.dispose(); _confirmPasswordController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return DraggableScrollableSheet( initialChildSize: 0.7, // Start at 70% for register form minChildSize: 0.5, // 50% minimum maxChildSize: 0.95, // 95% maximum snap: true, // Enable snap points snapSizes: [0.7, 0.95], // Snap to 70% or 95% builder: (context, scrollController) { return Container( decoration: BoxDecoration( color: AppColors.backgroundGrey, boxShadow: [ BoxShadow( color: Colors.black.withValues(alpha: 0.1), blurRadius: 10, offset: const Offset(0, -5), ), ], ), child: SingleChildScrollView( controller: scrollController, child: SafeArea( child: Padding( padding: const EdgeInsets.all(24.0), child: _buildRegistrationForm(), ), ), ), ); }, ); } Widget _buildRegistrationForm() { return Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Drag handle Center( child: Container( width: 60, height: 6, margin: const EdgeInsets.symmetric(vertical: 8), decoration: BoxDecoration( color: Colors.grey[400], borderRadius: BorderRadius.circular(3), ), ), ), const SizedBox(height: 24), // Title const Text( 'Registrar', style: TextStyle( fontSize: 28, fontWeight: FontWeight.bold, color: Colors.white, ), ), const SizedBox(height: 32), // Name field const Text( 'Nome', style: TextStyle(fontSize: 16, color: Colors.white70), ), const SizedBox(height: 8), TextFormField( controller: _nameController, decoration: InputDecoration( filled: true, fillColor: AppColors.white.withValues(alpha: 0.5), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none, ), hintText: 'Seu nome completo', hintStyle: const TextStyle(color: Colors.white38), ), style: const TextStyle(color: Colors.white), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, insira seu nome'; } if (value.length < 3) { return 'Nome deve ter pelo menos 3 caracteres'; } return null; }, ), const SizedBox(height: 20), // Email field const Text( 'Email', style: TextStyle(fontSize: 16, color: Colors.white70), ), const SizedBox(height: 8), TextFormField( controller: _emailController, decoration: InputDecoration( filled: true, fillColor: AppColors.white.withValues(alpha: 0.5), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none, ), hintText: 'seu@email.com', hintStyle: const TextStyle(color: Colors.white38), ), style: const TextStyle(color: Colors.white), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, insira seu email'; } if (!value.contains('@')) { return 'Email inválido'; } return null; }, ), const SizedBox(height: 20), // Password field const Text( 'Senha', style: TextStyle(fontSize: 16, color: Colors.white70), ), const SizedBox(height: 8), TextFormField( controller: _passwordController, obscureText: true, decoration: InputDecoration( filled: true, fillColor: AppColors.white.withValues(alpha: 0.5), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none, ), hintText: '••••••••', hintStyle: const TextStyle(color: Colors.white38), ), style: const TextStyle(color: Colors.white), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, insira sua senha'; } if (value.length < 6) { return 'Senha deve ter pelo menos 6 caracteres'; } return null; }, ), const SizedBox(height: 20), // Confirm password field const Text( 'Confirmar Senha', style: TextStyle(fontSize: 16, color: Colors.white70), ), const SizedBox(height: 8), TextFormField( controller: _confirmPasswordController, obscureText: true, decoration: InputDecoration( filled: true, fillColor: AppColors.white.withValues(alpha: 0.5), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: BorderSide.none, ), hintText: '••••••••', hintStyle: const TextStyle(color: Colors.white38), ), style: const TextStyle(color: Colors.white), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, confirme sua senha'; } if (value != _passwordController.text) { return 'Senhas não coincidem'; } return null; }, ), const SizedBox(height: 32), // Register button SizedBox( width: double.infinity, height: 60, child: ElevatedButton( onPressed: _isLoading ? null : _handleRegister, style: ElevatedButton.styleFrom( backgroundColor: AppColors.buttonColor, foregroundColor: AppColors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(30), ), elevation: 5, ), child: _isLoading ? const CircularProgressIndicator(color: Colors.white) : const Text( 'Registrar', style: TextStyle( fontSize: 18, fontWeight: FontWeight.w600, ), ), ), ), SizedBox(height: 40), ], ), ); } }