fix: soporte visible + perfil con vinculacion de correo
- Setting.fromDocument con valores por defecto (null-safe) - SettingsProvider crea Setting vacio si el backend falla - support_view: guard para settings null - profile_view: seccion 'Acceso con correo' con toggle, campos email/password/confirmar, envia OTP, verifica codigo Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
bfa494b7fe
commit
8372e19d9f
+28
-43
@@ -39,50 +39,35 @@ class Setting {
|
|||||||
required this.terminosCondicionesBody,
|
required this.terminosCondicionesBody,
|
||||||
});
|
});
|
||||||
|
|
||||||
static Setting fromDocument(Map<String, dynamic> doc) {
|
static bool _b(dynamic v, [bool d = false]) {
|
||||||
return Setting(
|
if (v == null) return d;
|
||||||
tarifas: doc['tarifas'] as bool,
|
if (v is bool) return v;
|
||||||
domicilios: doc['domicilios'] as bool,
|
return v.toString() == 'true';
|
||||||
google: doc['google'] as bool,
|
|
||||||
versionIos: doc['version_ios'] as String,
|
|
||||||
versionAndroid: doc['version_android'] as String,
|
|
||||||
horaNotificacion: doc['hora_notificacion'] as String,
|
|
||||||
tituloSoporte: doc['titulo_soporte'] as String,
|
|
||||||
parrafoSoporte: doc['parrafo_soporte'] as String,
|
|
||||||
numeroSoporte: doc['numero_soporte'] as String,
|
|
||||||
emailSoporte: doc['email_soporte'] as String,
|
|
||||||
diasSoporte: doc['dias_soporte'] as String,
|
|
||||||
horasSoporte: doc['horas_soporte'] as String,
|
|
||||||
politicasPrivacidad: doc['politicas_privacidad'] as String,
|
|
||||||
politicasPrivacidadTitle: doc['politicas_privacidad_title'] as String,
|
|
||||||
politicasPrivacidadBody: doc['politicas_privacidad_body'] as String,
|
|
||||||
terminosCondiciones: doc['terminos_condiciones'] as String,
|
|
||||||
terminosCondicionesTitle: doc['terminos_condiciones_title'] as String,
|
|
||||||
terminosCondicionesBody: doc['terminos_condiciones_body'] as String,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
static String _s(dynamic v, [String d = '']) => v?.toString() ?? d;
|
||||||
String toString() {
|
|
||||||
return '''Setting {
|
static Setting fromDocument(Map<String, dynamic> doc) {
|
||||||
tarifas: $tarifas
|
return Setting(
|
||||||
domicilios: $domicilios
|
tarifas: _b(doc['tarifas']),
|
||||||
google: $google
|
domicilios: _b(doc['domicilios']),
|
||||||
versionIos: $versionIos
|
google: _b(doc['google']),
|
||||||
versionAndroid: $versionAndroid
|
versionIos: _s(doc['version_ios']),
|
||||||
horaNotificacion: $horaNotificacion
|
versionAndroid: _s(doc['version_android']),
|
||||||
tituloSoporte: $tituloSoporte
|
horaNotificacion: _s(doc['hora_notificacion']),
|
||||||
parrafoSoporte: $parrafoSoporte
|
tituloSoporte: _s(doc['titulo_soporte'], 'Soporte ProsApp'),
|
||||||
numeroSoporte: $numeroSoporte
|
parrafoSoporte: _s(doc['parrafo_soporte'],
|
||||||
emailSoporte: $emailSoporte
|
'Estamos aquí para ayudarte. Contáctanos por cualquiera de estos canales.'),
|
||||||
diasSoporte: $diasSoporte
|
numeroSoporte: _s(doc['numero_soporte'] ?? doc['support_phone']),
|
||||||
horasSoporte: $horasSoporte
|
emailSoporte: _s(doc['email_soporte'] ?? doc['support_email']),
|
||||||
politicasPrivacidad: $politicasPrivacidad
|
diasSoporte: _s(doc['dias_soporte'] ?? doc['support_days'], 'Lunes a Viernes'),
|
||||||
politicasPrivacidadTitle: $politicasPrivacidadTitle
|
horasSoporte: _s(doc['horas_soporte'] ?? doc['support_hours'], '8:00 am - 6:00 pm'),
|
||||||
politicasPrivacidadBody: $politicasPrivacidadBody
|
politicasPrivacidad: _s(doc['politicas_privacidad']),
|
||||||
terminosCondiciones: $terminosCondiciones
|
politicasPrivacidadTitle: _s(doc['politicas_privacidad_title']),
|
||||||
terminosCondicionesTitle: $terminosCondicionesTitle
|
politicasPrivacidadBody: _s(doc['politicas_privacidad_body']),
|
||||||
terminosCondicionesBody: $terminosCondicionesBody
|
terminosCondiciones: _s(doc['terminos_condiciones']),
|
||||||
}''';
|
terminosCondicionesTitle: _s(doc['terminos_condiciones_title']),
|
||||||
|
terminosCondicionesBody: _s(doc['terminos_condiciones_body']),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,13 @@ class SettingsProvider extends ChangeNotifier {
|
|||||||
getSettings() async {
|
getSettings() async {
|
||||||
try {
|
try {
|
||||||
final data = await _api.get('/settings');
|
final data = await _api.get('/settings');
|
||||||
settings = Setting.fromDocument(data as Map<String, dynamic>);
|
if (data != null) {
|
||||||
|
settings = Setting.fromDocument(data as Map<String, dynamic>);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('Error obteniendo los settings: $e');
|
print('Error obteniendo los settings: $e');
|
||||||
} finally {
|
} finally {
|
||||||
|
settings ??= Setting.fromDocument({});
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|||||||
+305
-11
@@ -7,6 +7,7 @@ import 'package:prosapp_web_app/providers/auth_provider.dart';
|
|||||||
import 'package:prosapp_web_app/providers/cities_provider.dart';
|
import 'package:prosapp_web_app/providers/cities_provider.dart';
|
||||||
import 'package:prosapp_web_app/providers/profile_form_provider.dart';
|
import 'package:prosapp_web_app/providers/profile_form_provider.dart';
|
||||||
import 'package:prosapp_web_app/router/router.dart';
|
import 'package:prosapp_web_app/router/router.dart';
|
||||||
|
import 'package:prosapp_web_app/services/api_service.dart';
|
||||||
import 'package:prosapp_web_app/services/navigation_service.dart';
|
import 'package:prosapp_web_app/services/navigation_service.dart';
|
||||||
import 'package:prosapp_web_app/services/notifications_service.dart';
|
import 'package:prosapp_web_app/services/notifications_service.dart';
|
||||||
import 'package:prosapp_web_app/ui/cards/white_card.dart';
|
import 'package:prosapp_web_app/ui/cards/white_card.dart';
|
||||||
@@ -74,23 +75,25 @@ class _ProfileViewBody extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
_AvatarContainer(containerFull: true),
|
_AvatarContainer(containerFull: true),
|
||||||
_ProfileViewForm(),
|
_ProfileViewForm(),
|
||||||
|
_EmailLinkCard(),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return Center(
|
return Center(
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
constraints: const BoxConstraints(maxWidth: 900),
|
constraints: const BoxConstraints(maxWidth: 900),
|
||||||
child: Table(
|
child: const Column(
|
||||||
columnWidths: const {
|
children: [
|
||||||
0: FixedColumnWidth(250),
|
IntrinsicHeight(
|
||||||
},
|
child: Row(
|
||||||
children: const [
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
TableRow(
|
children: [
|
||||||
children: [
|
SizedBox(width: 250, child: _AvatarContainer(containerFull: true)),
|
||||||
_AvatarContainer(containerFull: true),
|
Expanded(child: _ProfileViewForm()),
|
||||||
_ProfileViewForm(),
|
],
|
||||||
],
|
),
|
||||||
)
|
),
|
||||||
|
_EmailLinkCard(),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -370,3 +373,294 @@ class _AvatarContainer extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Sección vinculación correo ───────────────────────────────────────────────
|
||||||
|
|
||||||
|
class _EmailLinkCard extends StatefulWidget {
|
||||||
|
const _EmailLinkCard();
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<_EmailLinkCard> createState() => _EmailLinkCardState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _EmailLinkCardState extends State<_EmailLinkCard> {
|
||||||
|
final _emailCtrl = TextEditingController();
|
||||||
|
final _passCtrl = TextEditingController();
|
||||||
|
final _confirmCtrl = TextEditingController();
|
||||||
|
final _codeCtrl = TextEditingController();
|
||||||
|
|
||||||
|
bool _enabled = false;
|
||||||
|
bool _codeSent = false;
|
||||||
|
bool _loading = false;
|
||||||
|
bool _obscurePass = true;
|
||||||
|
bool _obscureConfirm = true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
final user = context.read<AuthProvider>().user!;
|
||||||
|
_enabled = user.email != null && user.email!.isNotEmpty;
|
||||||
|
if (_enabled) _emailCtrl.text = user.email!;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_emailCtrl.dispose();
|
||||||
|
_passCtrl.dispose();
|
||||||
|
_confirmCtrl.dispose();
|
||||||
|
_codeCtrl.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _sendCode() async {
|
||||||
|
final email = _emailCtrl.text.trim();
|
||||||
|
final pass = _passCtrl.text;
|
||||||
|
final confirm = _confirmCtrl.text;
|
||||||
|
|
||||||
|
if (!RegExp(r'^[\w.+-]+@[\w-]+\.\w+$').hasMatch(email)) {
|
||||||
|
_snack('Ingresa un correo válido', error: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pass.length < 6) {
|
||||||
|
_snack('La contraseña debe tener al menos 6 caracteres', error: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pass != confirm) {
|
||||||
|
_snack('Las contraseñas no coinciden', error: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() => _loading = true);
|
||||||
|
try {
|
||||||
|
await ApiService.instance.post('/auth/send-email-otp', {'email': email});
|
||||||
|
setState(() => _codeSent = true);
|
||||||
|
_snack('Código enviado a $email');
|
||||||
|
} catch (e) {
|
||||||
|
_snack('Error al enviar el código', error: true);
|
||||||
|
} finally {
|
||||||
|
setState(() => _loading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _verify() async {
|
||||||
|
final code = _codeCtrl.text.trim();
|
||||||
|
if (code.length != 6) {
|
||||||
|
_snack('Ingresa el código de 6 dígitos', error: true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() => _loading = true);
|
||||||
|
try {
|
||||||
|
await ApiService.instance.post('/auth/link-email-otp', {
|
||||||
|
'email': _emailCtrl.text.trim(),
|
||||||
|
'password': _passCtrl.text,
|
||||||
|
'code': code,
|
||||||
|
});
|
||||||
|
await context.read<AuthProvider>().refreshUser();
|
||||||
|
setState(() { _codeSent = false; _codeCtrl.clear(); });
|
||||||
|
_snack('Correo vinculado exitosamente');
|
||||||
|
} catch (e) {
|
||||||
|
_snack('Código incorrecto o expirado', error: true);
|
||||||
|
} finally {
|
||||||
|
setState(() => _loading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _snack(String msg, {bool error = false}) {
|
||||||
|
if (error) {
|
||||||
|
NotificationsService.showSnackBarError(msg);
|
||||||
|
} else {
|
||||||
|
NotificationsService.showSnackbar(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final user = context.watch<AuthProvider>().user!;
|
||||||
|
final hasEmail = user.email != null && user.email!.isNotEmpty;
|
||||||
|
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||||
|
final fillColor = isDark ? const Color(0xFF0F172A) : const Color(0xFFF5F8FF);
|
||||||
|
final borderColor = isDark ? const Color(0xFF334155) : const Color(0xFFE0E7FF);
|
||||||
|
|
||||||
|
InputDecoration _fieldDeco(String label, String hint, IconData icon) =>
|
||||||
|
InputDecoration(
|
||||||
|
labelText: label,
|
||||||
|
hintText: hint,
|
||||||
|
prefixIcon: Icon(icon, size: 20),
|
||||||
|
filled: true,
|
||||||
|
fillColor: fillColor,
|
||||||
|
border: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(10), borderSide: BorderSide.none),
|
||||||
|
enabledBorder: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
borderSide: BorderSide(color: borderColor)),
|
||||||
|
focusedBorder: OutlineInputBorder(
|
||||||
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
borderSide: const BorderSide(color: Color(0xFF42A4EF), width: 2)),
|
||||||
|
);
|
||||||
|
|
||||||
|
return WhiteCard(
|
||||||
|
title: 'Acceso con correo',
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
hasEmail
|
||||||
|
? 'Correo vinculado: ${user.email}'
|
||||||
|
: 'Vincula un correo para iniciar sesión también con email y contraseña.',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 13,
|
||||||
|
color: isDark ? Colors.white54 : Colors.black54,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Switch(
|
||||||
|
value: _enabled,
|
||||||
|
activeColor: const Color(0xFF42A4EF),
|
||||||
|
onChanged: (val) {
|
||||||
|
if (hasEmail) return; // ya vinculado, no desactivar desde aquí
|
||||||
|
setState(() {
|
||||||
|
_enabled = val;
|
||||||
|
_codeSent = false;
|
||||||
|
_codeCtrl.clear();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
|
||||||
|
if (_enabled && !hasEmail) ...[
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
|
||||||
|
if (!_codeSent) ...[
|
||||||
|
TextField(
|
||||||
|
controller: _emailCtrl,
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
decoration: _fieldDeco('Correo electrónico', 'tu@correo.com', Icons.email_outlined),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: _passCtrl,
|
||||||
|
obscureText: _obscurePass,
|
||||||
|
decoration: _fieldDeco('Contraseña', 'Mínimo 6 caracteres', Icons.lock_outline).copyWith(
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(_obscurePass ? Icons.visibility_off_outlined : Icons.visibility_outlined, size: 20),
|
||||||
|
onPressed: () => setState(() => _obscurePass = !_obscurePass),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: _confirmCtrl,
|
||||||
|
obscureText: _obscureConfirm,
|
||||||
|
decoration: _fieldDeco('Confirmar contraseña', 'Repite la contraseña', Icons.lock_outline).copyWith(
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(_obscureConfirm ? Icons.visibility_off_outlined : Icons.visibility_outlined, size: 20),
|
||||||
|
onPressed: () => setState(() => _obscureConfirm = !_obscureConfirm),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
SizedBox(
|
||||||
|
width: double.infinity,
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: _loading ? null : _sendCode,
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: const Color(0xFF42A4EF),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||||
|
elevation: 0,
|
||||||
|
),
|
||||||
|
child: _loading
|
||||||
|
? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
|
||||||
|
: const Text('Enviar código de verificación', style: TextStyle(fontWeight: FontWeight.w600)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
] else ...[
|
||||||
|
Container(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: const Color(0xFF42A4EF).withOpacity(0.1),
|
||||||
|
borderRadius: BorderRadius.circular(10),
|
||||||
|
border: Border.all(color: const Color(0xFF42A4EF).withOpacity(0.3)),
|
||||||
|
),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.email_outlined, color: Color(0xFF42A4EF), size: 18),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
'Código enviado a ${_emailCtrl.text.trim()}',
|
||||||
|
style: const TextStyle(fontSize: 13, color: Color(0xFF42A4EF)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: _codeCtrl,
|
||||||
|
keyboardType: TextInputType.number,
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
maxLength: 6,
|
||||||
|
style: const TextStyle(fontSize: 26, fontWeight: FontWeight.bold, letterSpacing: 10),
|
||||||
|
decoration: _fieldDeco('', '------', Icons.pin_outlined).copyWith(
|
||||||
|
counterText: '',
|
||||||
|
hintStyle: const TextStyle(letterSpacing: 10, color: Colors.grey),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton(
|
||||||
|
onPressed: _loading ? null : () => setState(() { _codeSent = false; _codeCtrl.clear(); }),
|
||||||
|
style: OutlinedButton.styleFrom(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||||
|
side: const BorderSide(color: Color(0xFF42A4EF)),
|
||||||
|
),
|
||||||
|
child: const Text('← Cambiar datos', style: TextStyle(color: Color(0xFF42A4EF))),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 10),
|
||||||
|
Expanded(
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: _loading ? null : _verify,
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: const Color(0xFF42A4EF),
|
||||||
|
foregroundColor: Colors.white,
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||||
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||||
|
elevation: 0,
|
||||||
|
),
|
||||||
|
child: _loading
|
||||||
|
? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2))
|
||||||
|
: const Text('Verificar', style: TextStyle(fontWeight: FontWeight.w600)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
if (hasEmail) ...[
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.verified, color: Color(0xFF10B981), size: 18),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
Text('Correo verificado', style: TextStyle(color: isDark ? Colors.white70 : Colors.black87, fontSize: 13)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,6 +58,10 @@ class SupportView extends StatelessWidget {
|
|||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settingsProvider.settings == null) {
|
||||||
|
return const Center(child: Text('No se pudieron cargar los datos de soporte.'));
|
||||||
|
}
|
||||||
|
|
||||||
final settings = settingsProvider.settings!;
|
final settings = settingsProvider.settings!;
|
||||||
|
|
||||||
return ListView(
|
return ListView(
|
||||||
|
|||||||
Reference in New Issue
Block a user