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]);
|
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 {
|
Future<List<Usuario>> getPendientes() async {
|
||||||
final db = await _db.db;
|
final db = await _db.db;
|
||||||
final rows = await db.query('usuarios', where: 'sincronizado = 0');
|
final rows = await db.query('usuarios', where: 'sincronizado = 0');
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import 'package:path/path.dart';
|
|||||||
|
|
||||||
class DatabaseHelper {
|
class DatabaseHelper {
|
||||||
static const _dbName = 'sentinel_one.db';
|
static const _dbName = 'sentinel_one.db';
|
||||||
static const _dbVersion = 2;
|
static const _dbVersion = 3;
|
||||||
|
|
||||||
DatabaseHelper._();
|
DatabaseHelper._();
|
||||||
static final DatabaseHelper instance = DatabaseHelper._();
|
static final DatabaseHelper instance = DatabaseHelper._();
|
||||||
@@ -67,7 +67,8 @@ class DatabaseHelper {
|
|||||||
fecha_inicio TEXT,
|
fecha_inicio TEXT,
|
||||||
fecha_fin TEXT,
|
fecha_fin TEXT,
|
||||||
activo INTEGER NOT NULL DEFAULT 1,
|
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 _createTablesV2(db);
|
||||||
await _seedDataV2(db);
|
await _seedDataV2(db);
|
||||||
}
|
}
|
||||||
|
if (oldVersion < 3) {
|
||||||
|
await db.execute(
|
||||||
|
'ALTER TABLE usuarios ADD COLUMN foto_path TEXT');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _createTablesV2(Database db) async {
|
Future<void> _createTablesV2(Database db) async {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class Usuario {
|
|||||||
final String? fechaFin;
|
final String? fechaFin;
|
||||||
final bool activo;
|
final bool activo;
|
||||||
final bool sincronizado;
|
final bool sincronizado;
|
||||||
|
final String? fotoPath;
|
||||||
|
|
||||||
const Usuario({
|
const Usuario({
|
||||||
required this.id,
|
required this.id,
|
||||||
@@ -21,6 +22,7 @@ class Usuario {
|
|||||||
this.fechaFin,
|
this.fechaFin,
|
||||||
this.activo = true,
|
this.activo = true,
|
||||||
this.sincronizado = false,
|
this.sincronizado = false,
|
||||||
|
this.fotoPath,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory Usuario.fromMap(Map<String, dynamic> m) => Usuario(
|
factory Usuario.fromMap(Map<String, dynamic> m) => Usuario(
|
||||||
@@ -34,6 +36,7 @@ class Usuario {
|
|||||||
fechaFin: m['fecha_fin'] as String?,
|
fechaFin: m['fecha_fin'] as String?,
|
||||||
activo: (m['activo'] as int) == 1,
|
activo: (m['activo'] as int) == 1,
|
||||||
sincronizado: (m['sincronizado'] as int) == 1,
|
sincronizado: (m['sincronizado'] as int) == 1,
|
||||||
|
fotoPath: m['foto_path'] as String?,
|
||||||
);
|
);
|
||||||
|
|
||||||
Map<String, dynamic> toMap() => {
|
Map<String, dynamic> toMap() => {
|
||||||
@@ -47,5 +50,26 @@ class Usuario {
|
|||||||
'fecha_fin': fechaFin,
|
'fecha_fin': fechaFin,
|
||||||
'activo': activo ? 1 : 0,
|
'activo': activo ? 1 : 0,
|
||||||
'sincronizado': sincronizado ? 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:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
import '../theme.dart';
|
import '../theme.dart';
|
||||||
import '../models/usuario.dart';
|
import '../models/usuario.dart';
|
||||||
import '../db/dao/usuarios_dao.dart';
|
import '../db/dao/usuarios_dao.dart';
|
||||||
@@ -97,23 +99,30 @@ class _PerfilUsuarioPageState extends State<PerfilUsuarioPage> {
|
|||||||
ctrl.dispose();
|
ctrl.dispose();
|
||||||
if (nuevoNombre == null || nuevoNombre.isEmpty) return;
|
if (nuevoNombre == null || nuevoNombre.isEmpty) return;
|
||||||
|
|
||||||
final actualizado = Usuario(
|
final actualizado = _usuario!.copyWith(
|
||||||
id: _usuario!.id,
|
|
||||||
nombre: nuevoNombre,
|
nombre: nuevoNombre,
|
||||||
iniciales: _calcularIniciales(nuevoNombre),
|
iniciales: _calcularIniciales(nuevoNombre),
|
||||||
nivelAcceso: _usuario!.nivelAcceso,
|
sincronizado: false,
|
||||||
pinHash: _usuario!.pinHash,
|
|
||||||
areas: _usuario!.areas,
|
|
||||||
fechaInicio: _usuario!.fechaInicio,
|
|
||||||
fechaFin: _usuario!.fechaFin,
|
|
||||||
activo: _usuario!.activo,
|
|
||||||
sincronizado: false, // marcar para sincronizar
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await _dao.update(actualizado);
|
await _dao.update(actualizado);
|
||||||
if (mounted) setState(() => _usuario = 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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (_cargando) {
|
if (_cargando) {
|
||||||
@@ -155,47 +164,68 @@ class _PerfilUsuarioPageState extends State<PerfilUsuarioPage> {
|
|||||||
borderRadius: BorderRadius.circular(12)),
|
borderRadius: BorderRadius.circular(12)),
|
||||||
child: Column(children: [
|
child: Column(children: [
|
||||||
Stack(clipBehavior: Clip.none, children: [
|
Stack(clipBehavior: Clip.none, children: [
|
||||||
Container(
|
// ── Círculo avatar ───────────────────────────────────────────
|
||||||
width: 80, height: 80,
|
GestureDetector(
|
||||||
decoration: BoxDecoration(
|
onTap: _seleccionarFoto,
|
||||||
shape: BoxShape.circle,
|
child: Container(
|
||||||
gradient: const LinearGradient(
|
width: 80, height: 80,
|
||||||
colors: [AppColors.primary, AppColors.primaryContainer],
|
decoration: BoxDecoration(
|
||||||
begin: Alignment.topLeft,
|
shape: BoxShape.circle,
|
||||||
end: Alignment.bottomRight,
|
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(
|
Positioned(
|
||||||
bottom: 0, right: 0,
|
bottom: 0, right: 0,
|
||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
onTap: _editarNombre,
|
onTap: _seleccionarFoto,
|
||||||
child: Container(
|
child: Container(
|
||||||
width: 24, height: 24,
|
width: 26, height: 26,
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: AppColors.surfaceHighest,
|
color: AppColors.primary,
|
||||||
shape: BoxShape.circle,
|
shape: BoxShape.circle,
|
||||||
border: Border.all(
|
border: Border.all(
|
||||||
color: AppColors.surfaceHigh, width: 2),
|
color: AppColors.surfaceHigh, width: 2),
|
||||||
),
|
),
|
||||||
child: const Icon(Icons.edit,
|
child: const Icon(Icons.camera_alt,
|
||||||
size: 13, color: AppColors.primary),
|
size: 13, color: AppColors.primaryContainer),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
Text(nombre,
|
// ── Nombre con botón editar ──────────────────────────────────
|
||||||
style: appHeadline(size: 20, weight: FontWeight.w800)),
|
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),
|
const SizedBox(height: 4),
|
||||||
Text('Operador de Seguridad', style: appLabel(size: 13)),
|
Text('Operador de Seguridad', style: appLabel(size: 13)),
|
||||||
const SizedBox(height: 12),
|
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 ───────────────────────────────────────────────────────────
|
// ── Estadísticas ───────────────────────────────────────────────────────────
|
||||||
Widget _statsGrid() => Column(
|
Widget _statsGrid() => Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ dependencies:
|
|||||||
crypto: ^3.0.2
|
crypto: ^3.0.2
|
||||||
shared_preferences: ^2.0.0
|
shared_preferences: ^2.0.0
|
||||||
http: ^0.13.4
|
http: ^0.13.4
|
||||||
|
file_picker: ^4.6.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
Reference in New Issue
Block a user