@@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
7
7
import '../../constants/functions.dart' ;
8
8
import '../../constants/style.dart' ;
9
9
import '../../model/transaction.dart' ;
10
+ import '../../providers/accounts_provider.dart' ;
10
11
import '../../providers/transactions_provider.dart' ;
11
12
import "widgets/account_selector.dart" ;
12
13
import 'widgets/amount_section.dart' ;
@@ -31,8 +32,10 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
31
32
32
33
@override
33
34
void initState () {
34
- amountController.text = numToCurrency (ref.read (selectedTransactionUpdateProvider)? .amount);
35
- noteController.text = ref.read (selectedTransactionUpdateProvider)? .note ?? '' ;
35
+ amountController.text =
36
+ numToCurrency (ref.read (selectedTransactionUpdateProvider)? .amount);
37
+ noteController.text =
38
+ ref.read (selectedTransactionUpdateProvider)? .note ?? '' ;
36
39
37
40
amountController.addListener (_updateAmount);
38
41
@@ -47,7 +50,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
47
50
48
51
if (recurrencyEditingPermittedFromRoute == null ) {
49
52
final argsMap = args as Map <String , dynamic >? ;
50
- recurrencyEditingPermittedFromRoute = argsMap? ['recurrencyEditingPermitted' ] ?? widget.recurrencyEditingPermitted;
53
+ recurrencyEditingPermittedFromRoute =
54
+ argsMap? ['recurrencyEditingPermitted' ] ??
55
+ widget.recurrencyEditingPermitted;
51
56
}
52
57
}
53
58
@@ -60,7 +65,8 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
60
65
61
66
String getCleanAmountString () {
62
67
// Remove all non-numeric characters
63
- var cleanNumberString = amountController.text.replaceAll (RegExp (r'[^0-9\.]' ), '' );
68
+ var cleanNumberString =
69
+ amountController.text.replaceAll (RegExp (r'[^0-9\.]' ), '' );
64
70
65
71
// Remove leading zeros only if the number does not start with "0."
66
72
if (! cleanNumberString.startsWith ('0.' )) {
@@ -95,6 +101,13 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
95
101
}
96
102
}
97
103
104
+ void _refreshAccountAndNavigateBack () {
105
+ ref
106
+ .read (accountsProvider.notifier)
107
+ .selectAndUpdateAccount (ref.read (bankAccountProvider)! )
108
+ .whenComplete (() => Navigator .of (context).pop ());
109
+ }
110
+
98
111
void _createOrUpdateTransaction () {
99
112
final selectedType = ref.read (transactionTypeProvider);
100
113
final selectedTransaction = ref.read (selectedTransactionUpdateProvider);
@@ -106,54 +119,68 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
106
119
if (selectedTransaction != null ) {
107
120
// if the original transaction is not recurrent, but user sets a recurrency, add the corrispondent record
108
121
// and edit the original transaction
109
- if (ref.read (selectedRecurringPayProvider) && ! selectedTransaction.recurring) {
122
+ if (ref.read (selectedRecurringPayProvider) &&
123
+ ! selectedTransaction.recurring) {
110
124
ref
111
125
.read (transactionsProvider.notifier)
112
- .addRecurringTransaction (currencyToNum (cleanAmount), noteController.text)
126
+ .addRecurringTransaction (
127
+ currencyToNum (cleanAmount), noteController.text)
113
128
.then ((value) {
114
- if (value != null ) {
115
- ref
116
- .read (transactionsProvider.notifier)
117
- .updateTransaction (currencyToNum (cleanAmount), noteController.text, value.id)
118
- .whenComplete (() => Navigator .of (context).pop ());
119
- }
120
- });
129
+ if (value != null ) {
130
+ ref
131
+ .read (transactionsProvider.notifier)
132
+ .updateTransaction (
133
+ currencyToNum (cleanAmount), noteController.text, value.id)
134
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
135
+ }
136
+ });
121
137
} else {
122
138
ref
123
139
.read (transactionsProvider.notifier)
124
- .updateTransaction (currencyToNum (cleanAmount), noteController.text, selectedTransaction.idRecurringTransaction)
125
- .whenComplete (() => Navigator .of (context).pop ());
140
+ .updateTransaction (
141
+ currencyToNum (cleanAmount),
142
+ noteController.text,
143
+ selectedTransaction.idRecurringTransaction)
144
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
126
145
}
127
-
128
-
129
146
} else {
130
147
if (selectedType == TransactionType .transfer) {
131
148
if (ref.read (bankAccountTransferProvider) != null ) {
132
149
ref
133
150
.read (transactionsProvider.notifier)
134
151
.addTransaction (currencyToNum (cleanAmount), noteController.text)
135
- .whenComplete (() => Navigator . of (context). pop ());
152
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
136
153
}
137
154
} else {
138
155
// It's an income or an expense
139
156
if (ref.read (categoryProvider) != null ) {
140
- if (ref.read (selectedRecurringPayProvider)) {
157
+ if (ref.read (selectedRecurringPayProvider)) {
141
158
ref
142
- .read (transactionsProvider.notifier)
143
- .addRecurringTransaction (currencyToNum (cleanAmount), noteController.text)
144
- .whenComplete (() => Navigator .of (context).pop ());
159
+ .read (transactionsProvider.notifier)
160
+ .addRecurringTransaction (
161
+ currencyToNum (cleanAmount), noteController.text)
162
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
145
163
} else {
146
164
ref
147
- .read (transactionsProvider.notifier)
148
- .addTransaction (currencyToNum (cleanAmount), noteController.text)
149
- .whenComplete (() => Navigator .of (context).pop ());
165
+ .read (transactionsProvider.notifier)
166
+ .addTransaction (
167
+ currencyToNum (cleanAmount), noteController.text)
168
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
150
169
}
151
170
}
152
171
}
153
172
}
154
173
}
155
174
}
156
175
176
+ void _deleteTransaction () {
177
+ final selectedTransaction = ref.read (selectedTransactionUpdateProvider);
178
+ ref
179
+ .read (transactionsProvider.notifier)
180
+ .deleteTransaction (selectedTransaction! .id! )
181
+ .whenComplete (() => _refreshAccountAndNavigateBack ());
182
+ }
183
+
157
184
@override
158
185
Widget build (BuildContext context) {
159
186
final selectedType = ref.watch (transactionTypeProvider);
@@ -164,14 +191,17 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
164
191
return Scaffold (
165
192
appBar: AppBar (
166
193
title: Text (
167
- (selectedTransaction != null ) ? "Editing transaction" : "New transaction" ,
194
+ (selectedTransaction != null )
195
+ ? "Editing transaction"
196
+ : "New transaction" ,
168
197
),
169
198
leadingWidth: 100 ,
170
199
leading: TextButton (
171
200
onPressed: () => Navigator .pop (context),
172
201
child: Text (
173
202
'Cancel' ,
174
- style: Theme .of (context).textTheme.titleMedium! .copyWith (color: blue5),
203
+ style:
204
+ Theme .of (context).textTheme.titleMedium! .copyWith (color: blue5),
175
205
),
176
206
),
177
207
actions: [
@@ -183,12 +213,7 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
183
213
Icons .delete_outline,
184
214
color: Theme .of (context).colorScheme.error,
185
215
),
186
- onPressed: () async {
187
- ref
188
- .read (transactionsProvider.notifier)
189
- .deleteTransaction (selectedTransaction.id! )
190
- .whenComplete (() => Navigator .pop (context));
191
- },
216
+ onPressed: _deleteTransaction,
192
217
),
193
218
)
194
219
: const SizedBox (),
@@ -298,8 +323,9 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
298
323
minimumYear: 2015 ,
299
324
maximumYear: 2050 ,
300
325
mode: CupertinoDatePickerMode .date,
301
- onDateTimeChanged: (date) =>
302
- ref.read (dateProvider.notifier).state = date,
326
+ onDateTimeChanged: (date) => ref
327
+ .read (dateProvider.notifier)
328
+ .state = date,
303
329
),
304
330
),
305
331
);
@@ -311,16 +337,18 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
311
337
lastDate: DateTime (2050 ),
312
338
);
313
339
if (pickedDate != null ) {
314
- ref.read (dateProvider.notifier).state = pickedDate;
340
+ ref.read (dateProvider.notifier).state =
341
+ pickedDate;
315
342
}
316
343
}
317
344
},
318
345
),
319
346
if (selectedType == TransactionType .expense) ...[
320
347
RecurrenceListTile (
321
- recurrencyEditingPermitted: widget.recurrencyEditingPermitted,
322
- selectedTransaction: ref.read (selectedTransactionUpdateProvider)
323
- )
348
+ recurrencyEditingPermitted:
349
+ widget.recurrencyEditingPermitted,
350
+ selectedTransaction:
351
+ ref.read (selectedTransactionUpdateProvider))
324
352
],
325
353
],
326
354
),
@@ -354,14 +382,15 @@ class _AddPageState extends ConsumerState<AddPage> with Functions {
354
382
onPressed: _createOrUpdateTransaction,
355
383
style: TextButton .styleFrom (
356
384
backgroundColor: Theme .of (context).colorScheme.secondary,
357
- shape: RoundedRectangleBorder (borderRadius: BorderRadius .circular (8 )),
385
+ shape: RoundedRectangleBorder (
386
+ borderRadius: BorderRadius .circular (8 )),
358
387
),
359
388
child: Text (
360
- selectedTransaction != null ? "UPDATE TRANSACTION" : "ADD TRANSACTION" ,
361
- style : Theme . of (context)
362
- .textTheme
363
- . bodyLarge!
364
- . copyWith ( color: Theme .of (context).colorScheme.onPrimary),
389
+ selectedTransaction != null
390
+ ? "UPDATE TRANSACTION"
391
+ : "ADD TRANSACTION" ,
392
+ style : Theme . of (context).textTheme. bodyLarge! . copyWith (
393
+ color: Theme .of (context).colorScheme.onPrimary),
365
394
),
366
395
),
367
396
),
0 commit comments