import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../database/database_helper.dart'; import '../../widgets/password_strength_indicator.dart'; const _kBg = Color(0xFF0A1E3C); const _kTopBar = Color(0xFF0D2D6B); const _kPanel = Color(0xFF0F2A4A); const _kCard = Color(0xFF163558); const _kBorder = Color(0xFF1E4070); const _kAccent = Color(0xFF1976D2); const _kTextMuted = Color(0xFFAABBCC); const _kError = Color(0xFFFF6B6B); class RegistroScreen extends StatefulWidget { const RegistroScreen({Key? key}) : super(key: key); @override State createState() => _RegistroScreenState(); } class _RegistroScreenState extends State { final _formKey = GlobalKey(); final _nombreCtrl = TextEditingController(); final _emailCtrl = TextEditingController(); final _telCtrl = TextEditingController(); final _empresaCtrl = TextEditingController(); final _rucCtrl = TextEditingController(); final _passCtrl = TextEditingController(); final _confirmCtrl = TextEditingController(); bool _obscurePass = true; bool _obscureConfirm = true; bool _loading = false; String? _error; @override void dispose() { _nombreCtrl.dispose(); _emailCtrl.dispose(); _telCtrl.dispose(); _empresaCtrl.dispose(); _rucCtrl.dispose(); _passCtrl.dispose(); _confirmCtrl.dispose(); super.dispose(); } Future _crear() async { if (!(_formKey.currentState?.validate() ?? false)) return; setState(() { _loading = true; _error = null; }); final ok = await DatabaseHelper().registerUsuario( nombre: _nombreCtrl.text.trim(), email: _emailCtrl.text.trim(), telefono: _telCtrl.text.trim(), empresa: _empresaCtrl.text.trim(), ruc: _rucCtrl.text.trim(), password: _passCtrl.text, ); if (!mounted) return; setState(() => _loading = false); if (ok) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Cuenta creada. Ya puedes iniciar sesión.'), backgroundColor: Color(0xFF2E7D32), ), ); Navigator.pop(context); } else { setState(() => _error = 'Ya existe una cuenta con ese correo electrónico.'); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: _kBg, body: Stack( children: [ Container( height: 160, decoration: const BoxDecoration( gradient: LinearGradient( colors: [_kTopBar, _kBg], begin: Alignment.topCenter, end: Alignment.bottomCenter, ), ), ), SafeArea( child: Column( children: [ _buildTopBar(), Expanded( child: SingleChildScrollView( child: _buildPanel(), ), ), ], ), ), ], ), ); } Widget _buildTopBar() { return Padding( padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4), child: Row( children: [ IconButton( icon: const Icon(Icons.arrow_back, color: Colors.white), onPressed: () => Navigator.pop(context), ), const Text( 'Crear cuenta local', style: TextStyle( color: Colors.white, fontSize: 17, fontWeight: FontWeight.w600), ), ], ), ); } Widget _buildPanel() { return Container( width: double.infinity, constraints: BoxConstraints( minHeight: MediaQuery.of(context).size.height - 120, ), decoration: const BoxDecoration( color: _kPanel, borderRadius: BorderRadius.only( topLeft: Radius.circular(32), topRight: Radius.circular(32), ), ), padding: const EdgeInsets.fromLTRB(24, 28, 24, 40), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _sectionLabel('DATOS PERSONALES'), const SizedBox(height: 14), _field(_nombreCtrl, 'Nombre completo', Icons.person_outline, validator: _notEmpty), const SizedBox(height: 13), _field(_emailCtrl, 'Correo electrónico', Icons.email_outlined, keyboardType: TextInputType.emailAddress, validator: (v) { if (v == null || v.trim().isEmpty) return 'Obligatorio'; if (!v.contains('@') || !v.contains('.')) { return 'Correo inválido'; } return null; }), const SizedBox(height: 13), _field(_telCtrl, 'Teléfono', Icons.phone_outlined, keyboardType: TextInputType.phone), const SizedBox(height: 22), _sectionLabel('EMPRESA'), const SizedBox(height: 14), _field(_empresaCtrl, 'Nombre empresa', Icons.business_outlined, validator: _notEmpty), const SizedBox(height: 13), _field(_rucCtrl, 'RUC / Cédula', Icons.tag_outlined, keyboardType: TextInputType.number, inputFormatters: [FilteringTextInputFormatter.digitsOnly]), const SizedBox(height: 22), _sectionLabel('SEGURIDAD'), const SizedBox(height: 14), _passwordField(_passCtrl, 'Contraseña', _obscurePass, onToggle: () => setState(() => _obscurePass = !_obscurePass), validator: (v) { if (v == null || v.isEmpty) return 'Obligatorio'; if (v.length < 8) return 'Mínimo 8 caracteres'; return null; }), ValueListenableBuilder( valueListenable: _passCtrl, builder: (_, val, __) => PasswordStrengthIndicator(password: val.text), ), const SizedBox(height: 13), _passwordField(_confirmCtrl, 'Confirmar contraseña', _obscureConfirm, onToggle: () => setState(() => _obscureConfirm = !_obscureConfirm), validator: (v) { if (v == null || v.isEmpty) return 'Obligatorio'; if (v != _passCtrl.text) return 'Las contraseñas no coinciden'; return null; }), if (_error != null) ...[ const SizedBox(height: 14), _errorBox(_error!), ], const SizedBox(height: 24), _buildButton(), ], ), ), ); } Widget _sectionLabel(String text) { return Text( text, style: const TextStyle( color: _kAccent, fontSize: 11, fontWeight: FontWeight.bold, letterSpacing: 1.5, ), ); } Widget _field( TextEditingController ctrl, String label, IconData icon, { TextInputType? keyboardType, List? inputFormatters, String? Function(String?)? validator, }) { return TextFormField( controller: ctrl, keyboardType: keyboardType, inputFormatters: inputFormatters, validator: validator, style: const TextStyle(color: Colors.white, fontSize: 14), decoration: _deco(label, icon), ); } Widget _passwordField( TextEditingController ctrl, String label, bool obscure, { required VoidCallback onToggle, String? Function(String?)? validator, }) { return TextFormField( controller: ctrl, obscureText: obscure, validator: validator, style: const TextStyle(color: Colors.white, fontSize: 14), decoration: _deco(label, Icons.lock_outline).copyWith( suffixIcon: IconButton( icon: Icon( obscure ? Icons.visibility_outlined : Icons.visibility_off_outlined, color: _kTextMuted, size: 20, ), onPressed: onToggle, ), ), ); } InputDecoration _deco(String label, IconData icon) { return InputDecoration( labelText: label, labelStyle: const TextStyle(color: _kTextMuted, fontSize: 13), prefixIcon: Icon(icon, color: _kTextMuted, size: 20), filled: true, fillColor: _kCard, contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _kBorder)), focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _kAccent, width: 2)), errorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _kError)), focusedErrorBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(12), borderSide: const BorderSide(color: _kError, width: 2)), errorStyle: const TextStyle(color: _kError, fontSize: 11), ); } Widget _errorBox(String msg) { return Container( width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10), decoration: BoxDecoration( color: _kError.withOpacity(0.10), borderRadius: BorderRadius.circular(8), border: Border.all(color: _kError.withOpacity(0.35)), ), child: Row( children: [ const Icon(Icons.error_outline, color: _kError, size: 16), const SizedBox(width: 8), Expanded( child: Text(msg, style: const TextStyle(fontSize: 12, color: _kError))), ], ), ); } Widget _buildButton() { return SizedBox( width: double.infinity, height: 52, child: DecoratedBox( decoration: BoxDecoration( gradient: _loading ? null : const LinearGradient( colors: [_kTopBar, _kAccent], begin: Alignment.centerLeft, end: Alignment.centerRight), color: _loading ? _kBorder : null, borderRadius: BorderRadius.circular(12), boxShadow: _loading ? const [] : [ BoxShadow( color: _kAccent.withOpacity(0.4), blurRadius: 12, offset: const Offset(0, 4)) ], ), child: ElevatedButton( onPressed: _loading ? null : _crear, style: ElevatedButton.styleFrom( backgroundColor: Colors.transparent, foregroundColor: Colors.white, shadowColor: Colors.transparent, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12)), ), child: _loading ? const SizedBox( width: 22, height: 22, child: CircularProgressIndicator( strokeWidth: 2.5, color: Colors.white)) : const Text( 'CREAR CUENTA', style: TextStyle( fontSize: 15, fontWeight: FontWeight.bold, letterSpacing: 1.2), ), ), ), ); } String? _notEmpty(String? v) => (v == null || v.trim().isEmpty) ? 'Obligatorio' : null; }