Implement local vault service with encryption key management and integrate it into the app. Add settings screen for data management and enhance home screen with new features. Update database connection for encryption support and modify repository to use the new database structure. Improve UI elements across the application for better user experience.
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:notas/models/note.dart';
|
||||
import 'package:notas/screens/note_editor_screen.dart';
|
||||
import 'package:notas/widgets/app_title_bar.dart';
|
||||
import 'package:notas/widgets/menu_drawer.dart';
|
||||
import 'package:notas/screens/settings_screen.dart';
|
||||
import 'package:notas/widgets/note_card.dart';
|
||||
import 'package:notas/widgets/search_app_bar.dart';
|
||||
|
||||
@@ -18,14 +19,20 @@ import 'package:notas/widgets/search_app_bar.dart';
|
||||
// - Drag & drop reordering (updates `index` in the database)
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
const HomeScreen({
|
||||
super.key,
|
||||
required this.repository,
|
||||
required this.onDeleteAllData,
|
||||
});
|
||||
|
||||
final NoteRepository repository;
|
||||
final Future<void> Function() onDeleteAllData;
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
final NoteRepository _repository = NoteRepository();
|
||||
List<Note> _notes = <Note>[];
|
||||
String _searchQuery = '';
|
||||
bool _isLoading = true;
|
||||
@@ -39,7 +46,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Future<void> _loadNotes() async {
|
||||
final List<Note> storedNotes = await _repository.loadNotes();
|
||||
final List<Note> storedNotes = await widget.repository.loadNotes();
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
@@ -59,7 +66,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
if (result is Note) {
|
||||
final Note createdNote = await _repository.createNote(result);
|
||||
final Note createdNote = await widget.repository.createNote(result);
|
||||
final List<Note> updatedNotes = _normalizeNotes(<Note>[createdNote, ..._notes]);
|
||||
|
||||
if (!mounted) {
|
||||
@@ -73,7 +80,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Future<void> _deleteNote(Note note) async {
|
||||
await _repository.deleteNote(note);
|
||||
await widget.repository.deleteNote(note);
|
||||
|
||||
final List<Note> updatedNotes = _normalizeNotes(
|
||||
_notes.where((Note item) => item.id != note.id).toList(),
|
||||
@@ -97,7 +104,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
final Note movedNote = updatedNotes.removeAt(oldIndex);
|
||||
updatedNotes.insert(newIndex, movedNote);
|
||||
|
||||
await _repository.moveNote(movedNote, newIndex);
|
||||
await widget.repository.moveNote(movedNote, newIndex);
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
@@ -123,7 +130,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
if (result is Note) {
|
||||
final int noteIndex = _notes.indexWhere((Note item) => item == note);
|
||||
if (noteIndex != -1) {
|
||||
final Note savedNote = await _repository.updateNote(result);
|
||||
final Note savedNote = await widget.repository.updateNote(result);
|
||||
final List<Note> updatedNotes = [..._notes];
|
||||
updatedNotes[noteIndex] = savedNote;
|
||||
|
||||
@@ -293,8 +300,20 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
);
|
||||
|
||||
return Scaffold(
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Color(0xFF191A1D),
|
||||
Color(0xFF222326),
|
||||
Color(0xFF101114),
|
||||
],
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
const AppTitleBar(),
|
||||
SearchAppBar(
|
||||
@@ -353,6 +372,16 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
setState(() {
|
||||
_isMenuOpen = false;
|
||||
});
|
||||
|
||||
if (item == 'settings') {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => SettingsScreen(
|
||||
onDeleteAllData: widget.onDeleteAllData,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -361,7 +390,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _openNoteComposer,
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:notas/widgets/app_title_bar.dart';
|
||||
import 'package:notas/widgets/menu_drawer.dart';
|
||||
import 'package:notas/widgets/search_app_bar.dart';
|
||||
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
const SettingsScreen({
|
||||
super.key,
|
||||
required this.onDeleteAllData,
|
||||
});
|
||||
|
||||
final Future<void> Function() onDeleteAllData;
|
||||
|
||||
@override
|
||||
State<SettingsScreen> createState() => _SettingsScreenState();
|
||||
}
|
||||
|
||||
class _SettingsScreenState extends State<SettingsScreen> {
|
||||
bool _isBusy = false;
|
||||
bool _isMenuOpen = false;
|
||||
final GlobalKey _headerKey = GlobalKey();
|
||||
double _menuTopInset = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_updateMenuTopInset();
|
||||
});
|
||||
}
|
||||
|
||||
void _updateMenuTopInset() {
|
||||
final BuildContext? headerContext = _headerKey.currentContext;
|
||||
if (headerContext == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final RenderObject? renderObject = headerContext.findRenderObject();
|
||||
if (renderObject is! RenderBox) {
|
||||
return;
|
||||
}
|
||||
|
||||
final double newInset = renderObject.size.height;
|
||||
if ((newInset - _menuTopInset).abs() < 0.5) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_menuTopInset = newInset;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _confirmAndDeleteAll() async {
|
||||
final bool? confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Borrar todos los datos'),
|
||||
content: const Text('¿Estás seguro? Esta acción eliminará la base de datos local y la clave de cifrado. No se podrá recuperar.'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.of(context).pop(false), child: const Text('Cancelar')),
|
||||
TextButton(onPressed: () => Navigator.of(context).pop(true), child: const Text('Borrar', style: TextStyle(color: Colors.red))),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() {
|
||||
_isBusy = true;
|
||||
});
|
||||
|
||||
try {
|
||||
await widget.onDeleteAllData();
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Todos los datos locales han sido eliminados.')),
|
||||
);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
} catch (error) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Error al borrar los datos: $error')),
|
||||
);
|
||||
} finally {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_isBusy = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _handleMenuItemTapped(String item) {
|
||||
setState(() {
|
||||
_isMenuOpen = false;
|
||||
});
|
||||
|
||||
if (item == 'all_notes') {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
_updateMenuTopInset();
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Color(0xFF191A1D),
|
||||
Color(0xFF222326),
|
||||
Color(0xFF101114),
|
||||
],
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Stack(
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
Column(
|
||||
key: _headerKey,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const AppTitleBar(),
|
||||
SearchAppBar(
|
||||
onMenuPressed: () {
|
||||
setState(() {
|
||||
_isMenuOpen = !_isMenuOpen;
|
||||
});
|
||||
},
|
||||
showSearch: false,
|
||||
titleText: 'Configuración',
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.redAccent),
|
||||
onPressed: _isBusy ? null : _confirmAndDeleteAll,
|
||||
icon: _isBusy ? const SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2)) : const Icon(Icons.delete_forever),
|
||||
label: const Text('Borrar todos los datos'),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Esto cerrará el vault actual y eliminará la base de datos local junto con la clave de cifrado.'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
Positioned.fill(
|
||||
top: _menuTopInset,
|
||||
child: IgnorePointer(
|
||||
ignoring: !_isMenuOpen,
|
||||
child: AnimatedOpacity(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
opacity: _isMenuOpen ? 0.5 : 0.0,
|
||||
curve: Curves.easeOutCubic,
|
||||
child: GestureDetector(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_isMenuOpen = false;
|
||||
});
|
||||
},
|
||||
child: Container(color: Colors.black),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
AnimatedPositioned(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeOutCubic,
|
||||
left: _isMenuOpen ? 0 : -280,
|
||||
top: _menuTopInset,
|
||||
bottom: 0,
|
||||
width: 280,
|
||||
child: Material(
|
||||
color: const Color.fromRGBO(24, 25, 26, 1),
|
||||
elevation: 8,
|
||||
child: MenuDrawer(onMenuItemTapped: _handleMenuItemTapped),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:notas/widgets/app_title_bar.dart';
|
||||
|
||||
class VaultAccessScreen extends StatefulWidget {
|
||||
const VaultAccessScreen({
|
||||
super.key,
|
||||
required this.isBusy,
|
||||
required this.onCreateAccountPressed,
|
||||
required this.onSignInPressed,
|
||||
required this.onContinueWithoutAccount,
|
||||
});
|
||||
|
||||
final bool isBusy;
|
||||
final Future<void> Function(String email, String password) onCreateAccountPressed;
|
||||
final Future<void> Function(String email, String password) onSignInPressed;
|
||||
final Future<void> Function() onContinueWithoutAccount;
|
||||
|
||||
@override
|
||||
State<VaultAccessScreen> createState() => _VaultAccessScreenState();
|
||||
}
|
||||
|
||||
class _VaultAccessScreenState extends State<VaultAccessScreen> {
|
||||
final TextEditingController _emailController = TextEditingController();
|
||||
final TextEditingController _passwordController = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_emailController.dispose();
|
||||
_passwordController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _handleCreateAccount() async {
|
||||
await widget.onCreateAccountPressed(
|
||||
_emailController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _handleSignIn() async {
|
||||
await widget.onSignInPressed(
|
||||
_emailController.text.trim(),
|
||||
_passwordController.text,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Container(
|
||||
decoration: const BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
Color(0xFF191A1D),
|
||||
Color(0xFF222326),
|
||||
Color(0xFF101114),
|
||||
],
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
const AppTitleBar(),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 460),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(24),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFF1D1E20),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
border: Border.all(color: Colors.white.withValues(alpha: 0.08)),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: 0.35),
|
||||
blurRadius: 30,
|
||||
offset: const Offset(0, 18),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.lock_outline,
|
||||
color: Colors.amber,
|
||||
size: 44,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Mis Notas',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 30,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text(
|
||||
'Tus notas se guardan cifradas en este dispositivo. La cuenta y la sincronización vendrán después.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Colors.white.withValues(alpha: 0.72),
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
TextField(
|
||||
controller: _emailController,
|
||||
enabled: !widget.isBusy,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Email',
|
||||
labelStyle: TextStyle(color: Colors.white.withValues(alpha: 0.7)),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withValues(alpha: 0.05),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.12)),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.12)),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: const BorderSide(color: Colors.amber, width: 1.2),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _passwordController,
|
||||
enabled: !widget.isBusy,
|
||||
obscureText: true,
|
||||
style: const TextStyle(color: Colors.white),
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Contraseña',
|
||||
labelStyle: TextStyle(color: Colors.white.withValues(alpha: 0.7)),
|
||||
filled: true,
|
||||
fillColor: Colors.white.withValues(alpha: 0.05),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.12)),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: BorderSide(color: Colors.white.withValues(alpha: 0.12)),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
borderSide: const BorderSide(color: Colors.amber, width: 1.2),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 22),
|
||||
FilledButton(
|
||||
onPressed: widget.isBusy ? null : _handleCreateAccount,
|
||||
style: FilledButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
),
|
||||
child: widget.isBusy
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Text('Crear cuenta'),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
OutlinedButton(
|
||||
onPressed: widget.isBusy ? null : _handleSignIn,
|
||||
style: OutlinedButton.styleFrom(
|
||||
padding: const EdgeInsets.symmetric(vertical: 14),
|
||||
side: const BorderSide(color: Colors.white24),
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
child: const Text('Iniciar sesión'),
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
TextButton(
|
||||
onPressed: widget.isBusy ? null : widget.onContinueWithoutAccount,
|
||||
child: const Text('Entrar sin cuenta'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user