Skip to content

Commit 65f4e30

Browse files
authored
perf(add_category): autoselect category type based on transaction type (#402)
* perf(add_category): autoselect category type based on transaction type When creating a new category, the category type dropdown is preselected based on the transaction type (income or expense). - Import transaction provider in add_category - Read transaction type using the transaction provider - Default category type using transactionToCategoryProvider, or set to 'expense' if null - Remove redundant "hide income" logic in initial setup Ref: #390 * Remove unuseful comments * Fix dart format
1 parent fa3d533 commit 65f4e30

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lib/pages/categories/add_category.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import '../../constants/constants.dart';
44
import '../../constants/style.dart';
55
import '../../model/category_transaction.dart';
66
import '../../providers/categories_provider.dart';
7+
import '../../providers/transactions_provider.dart';
78
import '../../ui/device.dart';
89
import '../../ui/extensions.dart';
910

@@ -18,25 +19,27 @@ class AddCategory extends ConsumerStatefulWidget {
1819

1920
class _AddCategoryState extends ConsumerState<AddCategory> {
2021
final TextEditingController nameController = TextEditingController();
21-
CategoryTransactionType categoryType = CategoryTransactionType.income;
22+
late CategoryTransactionType categoryType;
2223
String categoryIcon = iconList.keys.first;
2324
int categoryColor = 0;
2425

2526
bool showCategoryIcons = false;
2627

2728
@override
2829
void initState() {
29-
if (widget.hideIncome) {
30-
categoryType = CategoryTransactionType.expense;
31-
}
30+
super.initState();
31+
32+
final transactionType = ref.read(transactionTypeProvider);
33+
categoryType = ref.read(transactionToCategoryProvider(transactionType)) ??
34+
CategoryTransactionType.expense;
35+
3236
final selectedCategory = ref.read(selectedCategoryProvider);
3337
if (selectedCategory != null) {
3438
nameController.text = selectedCategory.name;
3539
categoryType = selectedCategory.type;
3640
categoryIcon = selectedCategory.symbol;
3741
categoryColor = selectedCategory.color;
3842
}
39-
super.initState();
4043
}
4144

4245
@override

0 commit comments

Comments
 (0)