582 lines
19 KiB
Dart
582 lines
19 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../../providers/app_provider.dart';
|
|
|
|
// Paleta exclusiva de pantallas de autenticación (spec estructura_login.md §7)
|
|
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 LoginScreen extends StatefulWidget {
|
|
const LoginScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<LoginScreen> createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen>
|
|
with SingleTickerProviderStateMixin {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _emailCtrl = TextEditingController();
|
|
final _passCtrl = TextEditingController();
|
|
|
|
bool _obscure = true;
|
|
bool _rememberMe = true;
|
|
bool _modoCloud = true;
|
|
|
|
late final AnimationController _toggleAnim;
|
|
late final Animation<double> _toggleSlide;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_toggleAnim = AnimationController(
|
|
vsync: this,
|
|
duration: const Duration(milliseconds: 240),
|
|
value: 1.0, // arranca en Cloud (posición derecha)
|
|
);
|
|
_toggleSlide = CurvedAnimation(
|
|
parent: _toggleAnim, curve: Curves.easeInOut);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_emailCtrl.dispose();
|
|
_passCtrl.dispose();
|
|
_toggleAnim.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _switchModo(bool cloud) {
|
|
if (_modoCloud == cloud) return;
|
|
setState(() => _modoCloud = cloud);
|
|
if (cloud) {
|
|
_toggleAnim.forward();
|
|
} else {
|
|
_toggleAnim.reverse();
|
|
}
|
|
}
|
|
|
|
Future<void> _ingresar() async {
|
|
if (!(_formKey.currentState?.validate() ?? false)) return;
|
|
final provider = context.read<AppProvider>();
|
|
if (_modoCloud) {
|
|
await provider.login(
|
|
_emailCtrl.text.trim(),
|
|
_passCtrl.text,
|
|
rememberMe: _rememberMe,
|
|
);
|
|
} else {
|
|
await provider.loginLocal(
|
|
_emailCtrl.text.trim(),
|
|
_passCtrl.text,
|
|
rememberMe: _rememberMe,
|
|
);
|
|
}
|
|
// Si ok, el Consumer en main.dart navega a HomeScreen automáticamente
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final provider = context.watch<AppProvider>();
|
|
return Scaffold(
|
|
backgroundColor: _kBg,
|
|
body: Stack(
|
|
children: [
|
|
// Degradado superior
|
|
Container(
|
|
height: MediaQuery.of(context).size.height * 0.44,
|
|
decoration: const BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [_kTopBar, _kBg],
|
|
begin: Alignment.topCenter,
|
|
end: Alignment.bottomCenter,
|
|
),
|
|
),
|
|
),
|
|
SafeArea(
|
|
child: Column(
|
|
children: [
|
|
Expanded(child: _buildLogoZone()),
|
|
_buildFormPanel(provider),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Zona logo ────────────────────────────────────────────────
|
|
|
|
Widget _buildLogoZone() {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
width: 80,
|
|
height: 80,
|
|
decoration: BoxDecoration(
|
|
borderRadius: BorderRadius.circular(20),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withOpacity(0.38),
|
|
blurRadius: 18,
|
|
offset: const Offset(0, 7),
|
|
),
|
|
],
|
|
),
|
|
child: ClipRRect(
|
|
borderRadius: BorderRadius.circular(20),
|
|
child: Image.asset(
|
|
'assets/icon/icon.png',
|
|
fit: BoxFit.cover,
|
|
errorBuilder: (_, __, ___) => Container(
|
|
color: _kAccent,
|
|
child: const Icon(Icons.storefront,
|
|
size: 40, color: Colors.white),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
const Text(
|
|
'AGENTE VENTAS',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 22,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: 5,
|
|
),
|
|
),
|
|
const SizedBox(height: 5),
|
|
const Text(
|
|
'SIGMAC · Telcotronics',
|
|
style: TextStyle(
|
|
color: _kTextMuted,
|
|
fontSize: 12,
|
|
letterSpacing: 2,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Panel formulario ─────────────────────────────────────────
|
|
|
|
Widget _buildFormPanel(AppProvider provider) {
|
|
return Container(
|
|
decoration: const BoxDecoration(
|
|
color: _kPanel,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(32),
|
|
topRight: Radius.circular(32),
|
|
),
|
|
),
|
|
padding: const EdgeInsets.fromLTRB(24, 28, 24, 36),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Text(
|
|
'Bienvenido',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 3),
|
|
const Text(
|
|
'Ingresa tus credenciales para continuar',
|
|
style: TextStyle(color: _kTextMuted, fontSize: 13),
|
|
),
|
|
const SizedBox(height: 20),
|
|
_buildToggle(),
|
|
const SizedBox(height: 18),
|
|
_buildField(
|
|
controller: _emailCtrl,
|
|
label: _modoCloud ? 'Correo electrónico' : 'Usuario',
|
|
icon: _modoCloud ? Icons.email_outlined : Icons.person_outline,
|
|
keyboardType: _modoCloud
|
|
? TextInputType.emailAddress
|
|
: TextInputType.text,
|
|
validator: _modoCloud
|
|
? (v) {
|
|
if (v == null || v.trim().isEmpty) return 'Ingresa tu correo';
|
|
if (!v.contains('@') || !v.contains('.')) {
|
|
return 'Correo inválido';
|
|
}
|
|
return null;
|
|
}
|
|
: (v) {
|
|
if (v == null || v.trim().isEmpty) return 'Ingresa tu usuario';
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 13),
|
|
_buildField(
|
|
controller: _passCtrl,
|
|
label: 'Contraseña',
|
|
icon: Icons.lock_outline,
|
|
obscure: _obscure,
|
|
suffix: IconButton(
|
|
icon: Icon(
|
|
_obscure
|
|
? Icons.visibility_outlined
|
|
: Icons.visibility_off_outlined,
|
|
color: _kTextMuted,
|
|
size: 20,
|
|
),
|
|
onPressed: () => setState(() => _obscure = !_obscure),
|
|
),
|
|
validator: (v) {
|
|
if (v == null || v.isEmpty) return 'Ingresa tu contraseña';
|
|
if (v.length < 6) return 'Mínimo 6 caracteres';
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 12),
|
|
_buildRememberRow(),
|
|
if (provider.loginError != null) ...[
|
|
const SizedBox(height: 12),
|
|
_buildErrorBox(provider.loginError!),
|
|
],
|
|
const SizedBox(height: 16),
|
|
_buildButton(provider.loading),
|
|
const SizedBox(height: 8),
|
|
// Acceso rápido para evaluador de Google Play / App Store
|
|
_buildDemoAccessButton(),
|
|
const SizedBox(height: 10),
|
|
_buildRegistroLinks(),
|
|
const SizedBox(height: 12),
|
|
Center(
|
|
child: Text(
|
|
'v1.0.0 · SIGMAC',
|
|
style: TextStyle(
|
|
color: _kTextMuted.withOpacity(0.55), fontSize: 11),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Toggle Local / Cloud ─────────────────────────────────────
|
|
|
|
Widget _buildToggle() {
|
|
return LayoutBuilder(
|
|
builder: (_, constraints) {
|
|
final halfW = constraints.maxWidth / 2;
|
|
return Container(
|
|
height: 40,
|
|
decoration: BoxDecoration(
|
|
color: _kCard,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: _kBorder),
|
|
),
|
|
child: AnimatedBuilder(
|
|
animation: _toggleSlide,
|
|
builder: (_, __) => Stack(
|
|
children: [
|
|
// Indicador deslizante
|
|
Positioned(
|
|
left: _toggleSlide.value * halfW + 3,
|
|
top: 3,
|
|
bottom: 3,
|
|
width: halfW - 6,
|
|
child: Container(
|
|
decoration: BoxDecoration(
|
|
color: _kAccent,
|
|
borderRadius: BorderRadius.circular(7),
|
|
),
|
|
),
|
|
),
|
|
// Labels
|
|
Row(
|
|
children: [
|
|
_toggleOption('Cuenta local', !_modoCloud,
|
|
onTap: () => _switchModo(false)),
|
|
_toggleOption('Cloud', _modoCloud,
|
|
onTap: () => _switchModo(true)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _toggleOption(String label, bool active, {required VoidCallback onTap}) {
|
|
return Expanded(
|
|
child: GestureDetector(
|
|
onTap: onTap,
|
|
behavior: HitTestBehavior.opaque,
|
|
child: Center(
|
|
child: Text(
|
|
label,
|
|
style: TextStyle(
|
|
color: active ? Colors.white : _kTextMuted,
|
|
fontSize: 13,
|
|
fontWeight: active ? FontWeight.w600 : FontWeight.normal,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Campo de texto ───────────────────────────────────────────
|
|
|
|
Widget _buildField({
|
|
required TextEditingController controller,
|
|
required String label,
|
|
required IconData icon,
|
|
TextInputType? keyboardType,
|
|
bool obscure = false,
|
|
Widget? suffix,
|
|
String? Function(String?)? validator,
|
|
}) {
|
|
return TextFormField(
|
|
controller: controller,
|
|
obscureText: obscure,
|
|
keyboardType: keyboardType,
|
|
validator: validator,
|
|
style: const TextStyle(color: Colors.white, fontSize: 14),
|
|
decoration: InputDecoration(
|
|
labelText: label,
|
|
labelStyle: const TextStyle(color: _kTextMuted, fontSize: 13),
|
|
prefixIcon: Icon(icon, color: _kTextMuted, size: 20),
|
|
suffixIcon: suffix,
|
|
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),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Fila "Mantener sesión" ───────────────────────────────────
|
|
|
|
Widget _buildRememberRow() {
|
|
return Row(
|
|
children: [
|
|
SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: Checkbox(
|
|
value: _rememberMe,
|
|
onChanged: (v) => setState(() => _rememberMe = v ?? true),
|
|
activeColor: _kAccent,
|
|
side: const BorderSide(color: _kBorder, width: 1.5),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(4)),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
const Text('Mantener sesión',
|
|
style: TextStyle(color: _kTextMuted, fontSize: 13)),
|
|
const Spacer(),
|
|
if (_modoCloud)
|
|
GestureDetector(
|
|
onTap: () {},
|
|
child: const Text(
|
|
'¿Olvidaste tu contraseña?',
|
|
style: TextStyle(
|
|
color: _kAccent,
|
|
fontSize: 13,
|
|
fontWeight: FontWeight.w500,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
// ── Caja de error ────────────────────────────────────────────
|
|
|
|
Widget _buildErrorBox(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)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Links de registro ────────────────────────────────────────
|
|
|
|
Widget _buildRegistroLinks() {
|
|
return Column(
|
|
children: [
|
|
const Divider(color: _kBorder, height: 24),
|
|
const Text('¿No tienes cuenta?',
|
|
style: TextStyle(color: _kTextMuted, fontSize: 13)),
|
|
const SizedBox(height: 10),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: OutlinedButton.icon(
|
|
onPressed: () =>
|
|
Navigator.pushNamed(context, '/registro'),
|
|
icon: const Icon(Icons.smartphone, size: 16),
|
|
label: const Text('Cuenta local',
|
|
style: TextStyle(fontSize: 12)),
|
|
style: OutlinedButton.styleFrom(
|
|
foregroundColor: _kTextMuted,
|
|
side: const BorderSide(color: _kBorder),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
Expanded(
|
|
child: OutlinedButton.icon(
|
|
onPressed: () =>
|
|
Navigator.pushNamed(context, '/registro_cloud'),
|
|
icon: const Icon(Icons.cloud_outlined, size: 16),
|
|
label: const Text('Nueva Cloud',
|
|
style: TextStyle(fontSize: 12)),
|
|
style: OutlinedButton.styleFrom(
|
|
foregroundColor: _kAccent,
|
|
side: const BorderSide(color: _kAccent),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
// ── Botón ────────────────────────────────────────────────────
|
|
|
|
Widget _buildButton(bool loading) {
|
|
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 : _ingresar,
|
|
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),
|
|
)
|
|
: Text(
|
|
_modoCloud ? 'INGRESAR CON CLOUD' : 'INGRESAR',
|
|
style: const TextStyle(
|
|
fontSize: 15,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: 1.2,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// ── Botón de acceso de evaluación / Demo para tiendas ───────────────
|
|
// Facilita la revisión técnica de Google Play Store y Apple App Store
|
|
// autocompletando la cuenta demo preconfigurada (demo@empresa.com / 123456)
|
|
Widget _buildDemoAccessButton() {
|
|
return Center(
|
|
child: TextButton.icon(
|
|
onPressed: () {
|
|
setState(() {
|
|
_emailCtrl.text = 'demo@empresa.com';
|
|
_passCtrl.text = '123456';
|
|
});
|
|
_ingresar();
|
|
},
|
|
icon: const Icon(Icons.verified_user_outlined, size: 15, color: _kAccent),
|
|
label: const Text(
|
|
'Acceso Demo (Revisión de Tienda)',
|
|
style: TextStyle(
|
|
color: _kAccent,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w600,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|