refactor: Improve code readability by formatting and simplifying widget structures

This commit is contained in:
2026-07-02 10:52:33 +02:00
parent b00da9ae88
commit c2db704155
3 changed files with 127 additions and 126 deletions
+29 -26
View File
@@ -86,7 +86,8 @@ class _HomeScreenState extends State<HomeScreen> {
try { try {
final List<Note> notes = await widget.repository.loadNotes(); final List<Note> notes = await widget.repository.loadNotes();
final List<Category> categories = await widget.repository.loadCategories(); final List<Category> categories = await widget.repository
.loadCategories();
final DateTime? lastSyncAt = await widget.repository.getLastSyncAt(); final DateTime? lastSyncAt = await widget.repository.getLastSyncAt();
if (!mounted) { if (!mounted) {
@@ -138,7 +139,9 @@ class _HomeScreenState extends State<HomeScreen> {
Iterable<Note> notes = _notes; Iterable<Note> notes = _notes;
if (_selectedCategoryId != null) { if (_selectedCategoryId != null) {
notes = notes.where((Note note) => note.categoryId == _selectedCategoryId); notes = notes.where(
(Note note) => note.categoryId == _selectedCategoryId,
);
} }
if (_searchQuery.isEmpty) { if (_searchQuery.isEmpty) {
@@ -181,8 +184,8 @@ class _HomeScreenState extends State<HomeScreen> {
RelativeRect _menuRectFromContext(BuildContext anchorContext) { RelativeRect _menuRectFromContext(BuildContext anchorContext) {
final RenderBox button = anchorContext.findRenderObject()! as RenderBox; final RenderBox button = anchorContext.findRenderObject()! as RenderBox;
final RenderBox overlay = Overlay.of(anchorContext).context.findRenderObject()! final RenderBox overlay =
as RenderBox; Overlay.of(anchorContext).context.findRenderObject()! as RenderBox;
final Offset topLeft = button.localToGlobal(Offset.zero, ancestor: overlay); final Offset topLeft = button.localToGlobal(Offset.zero, ancestor: overlay);
final Offset bottomRight = button.localToGlobal( final Offset bottomRight = button.localToGlobal(
button.size.bottomRight(Offset.zero), button.size.bottomRight(Offset.zero),
@@ -190,12 +193,7 @@ class _HomeScreenState extends State<HomeScreen> {
); );
return RelativeRect.fromRect( return RelativeRect.fromRect(
Rect.fromLTRB( Rect.fromLTRB(topLeft.dx, topLeft.dy, bottomRight.dx, bottomRight.dy),
topLeft.dx,
topLeft.dy,
bottomRight.dx,
bottomRight.dy,
),
Offset.zero & overlay.size, Offset.zero & overlay.size,
); );
} }
@@ -246,7 +244,10 @@ class _HomeScreenState extends State<HomeScreen> {
}); });
} }
Future<void> _changeNoteCategory(BuildContext anchorContext, Note note) async { Future<void> _changeNoteCategory(
BuildContext anchorContext,
Note note,
) async {
final Category? selected = await _showAnchoredCategoryMenu<Category?>( final Category? selected = await _showAnchoredCategoryMenu<Category?>(
anchorContext: anchorContext, anchorContext: anchorContext,
items: <PopupMenuEntry<Category?>>[ items: <PopupMenuEntry<Category?>>[
@@ -256,10 +257,7 @@ class _HomeScreenState extends State<HomeScreen> {
), ),
const PopupMenuDivider(), const PopupMenuDivider(),
for (final Category category in _categories) for (final Category category in _categories)
PopupMenuItem<Category?>( PopupMenuItem<Category?>(value: category, child: Text(category.name)),
value: category,
child: Text(category.name),
),
], ],
); );
@@ -409,9 +407,9 @@ class _HomeScreenState extends State<HomeScreen> {
return; return;
} }
await Navigator.of(context).push( await Navigator.of(
MaterialPageRoute<void>(builder: (_) => editor), context,
); ).push(MaterialPageRoute<void>(builder: (_) => editor));
} }
Future<void> _handleNoteTap(Note note, bool isDesktop) async { Future<void> _handleNoteTap(Note note, bool isDesktop) async {
@@ -430,7 +428,8 @@ class _HomeScreenState extends State<HomeScreen> {
} }
final Note movedNote = visibleNotes[oldIndex]; final Note movedNote = visibleNotes[oldIndex];
final List<Note> remainingVisible = <Note>[...visibleNotes]..removeAt(oldIndex); final List<Note> remainingVisible = <Note>[...visibleNotes]
..removeAt(oldIndex);
final int clampedNewIndex = newIndex.clamp(0, remainingVisible.length); final int clampedNewIndex = newIndex.clamp(0, remainingVisible.length);
int targetFullIndex; int targetFullIndex;
@@ -442,7 +441,9 @@ class _HomeScreenState extends State<HomeScreen> {
targetFullIndex = _notes.length - 1; targetFullIndex = _notes.length - 1;
} else { } else {
final Note afterNote = remainingVisible[clampedNewIndex]; final Note afterNote = remainingVisible[clampedNewIndex];
targetFullIndex = _notes.indexWhere((Note note) => note.id == afterNote.id); targetFullIndex = _notes.indexWhere(
(Note note) => note.id == afterNote.id,
);
if (targetFullIndex < 0) { if (targetFullIndex < 0) {
targetFullIndex = 0; targetFullIndex = 0;
} }
@@ -501,7 +502,10 @@ class _HomeScreenState extends State<HomeScreen> {
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Buscar notas...', hintText: 'Buscar notas...',
hintStyle: TextStyle(color: palette.textSecondary), hintStyle: TextStyle(color: palette.textSecondary),
prefixIcon: Icon(Icons.search, color: palette.textSecondary), prefixIcon: Icon(
Icons.search,
color: palette.textSecondary,
),
suffixIcon: _searchQuery.isEmpty suffixIcon: _searchQuery.isEmpty
? null ? null
: IconButton( : IconButton(
@@ -582,10 +586,7 @@ class _HomeScreenState extends State<HomeScreen> {
padding: const EdgeInsets.only(top: 8, bottom: 88), padding: const EdgeInsets.only(top: 8, bottom: 88),
child: Text( child: Text(
_formatLastSyncAt(), _formatLastSyncAt(),
style: TextStyle( style: TextStyle(color: palette.textSecondary, fontSize: 12),
color: palette.textSecondary,
fontSize: 12,
),
), ),
), ),
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
@@ -598,6 +599,7 @@ class _HomeScreenState extends State<HomeScreen> {
child: NoteCard( child: NoteCard(
note: note, note: note,
isSelected: note.id == _selectedNoteId, isSelected: note.id == _selectedNoteId,
showSelectionBorder: isDesktop,
onTap: () => _handleNoteTap(note, isDesktop), onTap: () => _handleNoteTap(note, isDesktop),
onDelete: () => _deleteNote(note), onDelete: () => _deleteNote(note),
onChangeCategory: (BuildContext buttonContext) => onChangeCategory: (BuildContext buttonContext) =>
@@ -637,7 +639,8 @@ class _HomeScreenState extends State<HomeScreen> {
final AppPalette palette = _paletteOf(context); final AppPalette palette = _paletteOf(context);
final double leftWidth = (constraints.maxWidth * 0.34).clamp(320, 440); final double leftWidth = (constraints.maxWidth * 0.34).clamp(320, 440);
final Note? selectedNote = _selectedNote(); final Note? selectedNote = _selectedNote();
final bool selectedIsVisible = selectedNote != null && final bool selectedIsVisible =
selectedNote != null &&
_visibleNotes().any((Note note) => note.id == selectedNote.id); _visibleNotes().any((Note note) => note.id == selectedNote.id);
return Row( return Row(
+14 -20
View File
@@ -116,8 +116,8 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
RelativeRect _menuRectFromContext(BuildContext anchorContext) { RelativeRect _menuRectFromContext(BuildContext anchorContext) {
final RenderBox button = anchorContext.findRenderObject()! as RenderBox; final RenderBox button = anchorContext.findRenderObject()! as RenderBox;
final RenderBox overlay = Overlay.of(anchorContext).context.findRenderObject()! final RenderBox overlay =
as RenderBox; Overlay.of(anchorContext).context.findRenderObject()! as RenderBox;
final Offset topLeft = button.localToGlobal(Offset.zero, ancestor: overlay); final Offset topLeft = button.localToGlobal(Offset.zero, ancestor: overlay);
final Offset bottomRight = button.localToGlobal( final Offset bottomRight = button.localToGlobal(
button.size.bottomRight(Offset.zero), button.size.bottomRight(Offset.zero),
@@ -125,12 +125,7 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
); );
return RelativeRect.fromRect( return RelativeRect.fromRect(
Rect.fromLTRB( Rect.fromLTRB(topLeft.dx, topLeft.dy, bottomRight.dx, bottomRight.dy),
topLeft.dx,
topLeft.dy,
bottomRight.dx,
bottomRight.dy,
),
Offset.zero & overlay.size, Offset.zero & overlay.size,
); );
} }
@@ -157,7 +152,8 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
isDirty: true, isDirty: true,
); );
final bool hasChanges = draft.title != _baselineNote.title || final bool hasChanges =
draft.title != _baselineNote.title ||
draft.body != _baselineNote.body || draft.body != _baselineNote.body ||
draft.categoryId != _baselineNote.categoryId; draft.categoryId != _baselineNote.categoryId;
@@ -206,10 +202,7 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
), ),
const PopupMenuDivider(), const PopupMenuDivider(),
for (final Category category in widget.categories) for (final Category category in widget.categories)
PopupMenuItem<Category?>( PopupMenuItem<Category?>(value: category, child: Text(category.name)),
value: category,
child: Text(category.name),
),
], ],
); );
@@ -273,8 +266,11 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
); );
} }
Widget _buildEditorBody() { Widget _buildEditorBody({required bool embedded}) {
final AppPalette palette = _paletteOf(context); final AppPalette palette = _paletteOf(context);
final BoxBorder? bodyBorder = embedded
? null
: Border.all(color: palette.border);
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.stretch, crossAxisAlignment: CrossAxisAlignment.stretch,
@@ -309,7 +305,7 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
decoration: BoxDecoration( decoration: BoxDecoration(
color: palette.transparent, color: palette.transparent,
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
border: Border.all(color: palette.border), border: bodyBorder,
), ),
padding: const EdgeInsets.all(14), padding: const EdgeInsets.all(14),
child: QuillEditor.basic( child: QuillEditor.basic(
@@ -372,18 +368,16 @@ class _NoteEditorScreenState extends State<NoteEditorScreen> {
final Widget editor = Padding( final Widget editor = Padding(
padding: const EdgeInsets.all(20), padding: const EdgeInsets.all(20),
child: _buildEditorBody(), child: _buildEditorBody(embedded: widget.embedded),
); );
if (widget.embedded) { if (widget.embedded) {
return Container(color: palette.cardBackground, child: editor); return editor;
} }
return Scaffold( return Scaffold(
backgroundColor: palette.cardBackground, backgroundColor: palette.cardBackground,
appBar: AppBar( appBar: AppBar(title: const Text('Editar nota')),
title: const Text('Editar nota'),
),
body: SafeArea(child: editor), body: SafeArea(child: editor),
); );
} }
+12 -8
View File
@@ -13,6 +13,7 @@ class NoteCard extends StatelessWidget {
this.onTap, this.onTap,
this.onDelete, this.onDelete,
this.onChangeCategory, this.onChangeCategory,
this.showSelectionBorder = true,
}); });
final Note note; final Note note;
@@ -21,6 +22,7 @@ class NoteCard extends StatelessWidget {
final VoidCallback? onTap; final VoidCallback? onTap;
final VoidCallback? onDelete; final VoidCallback? onDelete;
final ValueChanged<BuildContext>? onChangeCategory; final ValueChanged<BuildContext>? onChangeCategory;
final bool showSelectionBorder;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -31,16 +33,21 @@ return Material(
color: Colors.transparent, // 1. Fondo completamente transparente color: Colors.transparent, // 1. Fondo completamente transparente
shape: BorderDirectional( shape: BorderDirectional(
start: BorderSide( start: BorderSide(
color: isSelected ? palette.accent : Colors.transparent, color: (isSelected && showSelectionBorder)
? palette.accent
: Colors.transparent,
width: isSelected ? 1.6 : 1.0, width: isSelected ? 1.6 : 1.0,
), ),
), ),
child: InkWell( child: InkWell(
borderRadius: BorderRadius.circular(14), borderRadius: BorderRadius.circular(14),
onTap: onTap, onTap: onTap,
hoverColor: Colors.transparent, // 2. Desactiva el efecto hover (pasar el ratón) hoverColor:
splashColor: Colors.transparent, // 3. Desactiva el efecto de onda al hacer clic Colors.transparent, // 2. Desactiva el efecto hover (pasar el ratón)
highlightColor: Colors.transparent, // Desactiva el brillo al mantener pulsado splashColor:
Colors.transparent, // 3. Desactiva el efecto de onda al hacer clic
highlightColor:
Colors.transparent, // Desactiva el brillo al mantener pulsado
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12), padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
child: Row( child: Row(
@@ -78,10 +85,7 @@ return Material(
const SizedBox(width: 8), const SizedBox(width: 8),
PopupMenuButton<String>( PopupMenuButton<String>(
tooltip: 'Más opciones', tooltip: 'Más opciones',
icon: Icon( icon: Icon(Icons.more_vert, color: palette.textSecondary),
Icons.more_vert,
color: palette.textSecondary,
),
onOpened: () {}, onOpened: () {},
onSelected: (String value) { onSelected: (String value) {
switch (value) { switch (value) {