- Add offline-first SQLite architecture with DatabaseHelper singleton, 12 CREATE TABLE statements and full seed data on first launch - Add 10 models (fromMap/toMap) and 12 DAOs with CRUD + getPendientes() - Add ApiAuthService (JWT via SharedPreferences) and Sincronizador skeleton - Move login auth from SQLite to SharedPreferences with SHA-256 PIN hash; default PIN 123456, platform-independent (no native plugin required) - Add seguridad_page.dart: change PIN form (current → new → confirm) - Add Cerrar Sesión with confirmation dialog and full stack clear - Redesign settings_page.dart with AppColors dark theme; add Sistema section with "Inicializar base de datos" and "Inicializar sincronización" - Fix Linux desktop: sqflite_common_ffi with platform detection in DatabaseHelper - Fix ScrollController conflict on Linux: primary: false on all IndexedStack pages - Update plan_proyecto.md with session 2–3 progress and auth roadmap Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
322 lines
11 KiB
Dart
322 lines
11 KiB
Dart
import 'dart:convert';
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'theme.dart';
|
|
import 'main_page.dart';
|
|
import 'pages/register_page.dart';
|
|
import 'widgets/app_menu_button.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'SENTINEL ONE',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: ThemeData(
|
|
brightness: Brightness.dark,
|
|
scaffoldBackgroundColor: AppColors.surfaceLowest,
|
|
useMaterial3: true,
|
|
),
|
|
home: const LoginPage(),
|
|
);
|
|
}
|
|
}
|
|
|
|
// ─── Página de Login ───────────────────────────────────────────────────────────
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<LoginPage> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
static const int _codeLength = 6;
|
|
|
|
final List<TextEditingController> _controllers =
|
|
List.generate(_codeLength, (_) => TextEditingController());
|
|
final List<FocusNode> _focusNodes =
|
|
List.generate(_codeLength, (_) => FocusNode());
|
|
|
|
bool _cargando = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
for (final c in _controllers) c.dispose();
|
|
for (final f in _focusNodes) f.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _onDigitChanged(int index, String value) {
|
|
if (value.isNotEmpty && index < _codeLength - 1) {
|
|
_focusNodes[index + 1].requestFocus();
|
|
}
|
|
setState(() {});
|
|
}
|
|
|
|
void _onKeyEvent(int index, RawKeyEvent event) {
|
|
if (event is RawKeyDownEvent &&
|
|
event.logicalKey == LogicalKeyboardKey.backspace &&
|
|
_controllers[index].text.isEmpty &&
|
|
index > 0) {
|
|
_focusNodes[index - 1].requestFocus();
|
|
_controllers[index - 1].clear();
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
// PIN inicial por defecto: 123456 — el usuario puede cambiarlo desde ajustes
|
|
static const _keyPinHash = 'local_pin_hash';
|
|
static const _defaultPin = '123456';
|
|
|
|
static String _hashPin(String pin) =>
|
|
sha256.convert(utf8.encode(pin)).toString();
|
|
|
|
Future<void> _handleLogin() async {
|
|
final pin = _controllers.map((c) => c.text).join();
|
|
if (pin.length < 4) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Introduce al menos 4 dígitos')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() => _cargando = true);
|
|
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
// Si aún no hay PIN guardado, usamos el por defecto
|
|
final storedHash = prefs.getString(_keyPinHash) ??
|
|
_hashPin(_defaultPin);
|
|
|
|
if (_hashPin(pin) != storedHash) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('PIN incorrecto')),
|
|
);
|
|
_clearCode();
|
|
return;
|
|
}
|
|
|
|
if (!mounted) return;
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const MainPage(initialIndex: 0)),
|
|
);
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('Error: $e')),
|
|
);
|
|
} finally {
|
|
if (mounted) setState(() => _cargando = false);
|
|
}
|
|
}
|
|
|
|
void _clearCode() {
|
|
for (final c in _controllers) c.clear();
|
|
_focusNodes[0].requestFocus();
|
|
setState(() {});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final filled = _controllers.where((c) => c.text.isNotEmpty).length;
|
|
|
|
return Scaffold(
|
|
backgroundColor: AppColors.surfaceLowest,
|
|
appBar: AppBar(
|
|
backgroundColor: AppColors.surfaceLow,
|
|
elevation: 0,
|
|
automaticallyImplyLeading: false,
|
|
title: Row(children: [
|
|
const Icon(Icons.security, color: AppColors.primary, size: 20),
|
|
const SizedBox(width: 10),
|
|
Text('SENTINEL ONE',
|
|
style: appHeadline(size: 16, weight: FontWeight.w900,
|
|
color: AppColors.primary, spacing: 2.0)),
|
|
]),
|
|
actions: const [AppMenuButton()],
|
|
),
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(
|
|
minHeight: MediaQuery.of(context).size.height -
|
|
MediaQuery.of(context).padding.top,
|
|
),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const SizedBox(height: 48),
|
|
// Logo
|
|
Container(
|
|
width: 72, height: 72,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.circle,
|
|
color: AppColors.primaryContainer,
|
|
boxShadow: [BoxShadow(
|
|
color: AppColors.primary.withOpacity(0.3),
|
|
blurRadius: 24, spreadRadius: 4)],
|
|
),
|
|
child: const Icon(Icons.security,
|
|
size: 36, color: AppColors.primary),
|
|
),
|
|
const SizedBox(height: 20),
|
|
Text('SENTINEL ONE',
|
|
style: GoogleFonts.manrope(
|
|
fontSize: 22,
|
|
fontWeight: FontWeight.w900,
|
|
color: AppColors.primary,
|
|
letterSpacing: 2.5)),
|
|
const SizedBox(height: 8),
|
|
Text('Sistema de Seguridad Electrónica',
|
|
style: GoogleFonts.inter(
|
|
fontSize: 13,
|
|
color: AppColors.onSurfaceVariant)),
|
|
const SizedBox(height: 48),
|
|
// Código de acceso
|
|
Container(
|
|
padding: const EdgeInsets.all(24),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceHigh,
|
|
borderRadius: BorderRadius.circular(16)),
|
|
child: Column(children: [
|
|
Text('CÓDIGO DE ACCESO',
|
|
style: GoogleFonts.inter(
|
|
fontSize: 11,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.onSurfaceVariant,
|
|
letterSpacing: 2.0)),
|
|
const SizedBox(height: 20),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: List.generate(
|
|
_codeLength, (i) => _digitBox(i)),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text('$filled / 4-6 dígitos',
|
|
style: GoogleFonts.inter(
|
|
fontSize: 11,
|
|
color: AppColors.outline)),
|
|
]),
|
|
),
|
|
const SizedBox(height: 24),
|
|
SizedBox(
|
|
width: double.infinity,
|
|
child: ElevatedButton(
|
|
onPressed: (filled >= 4 && !_cargando) ? _handleLogin : null,
|
|
style: ElevatedButton.styleFrom(
|
|
primary: AppColors.primary,
|
|
onPrimary: AppColors.primaryContainer,
|
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12)),
|
|
elevation: 0,
|
|
),
|
|
child: _cargando
|
|
? const SizedBox(
|
|
width: 20, height: 20,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
color: AppColors.primaryContainer))
|
|
: Text('INGRESAR',
|
|
style: GoogleFonts.inter(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.bold,
|
|
letterSpacing: 1.5,
|
|
color: filled >= 4
|
|
? AppColors.primaryContainer
|
|
: AppColors.onSurfaceVariant)),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextButton.icon(
|
|
onPressed: _clearCode,
|
|
icon: const Icon(Icons.backspace_outlined,
|
|
size: 16, color: AppColors.onSurfaceVariant),
|
|
label: Text('Limpiar',
|
|
style: GoogleFonts.inter(
|
|
color: AppColors.onSurfaceVariant, fontSize: 13)),
|
|
),
|
|
const SizedBox(height: 24),
|
|
TextButton(
|
|
onPressed: () => Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => const RegisterPage()),
|
|
),
|
|
child: Text('¿No tienes cuenta? Regístrate',
|
|
style: GoogleFonts.inter(
|
|
color: AppColors.primary, fontSize: 13)),
|
|
),
|
|
const SizedBox(height: 48),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _digitBox(int index) {
|
|
final isFilled = _controllers[index].text.isNotEmpty;
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 4),
|
|
child: RawKeyboardListener(
|
|
focusNode: FocusNode(),
|
|
onKey: (e) => _onKeyEvent(index, e),
|
|
child: SizedBox(
|
|
width: 44,
|
|
child: TextFormField(
|
|
controller: _controllers[index],
|
|
focusNode: _focusNodes[index],
|
|
textAlign: TextAlign.center,
|
|
keyboardType: TextInputType.number,
|
|
obscureText: true,
|
|
maxLength: 1,
|
|
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
|
|
style: GoogleFonts.inter(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.bold,
|
|
color: AppColors.primary),
|
|
decoration: InputDecoration(
|
|
counterText: '',
|
|
contentPadding: const EdgeInsets.symmetric(vertical: 14),
|
|
enabledBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: BorderSide(
|
|
color: isFilled
|
|
? AppColors.primary
|
|
: AppColors.outlineVariant,
|
|
width: isFilled ? 2 : 1,
|
|
),
|
|
),
|
|
focusedBorder: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(
|
|
color: AppColors.primary, width: 2),
|
|
),
|
|
filled: true,
|
|
fillColor: isFilled
|
|
? AppColors.primaryContainer.withOpacity(0.3)
|
|
: AppColors.surfaceHighest,
|
|
),
|
|
onChanged: (v) => _onDigitChanged(index, v),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|