feat: add SQLite data layer, local auth and security settings
- 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>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
8f13d57839
commit
1184ab5fe2
+63
-17
@@ -1,12 +1,16 @@
|
||||
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() {
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
@@ -44,6 +48,8 @@ class _LoginPageState extends State<LoginPage> {
|
||||
final List<FocusNode> _focusNodes =
|
||||
List.generate(_codeLength, (_) => FocusNode());
|
||||
|
||||
bool _cargando = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final c in _controllers) c.dispose();
|
||||
@@ -69,18 +75,52 @@ class _LoginPageState extends State<LoginPage> {
|
||||
}
|
||||
}
|
||||
|
||||
void _handleLogin() {
|
||||
final filled = _controllers.where((c) => c.text.isNotEmpty).length;
|
||||
if (filled < 4) {
|
||||
// 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;
|
||||
}
|
||||
Navigator.pushReplacement(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => const MainPage(initialIndex: 0)),
|
||||
);
|
||||
|
||||
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() {
|
||||
@@ -176,7 +216,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: filled >= 4 ? _handleLogin : null,
|
||||
onPressed: (filled >= 4 && !_cargando) ? _handleLogin : null,
|
||||
style: ElevatedButton.styleFrom(
|
||||
primary: AppColors.primary,
|
||||
onPrimary: AppColors.primaryContainer,
|
||||
@@ -185,14 +225,20 @@ class _LoginPageState extends State<LoginPage> {
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
elevation: 0,
|
||||
),
|
||||
child: Text('INGRESAR',
|
||||
style: GoogleFonts.inter(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
letterSpacing: 1.5,
|
||||
color: filled >= 4
|
||||
? AppColors.primaryContainer
|
||||
: AppColors.onSurfaceVariant)),
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user