328 lines
13 KiB
Dart
328 lines
13 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
// Comentario explicativo: sqflite_common_ffi provee databaseFactoryFfi e inicialización sqflite sin redundancia
|
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
|
import 'theme.dart';
|
|
import 'services/api_auth_service.dart';
|
|
// Comentario explicativo: Servicio centralizado para capturar y reportar errores de runtime a API-SIGMA-WEBCONTROL
|
|
import 'services/error_reporter_service.dart';
|
|
import 'shell_page.dart';
|
|
|
|
// Comentario explicativo: Inicialización de la aplicación.
|
|
// sqfliteFfi se inicializa exclusivamente en Linux/Windows desktop. En plataformas móviles
|
|
// (Android e iOS), sqflite utiliza automáticamente el backend nativo del sistema operativo.
|
|
Future<void> main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
// Comentario explicativo: Inicializa la captura automática y despacho de fallos a la bitácora centralizada de errores
|
|
ErrorReporterService.instance.inicializar();
|
|
if (!kIsWeb && (Platform.isLinux || Platform.isWindows)) {
|
|
sqfliteFfiInit();
|
|
databaseFactory = databaseFactoryFfi;
|
|
}
|
|
|
|
final autenticado = await ApiAuthService.instance.isAuthenticated();
|
|
runApp(CentroMonitoreoApp(inicioAutenticado: autenticado));
|
|
}
|
|
|
|
class CentroMonitoreoApp extends StatelessWidget {
|
|
final bool inicioAutenticado;
|
|
const CentroMonitoreoApp({Key? key, required this.inicioAutenticado})
|
|
: super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Centro de Monitoreo',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: ThemeData(
|
|
brightness: Brightness.dark,
|
|
scaffoldBackgroundColor: AppColors.surfaceLowest,
|
|
canvasColor: AppColors.surfaceLow,
|
|
cardColor: AppColors.surfaceHigh,
|
|
dialogBackgroundColor: AppColors.surfaceHigh,
|
|
dividerColor: AppColors.outlineVariant,
|
|
colorScheme: const ColorScheme.dark(
|
|
primary: AppColors.primary,
|
|
onPrimary: AppColors.primaryContainer,
|
|
secondary: AppColors.secondary,
|
|
surface: AppColors.surface,
|
|
onSurface: AppColors.onSurface,
|
|
error: AppColors.error,
|
|
onError: AppColors.onError,
|
|
),
|
|
),
|
|
initialRoute: inicioAutenticado ? '/shell' : '/login',
|
|
routes: {
|
|
'/login': (_) => const OperadorLoginPage(),
|
|
'/shell': (_) => const ShellPage(),
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
// ── Login Page ───────────────────────────────────────────────────────────────
|
|
|
|
class OperadorLoginPage extends StatefulWidget {
|
|
const OperadorLoginPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<OperadorLoginPage> createState() => _OperadorLoginPageState();
|
|
}
|
|
|
|
class _OperadorLoginPageState extends State<OperadorLoginPage> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _ctrlUser = TextEditingController();
|
|
final _ctrlPass = TextEditingController();
|
|
bool _cargando = false;
|
|
bool _verPass = false;
|
|
String? _error;
|
|
|
|
@override
|
|
void dispose() {
|
|
_ctrlUser.dispose();
|
|
_ctrlPass.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _login() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
setState(() { _cargando = true; _error = null; });
|
|
|
|
final ok = await ApiAuthService.instance.login(
|
|
_ctrlUser.text.trim(),
|
|
_ctrlPass.text,
|
|
);
|
|
|
|
if (!mounted) return;
|
|
if (ok) {
|
|
Navigator.of(context).pushReplacementNamed('/shell');
|
|
} else {
|
|
setState(() {
|
|
_cargando = false;
|
|
_error = 'Credenciales incorrectas o sin conexión con WebControl.';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
backgroundColor: AppColors.surfaceLowest,
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
primary: false,
|
|
padding: const EdgeInsets.all(32),
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
child: Column(children: [
|
|
// Logo
|
|
Image.asset(
|
|
'lib/recursos/logo_app.png',
|
|
width: 96,
|
|
height: 96,
|
|
),
|
|
const SizedBox(height: 20),
|
|
Text('CENTRO DE MONITOREO',
|
|
style: appHeadline(
|
|
size: 18,
|
|
weight: FontWeight.w800,
|
|
color: AppColors.primary,
|
|
spacing: 2.0)),
|
|
const SizedBox(height: 6),
|
|
Text('Acceso para operadores autorizados',
|
|
style: appLabel(size: 12, color: AppColors.outline)),
|
|
const SizedBox(height: 40),
|
|
|
|
// Card de login
|
|
Container(
|
|
padding: const EdgeInsets.all(32),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(16),
|
|
border: Border.all(color: AppColors.outlineVariant, width: 1),
|
|
),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
|
Text('Iniciar sesión',
|
|
style: appHeadline(size: 16, weight: FontWeight.w700)),
|
|
const SizedBox(height: 24),
|
|
|
|
// Usuario
|
|
_campo(
|
|
controller: _ctrlUser,
|
|
label: 'Usuario',
|
|
icono: Icons.person_outline,
|
|
validator: (v) =>
|
|
(v == null || v.trim().isEmpty) ? 'Ingresa tu usuario' : null,
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Contraseña
|
|
_campo(
|
|
controller: _ctrlPass,
|
|
label: 'Contraseña',
|
|
icono: Icons.lock_outline,
|
|
obscure: !_verPass,
|
|
sufijo: IconButton(
|
|
icon: Icon(
|
|
_verPass ? Icons.visibility_off : Icons.visibility,
|
|
color: AppColors.outline,
|
|
size: 18,
|
|
),
|
|
onPressed: () => setState(() => _verPass = !_verPass),
|
|
),
|
|
validator: (v) =>
|
|
(v == null || v.isEmpty) ? 'Ingresa tu contraseña' : null,
|
|
onSubmit: (_) => _login(),
|
|
),
|
|
const SizedBox(height: 12),
|
|
|
|
// Error
|
|
if (_error != null)
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 12),
|
|
child: Text(_error!,
|
|
style: appLabel(size: 12, color: AppColors.error)),
|
|
),
|
|
|
|
// Botón principal
|
|
SizedBox(
|
|
height: 48,
|
|
// Comentario explicativo: Uso de backgroundColor y foregroundColor compatibles con Flutter 3.x
|
|
child: ElevatedButton(
|
|
onPressed: _cargando ? null : _login,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.primary,
|
|
foregroundColor: AppColors.primaryContainer,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
),
|
|
child: _cargando
|
|
? const SizedBox(
|
|
width: 20,
|
|
height: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
color: AppColors.primaryContainer),
|
|
)
|
|
: Text('INGRESAR',
|
|
style: appHeadline(
|
|
size: 13,
|
|
weight: FontWeight.w800,
|
|
color: AppColors.primaryContainer,
|
|
spacing: 1.2)),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
// Comentario explicativo: Acceso Demo garantizado para revisores de Google Play Store y Apple App Store.
|
|
// Permite autocompletar e ingresar con un solo toque utilizando las credenciales oficiales fijas
|
|
// (demo@empresa.com / 123456) sin requerir SMS, WhatsApp ni conexión de red externa.
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceHighest,
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: AppColors.outlineVariant.withOpacity(0.5)),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
const Icon(Icons.verified_user_outlined, size: 16, color: AppColors.primary),
|
|
const SizedBox(width: 6),
|
|
Text('Acceso para Revisión (Store Demo)',
|
|
style: appLabel(size: 11, weight: FontWeight.w600, color: AppColors.primary)),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text('demo@empresa.com • Clave: 123456',
|
|
style: appLabel(size: 11, color: AppColors.outline)),
|
|
const SizedBox(height: 8),
|
|
OutlinedButton.icon(
|
|
icon: const Icon(Icons.login, size: 16, color: AppColors.primary),
|
|
label: Text('Autocompletar e Ingresar',
|
|
style: appLabel(size: 11, weight: FontWeight.bold, color: AppColors.primary)),
|
|
style: OutlinedButton.styleFrom(
|
|
side: const BorderSide(color: AppColors.primary),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
onPressed: _cargando
|
|
? null
|
|
: () {
|
|
setState(() {
|
|
_ctrlUser.text = 'demo@empresa.com';
|
|
_ctrlPass.text = '123456';
|
|
});
|
|
_login();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
]),
|
|
),
|
|
),
|
|
|
|
const SizedBox(height: 32),
|
|
Text('Telcotronics · WebControl',
|
|
style: appLabel(size: 11, color: AppColors.outlineVariant)),
|
|
]),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _campo({
|
|
required TextEditingController controller,
|
|
required String label,
|
|
required IconData icono,
|
|
bool obscure = false,
|
|
Widget? sufijo,
|
|
String? Function(String?)? validator,
|
|
void Function(String)? onSubmit,
|
|
}) =>
|
|
TextFormField(
|
|
controller: controller,
|
|
obscureText: obscure,
|
|
style: appLabel(size: 13, color: AppColors.onSurface),
|
|
onFieldSubmitted: onSubmit,
|
|
decoration: InputDecoration(
|
|
labelText: label,
|
|
labelStyle: appLabel(size: 12, color: AppColors.outline),
|
|
prefixIcon: Icon(icono, color: AppColors.outline, size: 18),
|
|
suffixIcon: sufijo,
|
|
filled: true,
|
|
fillColor: AppColors.surfaceHigh,
|
|
contentPadding:
|
|
const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
borderSide: const BorderSide(color: AppColors.outlineVariant),
|
|
),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
borderSide: const BorderSide(color: AppColors.outlineVariant),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
borderSide: const BorderSide(color: AppColors.primary, width: 1.5),
|
|
),
|
|
errorBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
borderSide: const BorderSide(color: AppColors.error),
|
|
),
|
|
focusedErrorBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10),
|
|
borderSide: const BorderSide(color: AppColors.error, width: 1.5),
|
|
),
|
|
),
|
|
validator: validator,
|
|
);
|
|
}
|