diff --git a/lib/data/repositories/account_repository.dart b/lib/data/repositories/account_repository.dart index dd336c6..3433419 100644 --- a/lib/data/repositories/account_repository.dart +++ b/lib/data/repositories/account_repository.dart @@ -172,14 +172,14 @@ class AccountRepository { )); } - Future add(model.Account account) async { - await _db.into(_db.accounts).insert( + Future add(model.Account account) async { + return await _db.into(_db.accounts).insert( AccountsCompanion.insert( - id: Value(account.id), name: account.name, isMain: Value(account.isMain), - currency: Value(account.currency), sortOrder: Value(account.sortOrder), + currency: Value(account.currency), + createdAt: Value(account.createdAt), ), ); } diff --git a/lib/features/dashboard/screen.dart b/lib/features/dashboard/screen.dart index 3e31135..da72cdc 100644 --- a/lib/features/dashboard/screen.dart +++ b/lib/features/dashboard/screen.dart @@ -162,7 +162,7 @@ class _DashboardScreenState extends ConsumerState { if (isAddingAccount) { // Create new account final newAccount = Account( - id: DateTime.now().millisecondsSinceEpoch, + id: DateTime.now().millisecondsSinceEpoch, // temporary name: tempAccountName.trim(), isMain: false, sortOrder: 99, @@ -170,14 +170,19 @@ class _DashboardScreenState extends ConsumerState { createdAt: DateTime.now(), ); + // Insert into database await ref.read(accountRepositoryProvider).add(newAccount); - // FIX: Fetch the actual ID assigned by the database to save colors correctly - final accounts = await ref.read(accountRepositoryProvider).getAll(); - final actualNewAccount = accounts.lastWhere((a) => a.name == tempAccountName.trim()); + // SECURE FIX: Fetch the actual ID assigned by SQLite, avoiding add() return issues + final allAccounts = await ref.read(accountRepositoryProvider).getAll(); + final actualAccount = allAccounts.lastWhere( + (a) => a.name == tempAccountName.trim() && a.currency == tempAccountCurrency, + orElse: () => allAccounts.last, + ); + // Save colors using the guaranteed correct database ID await ref - .read(accountCardColorsProvider(actualNewAccount.id).notifier) + .read(accountCardColorsProvider(actualAccount.id).notifier) .save(tempPrimary, tempSecondary, tempGradientType); } else if (editingAccount != null) { // Existing edit logic diff --git a/lib/features/dashboard/widgets/account_editor_overlay.dart b/lib/features/dashboard/widgets/account_editor_overlay.dart index 57fa696..abb7ee4 100644 --- a/lib/features/dashboard/widgets/account_editor_overlay.dart +++ b/lib/features/dashboard/widgets/account_editor_overlay.dart @@ -263,40 +263,14 @@ class _AccountEditorOverlayState extends State { ), // Top Right Buttons (Delete & Close) Positioned( - top: mq.padding.top + 8, + top: cardTop - 20, // Center buttons exactly on the top edge of the card right: 20, - child: SafeArea( - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - if (!dash.isAddingAccount && (ref.watch(accountsProvider).valueOrNull?.length ?? 0) > 1) ...[ - GestureDetector( - onTap: () => setState(() => _showDeleteDialog = true), - child: Container( - width: 40, - height: 40, - decoration: BoxDecoration( - color: Theme.of(widget.context).colorScheme.surface, - shape: BoxShape.circle, - boxShadow: [ - BoxShadow( - color: Colors.red.withOpacity(0.2), - blurRadius: 8, - offset: const Offset(0, 2), - ), - ], - ), - child: const Icon( - Icons.delete_outline_rounded, - size: 22, - color: Colors.red, - ), - ), - ), - const SizedBox(width: 12), - ], + child: Row( // REMOVED SafeArea to fix the vertical offset + mainAxisSize: MainAxisSize.min, + children: [ + if (!dash.isAddingAccount && (ref.watch(accountsProvider).valueOrNull?.length ?? 0) > 1) ...[ GestureDetector( - onTap: () => dash.closeAccountOverlay(apply: false), + onTap: () => setState(() => _showDeleteDialog = true), child: Container( width: 40, height: 40, @@ -305,21 +279,45 @@ class _AccountEditorOverlayState extends State { shape: BoxShape.circle, boxShadow: [ BoxShadow( - color: Colors.black.withOpacity(0.3), + color: Colors.red.withOpacity(0.2), blurRadius: 8, offset: const Offset(0, 2), ), ], ), - child: Icon( - Icons.close_rounded, - size: 24, - color: Theme.of(widget.context).colorScheme.onSurface, + child: const Icon( + Icons.delete_outline_rounded, + size: 22, + color: Colors.red, ), ), ), + const SizedBox(width: 12), ], - ), + GestureDetector( + onTap: () => dash.closeAccountOverlay(apply: false), + child: Container( + width: 40, + height: 40, + decoration: BoxDecoration( + color: Theme.of(widget.context).colorScheme.surface, + shape: BoxShape.circle, + boxShadow: [ + BoxShadow( + color: Colors.black.withOpacity(0.3), + blurRadius: 8, + offset: const Offset(0, 2), + ), + ], + ), + child: Icon( + Icons.close_rounded, + size: 24, + color: Theme.of(widget.context).colorScheme.onSurface, + ), + ), + ), + ], ), ), // Custom Dialog Overlay