This commit is contained in:
2026-03-24 16:40:00 +03:00
parent 494ff12cb0
commit e0fa55c6f5
3 changed files with 213 additions and 111 deletions
@@ -171,4 +171,20 @@ class AccountRepository {
createdAt: Value(account.createdAt), createdAt: Value(account.createdAt),
)); ));
} }
Future<void> add(model.Account account) async {
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),
),
);
}
Future<void> delete(int id) async {
await (_db.delete(_db.accounts)..where((a) => a.id.equals(id))).go();
}
} }
+14 -16
View File
@@ -2,11 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:drift/drift.dart' as drift;
import '../../core/l10n/locale_provider.dart'; import '../../core/l10n/locale_provider.dart';
import '../../core/services/card_color_service.dart'; import '../../core/services/card_color_service.dart';
import '../../core/services/haptic_service.dart'; import '../../core/services/haptic_service.dart';
import '../../data/database/app_database.dart' hide Account;
import '../../shared/models/account.dart'; import '../../shared/models/account.dart';
import '../settings/provider.dart'; import '../settings/provider.dart';
import 'provider.dart'; import 'provider.dart';
@@ -163,23 +161,23 @@ class _DashboardScreenState extends ConsumerState<DashboardScreen> {
if (isAddingAccount) { if (isAddingAccount) {
// Create new account // Create new account
final newId = DateTime.now().millisecondsSinceEpoch; final newAccount = Account(
id: DateTime.now().millisecondsSinceEpoch,
// Insert the new account using Drift's insert method name: tempAccountName.trim(),
final db = ref.read(appDatabaseProvider); isMain: false,
await db.into(db.accounts).insert( sortOrder: 99,
AccountsCompanion.insert( currency: tempAccountCurrency,
id: drift.Value(newId), createdAt: DateTime.now(),
name: tempAccountName.trim(),
isMain: const drift.Value(false),
currency: drift.Value(tempAccountCurrency),
sortOrder: const drift.Value(99),
),
); );
// Save the chosen colors for the newly created account 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());
await ref await ref
.read(accountCardColorsProvider(newId).notifier) .read(accountCardColorsProvider(actualNewAccount.id).notifier)
.save(tempPrimary, tempSecondary, tempGradientType); .save(tempPrimary, tempSecondary, tempGradientType);
} else if (editingAccount != null) { } else if (editingAccount != null) {
// Existing edit logic // Existing edit logic
@@ -36,6 +36,7 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
late String _selectedCurrency; late String _selectedCurrency;
bool _showCurrencyDropdown = false; bool _showCurrencyDropdown = false;
bool _showLimitError = false; bool _showLimitError = false;
bool _showDeleteDialog = false;
@override @override
void initState() { void initState() {
@@ -260,36 +261,117 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
), ),
), ),
), ),
// Close Button - Top Right // Top Right Buttons (Delete & Close)
Positioned( Positioned(
top: mq.padding.top + 8, top: mq.padding.top + 8,
right: 20, right: 20,
child: SafeArea( child: SafeArea(
child: GestureDetector( child: Row(
onTap: () => dash.closeAccountOverlay(apply: false), mainAxisSize: MainAxisSize.min,
child: Container( children: [
width: 40, if (!dash.isAddingAccount && (ref.watch(accountsProvider).valueOrNull?.length ?? 0) > 1) ...[
height: 40, GestureDetector(
decoration: BoxDecoration( onTap: () => setState(() => _showDeleteDialog = true),
color: Theme.of(widget.context).colorScheme.surface, child: Container(
shape: BoxShape.circle, width: 40,
boxShadow: [ height: 40,
BoxShadow( decoration: BoxDecoration(
color: Colors.black.withOpacity(0.3), color: Theme.of(widget.context).colorScheme.surface,
blurRadius: 8, shape: BoxShape.circle,
offset: const Offset(0, 2), 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),
],
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,
),
),
), ),
child: Icon( ],
Icons.close_rounded, ),
size: 24, ),
color: Theme.of(widget.context).colorScheme.onSurface, ),
// Custom Dialog Overlay
if (_showDeleteDialog)
Positioned.fill(
child: Container(
color: Colors.black.withOpacity(0.4),
child: Center(
child: Material(
color: Colors.transparent,
child: AlertDialog(
backgroundColor: Theme.of(widget.context).colorScheme.surface,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
title: const Text('Delete Account?'),
content: const Text(
'Are you sure you want to delete this account? All associated transactions will also be permanently deleted.',
),
actions: [
TextButton(
onPressed: () => setState(() => _showDeleteDialog = false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () async {
if (dash.editingAccount == null) return;
final accountId = dash.editingAccount!.id;
dash.closeAccountOverlay(apply: false);
final txs = ref.read(transactionsProvider).valueOrNull ?? [];
final accountTxs = txs.where((t) => t.accountId == accountId).toList();
for (final t in accountTxs) {
await ref.read(transactionsProvider.notifier).delete(t.id);
}
await ref.read(accountRepositoryProvider).delete(accountId);
if (ref.read(hapticEnabledProvider)) {
HapticService.medium();
}
},
child: const Text('Delete', style: TextStyle(color: Colors.red)),
),
],
),
), ),
), ),
), ),
), ),
),
], ],
), ),
); );
@@ -298,45 +380,47 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
} }
Widget _buildEditorPanel(double panelHeight) { Widget _buildEditorPanel(double panelHeight) {
return Container( return Consumer(
height: panelHeight, builder: (context, ref, _) {
decoration: BoxDecoration( return Container(
color: Theme.of(widget.context).colorScheme.surface, height: panelHeight,
borderRadius: BorderRadius.circular(20), decoration: BoxDecoration(
border: Border.all( color: Theme.of(widget.context).colorScheme.surface,
color: Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.1), borderRadius: BorderRadius.circular(20),
width: 1.5, border: Border.all(
), color: Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.1),
boxShadow: [ width: 1.5,
BoxShadow(
color: Colors.black.withOpacity(0.3),
blurRadius: 24,
offset: const Offset(0, 8),
),
],
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Account Settings',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.6),
),
), ),
const SizedBox(height: 8), boxShadow: [
IntrinsicHeight( BoxShadow(
child: Row( color: Colors.black.withOpacity(0.3),
crossAxisAlignment: CrossAxisAlignment.start, blurRadius: 24,
children: [ offset: const Offset(0, 8),
Expanded( ),
flex: 3, ],
child: TextField( ),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Account Settings',
style: TextStyle(
fontSize: 11,
fontWeight: FontWeight.w600,
color: Theme.of(widget.context).colorScheme.onSurface.withOpacity(0.6),
),
),
const SizedBox(height: 8),
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 3,
child: TextField(
controller: _nameController, controller: _nameController,
buildCounter: (context, {required currentLength, required isFocused, maxLength}) => null, buildCounter: (context, {required currentLength, required isFocused, maxLength}) => null,
style: const TextStyle(fontSize: 13), style: const TextStyle(fontSize: 13),
@@ -423,6 +507,8 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
), ),
), ),
); );
},
);
} }
Widget _buildColorPanel(double panelHeight) { Widget _buildColorPanel(double panelHeight) {
@@ -521,48 +607,50 @@ class _AccountEditorOverlayState extends State<AccountEditorOverlay> {
margin: const EdgeInsets.symmetric(vertical: 4), margin: const EdgeInsets.symmetric(vertical: 4),
), ),
), ),
GestureDetector( Expanded(
onTap: isSolid ? null : () { child: GestureDetector(
dash.setState(() { onTap: isSolid ? null : () {
dash.tempGradientType = GradientType.solid; dash.setState(() {
dash.editingPrimary = true; dash.tempGradientType = GradientType.solid;
}); dash.editingPrimary = true;
setPanelState(() {}); });
dash.overlayEntry?.markNeedsBuild(); setPanelState(() {});
}, dash.overlayEntry?.markNeedsBuild();
child: Container( },
height: double.infinity, child: Container(
padding: const EdgeInsets.symmetric( height: double.infinity,
horizontal: 10, vertical: 6), padding: const EdgeInsets.symmetric(
decoration: BoxDecoration( horizontal: 4, vertical: 6),
color: isSolid decoration: BoxDecoration(
? const Color(0xFF7C6DED).withOpacity(0.15)
: Colors.transparent,
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: isSolid color: isSolid
? const Color(0xFF7C6DED) ? const Color(0xFF7C6DED).withOpacity(0.15)
: Theme.of(widget.context) : Colors.transparent,
.colorScheme borderRadius: BorderRadius.circular(10),
.onSurface border: Border.all(
.withOpacity(0.2),
width: 1.5,
),
),
child: Center(
child: Text(
s.colorSolid,
style: TextStyle(
fontSize: 11,
fontWeight: isSolid
? FontWeight.w600
: FontWeight.normal,
color: isSolid color: isSolid
? const Color(0xFF7C6DED) ? const Color(0xFF7C6DED)
: Theme.of(widget.context) : Theme.of(widget.context)
.colorScheme .colorScheme
.onSurface .onSurface
.withOpacity(0.5), .withOpacity(0.2),
width: 1.5,
),
),
child: Center(
child: Text(
s.colorSolid,
style: TextStyle(
fontSize: 11,
fontWeight: isSolid
? FontWeight.w600
: FontWeight.normal,
color: isSolid
? const Color(0xFF7C6DED)
: Theme.of(widget.context)
.colorScheme
.onSurface
.withOpacity(0.5),
),
), ),
), ),
), ),
@@ -937,7 +1025,7 @@ class PanelTab extends StatelessWidget {
child: AnimatedContainer( child: AnimatedContainer(
duration: const Duration(milliseconds: 150), duration: const Duration(milliseconds: 150),
width: double.infinity, width: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8), padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
color: isSelected ? const Color(0xFF7C6DED).withOpacity(0.15) : Colors.transparent, color: isSelected ? const Color(0xFF7C6DED).withOpacity(0.15) : Colors.transparent,
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),