feat: foto de perfil seleccionable desde archivo
- file_picker ^4.6.1 — abre galería/archivos (en Android incluye cámara) - BD v3: columna foto_path en usuarios, migración onUpgrade - Usuario modelo: campo fotoPath + copyWith - UsuariosDao: método updateFoto(id, path) - PerfilUsuarioPage: · Avatar muestra foto si hay, iniciales si no · Toque en avatar o ícono cámara → FilePicker → guarda ruta en BD · Ícono editar nombre movido junto al texto del nombre Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
f42467037d
commit
33f0b8aa29
@@ -41,6 +41,16 @@ class UsuariosDao {
|
||||
return db.delete('usuarios', where: 'id = ?', whereArgs: [id]);
|
||||
}
|
||||
|
||||
Future<int> 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<List<Usuario>> getPendientes() async {
|
||||
final db = await _db.db;
|
||||
final rows = await db.query('usuarios', where: 'sincronizado = 0');
|
||||
|
||||
@@ -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<void> _createTablesV2(Database db) async {
|
||||
|
||||
@@ -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<String, dynamic> 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<String, dynamic> 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,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<PerfilUsuarioPage> {
|
||||
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<void> _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<PerfilUsuarioPage> {
|
||||
borderRadius: BorderRadius.circular(12)),
|
||||
child: Column(children: [
|
||||
Stack(clipBehavior: Clip.none, children: [
|
||||
Container(
|
||||
// ── Círculo avatar ───────────────────────────────────────────
|
||||
GestureDetector(
|
||||
onTap: _seleccionarFoto,
|
||||
child: Container(
|
||||
width: 80, height: 80,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
gradient: const LinearGradient(
|
||||
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: Center(
|
||||
child: Text(iniciales,
|
||||
style: appHeadline(size: 28, weight: FontWeight.w800,
|
||||
color: AppColors.primaryContainer)),
|
||||
child: ClipOval(
|
||||
child: _usuario?.fotoPath != null
|
||||
? Image.file(
|
||||
File(_usuario!.fotoPath!),
|
||||
width: 80, height: 80,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _avatarIniciales(iniciales),
|
||||
)
|
||||
: _avatarIniciales(iniciales),
|
||||
),
|
||||
),
|
||||
),
|
||||
// ── 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),
|
||||
// ── 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<PerfilUsuarioPage> {
|
||||
]),
|
||||
);
|
||||
|
||||
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,
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user