diff --git a/lib/db/dao/usuarios_dao.dart b/lib/db/dao/usuarios_dao.dart index 1f511c1..49ad739 100644 --- a/lib/db/dao/usuarios_dao.dart +++ b/lib/db/dao/usuarios_dao.dart @@ -41,6 +41,16 @@ class UsuariosDao { return db.delete('usuarios', where: 'id = ?', whereArgs: [id]); } + Future updateFoto(int id, String? path) async { + final db = await _db.db; + return db.update( + 'usuarios', + {'foto_path': path, 'sincronizado': 0}, + where: 'id = ?', + whereArgs: [id], + ); + } + Future> getPendientes() async { final db = await _db.db; final rows = await db.query('usuarios', where: 'sincronizado = 0'); diff --git a/lib/db/database_helper.dart b/lib/db/database_helper.dart index 711fd98..a57ebe4 100644 --- a/lib/db/database_helper.dart +++ b/lib/db/database_helper.dart @@ -5,7 +5,7 @@ import 'package:path/path.dart'; class DatabaseHelper { static const _dbName = 'sentinel_one.db'; - static const _dbVersion = 2; + static const _dbVersion = 3; DatabaseHelper._(); static final DatabaseHelper instance = DatabaseHelper._(); @@ -67,7 +67,8 @@ class DatabaseHelper { fecha_inicio TEXT, fecha_fin TEXT, activo INTEGER NOT NULL DEFAULT 1, - sincronizado INTEGER NOT NULL DEFAULT 0 + sincronizado INTEGER NOT NULL DEFAULT 0, + foto_path TEXT ) '''); @@ -210,6 +211,10 @@ class DatabaseHelper { await _createTablesV2(db); await _seedDataV2(db); } + if (oldVersion < 3) { + await db.execute( + 'ALTER TABLE usuarios ADD COLUMN foto_path TEXT'); + } } Future _createTablesV2(Database db) async { diff --git a/lib/models/usuario.dart b/lib/models/usuario.dart index 395b978..b1f891b 100644 --- a/lib/models/usuario.dart +++ b/lib/models/usuario.dart @@ -9,6 +9,7 @@ class Usuario { final String? fechaFin; final bool activo; final bool sincronizado; + final String? fotoPath; const Usuario({ required this.id, @@ -21,6 +22,7 @@ class Usuario { this.fechaFin, this.activo = true, this.sincronizado = false, + this.fotoPath, }); factory Usuario.fromMap(Map m) => Usuario( @@ -34,6 +36,7 @@ class Usuario { fechaFin: m['fecha_fin'] as String?, activo: (m['activo'] as int) == 1, sincronizado: (m['sincronizado'] as int) == 1, + fotoPath: m['foto_path'] as String?, ); Map toMap() => { @@ -47,5 +50,26 @@ class Usuario { 'fecha_fin': fechaFin, 'activo': activo ? 1 : 0, 'sincronizado': sincronizado ? 1 : 0, + 'foto_path': fotoPath, }; + + Usuario copyWith({ + String? nombre, + String? iniciales, + String? fotoPath, + bool? sincronizado, + }) => + Usuario( + id: id, + nombre: nombre ?? this.nombre, + iniciales: iniciales ?? this.iniciales, + nivelAcceso: nivelAcceso, + pinHash: pinHash, + areas: areas, + fechaInicio: fechaInicio, + fechaFin: fechaFin, + activo: activo, + sincronizado: sincronizado ?? this.sincronizado, + fotoPath: fotoPath ?? this.fotoPath, + ); } diff --git a/lib/pages/perfil_usuario_page.dart b/lib/pages/perfil_usuario_page.dart index 545934d..449dbd6 100644 --- a/lib/pages/perfil_usuario_page.dart +++ b/lib/pages/perfil_usuario_page.dart @@ -1,5 +1,7 @@ +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:file_picker/file_picker.dart'; import '../theme.dart'; import '../models/usuario.dart'; import '../db/dao/usuarios_dao.dart'; @@ -97,23 +99,30 @@ class _PerfilUsuarioPageState extends State { ctrl.dispose(); if (nuevoNombre == null || nuevoNombre.isEmpty) return; - final actualizado = Usuario( - id: _usuario!.id, + final actualizado = _usuario!.copyWith( nombre: nuevoNombre, iniciales: _calcularIniciales(nuevoNombre), - nivelAcceso: _usuario!.nivelAcceso, - pinHash: _usuario!.pinHash, - areas: _usuario!.areas, - fechaInicio: _usuario!.fechaInicio, - fechaFin: _usuario!.fechaFin, - activo: _usuario!.activo, - sincronizado: false, // marcar para sincronizar + sincronizado: false, ); await _dao.update(actualizado); if (mounted) setState(() => _usuario = actualizado); } + // ── Seleccionar foto de perfil ──────────────────────────────────────────── + Future _seleccionarFoto() async { + final result = await FilePicker.platform.pickFiles( + type: FileType.image, + allowMultiple: false, + ); + if (result == null || result.files.isEmpty) return; + final path = result.files.first.path; + if (path == null || _usuario == null) return; + + await _dao.updateFoto(_usuario!.id, path); + if (mounted) setState(() => _usuario = _usuario!.copyWith(fotoPath: path)); + } + @override Widget build(BuildContext context) { if (_cargando) { @@ -155,47 +164,68 @@ class _PerfilUsuarioPageState extends State { borderRadius: BorderRadius.circular(12)), child: Column(children: [ Stack(clipBehavior: Clip.none, children: [ - Container( - width: 80, height: 80, - decoration: BoxDecoration( - shape: BoxShape.circle, - gradient: const LinearGradient( - colors: [AppColors.primary, AppColors.primaryContainer], - begin: Alignment.topLeft, - end: Alignment.bottomRight, + // ── Círculo avatar ─────────────────────────────────────────── + GestureDetector( + onTap: _seleccionarFoto, + child: Container( + width: 80, height: 80, + decoration: BoxDecoration( + shape: BoxShape.circle, + gradient: _usuario?.fotoPath == null + ? const LinearGradient( + colors: [AppColors.primary, AppColors.primaryContainer], + begin: Alignment.topLeft, + end: Alignment.bottomRight, + ) + : null, + boxShadow: [BoxShadow( + color: AppColors.primary.withOpacity(0.3), + blurRadius: 20, spreadRadius: 2, + )], + ), + child: ClipOval( + child: _usuario?.fotoPath != null + ? Image.file( + File(_usuario!.fotoPath!), + width: 80, height: 80, + fit: BoxFit.cover, + errorBuilder: (_, __, ___) => _avatarIniciales(iniciales), + ) + : _avatarIniciales(iniciales), ), - boxShadow: [BoxShadow( - color: AppColors.primary.withOpacity(0.3), - blurRadius: 20, spreadRadius: 2, - )], - ), - child: Center( - child: Text(iniciales, - style: appHeadline(size: 28, weight: FontWeight.w800, - color: AppColors.primaryContainer)), ), ), + // ── Botón cámara ───────────────────────────────────────────── Positioned( bottom: 0, right: 0, child: GestureDetector( - onTap: _editarNombre, + onTap: _seleccionarFoto, child: Container( - width: 24, height: 24, + width: 26, height: 26, decoration: BoxDecoration( - color: AppColors.surfaceHighest, + color: AppColors.primary, shape: BoxShape.circle, border: Border.all( color: AppColors.surfaceHigh, width: 2), ), - child: const Icon(Icons.edit, - size: 13, color: AppColors.primary), + child: const Icon(Icons.camera_alt, + size: 13, color: AppColors.primaryContainer), ), ), ), ]), const SizedBox(height: 16), - Text(nombre, - style: appHeadline(size: 20, weight: FontWeight.w800)), + // ── Nombre con botón editar ────────────────────────────────── + Row(mainAxisAlignment: MainAxisAlignment.center, children: [ + Text(nombre, + style: appHeadline(size: 20, weight: FontWeight.w800)), + const SizedBox(width: 8), + GestureDetector( + onTap: _editarNombre, + child: const Icon(Icons.edit, + size: 15, color: AppColors.primary), + ), + ]), const SizedBox(height: 4), Text('Operador de Seguridad', style: appLabel(size: 13)), const SizedBox(height: 12), @@ -211,6 +241,14 @@ class _PerfilUsuarioPageState extends State { ]), ); + Widget _avatarIniciales(String iniciales) => Center( + child: Text(iniciales, + style: appHeadline( + size: 28, + weight: FontWeight.w800, + color: AppColors.primaryContainer)), + ); + // ── Estadísticas ─────────────────────────────────────────────────────────── Widget _statsGrid() => Column( crossAxisAlignment: CrossAxisAlignment.start, diff --git a/pubspec.yaml b/pubspec.yaml index e28c379..2c4a540 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,7 @@ dependencies: crypto: ^3.0.2 shared_preferences: ^2.0.0 http: ^0.13.4 + file_picker: ^4.6.1 dev_dependencies: flutter_test: