[Functional Improvement] Auto-Select Category Type when creating a New Category from a New Transaction
📝 Improvement Description
When creating a new category from the "New Transaction" screen, the default category type should match the type of the selected transaction: Income or Expense.
However, the user should still have the option to change the category type during the category creation process.
🛑 Problem Statement
Currently, when creating a new Expense transaction, if the user decides to add a new Expense category, the default category type is set to Income. If the user does not change this default selection, they may mistakenly create an Income category instead.
After this incorrect creation, there is no clear feedback indicating the mistake. The only visible sign that something went wrong is the absence of the newly created category in the Expense category list.
The only way for the user to realize the issue is to switch to an Income transaction and attempt to add a category. The incorrectly created category will be listed there.
https://github.com/user-attachments/assets/ddf658e1-1606-42fe-8dc1-8a2f049ead0a
💡 Proposed Solution
When creating a category from the New Transaction screen, the default category type should automatically match the transaction type.
🌿 Benefits
- Improves consistency in category creation.
- Reduces errors and unintended category misclassification.
- Minimizes the number of taps required for create an Expense category, improving the user experience.
🧱 Impact Area
- [x] Backend
- [x] Frontend
- [ ] Infrastructure
- [ ] DevOps
- [ ] Other
✍️ Additional Context
This issue was tested in the latest version of Sossoldi: v0.1.5-beta
📜 Code of Conduct
- [x] ✅ I agree to the Code of Conduct
I would like to have this issue assigned to me. I might need pair programming to resolve it, so I'll ask in the Discord group.
I pushed my pull request #402. I'm new to Flutter and trying to learn by working on small pieces of code.
In my pull request, I had to understand what a provider is. This was really helpful: Video: Flutter Provider Simply Explained.
What I Learned
1. Understanding Riverpod in initState
During the analysis, I quickly figured out what needed to be done: use the transaction provider to read the transaction type and set the initial state of the category type.
The problem started when I used Riverpod in the initState block to read the transaction type with:
@override
void initState() {
[...]
// ❌ WRONG: 'watch' cannot be used in initState, it will cause an exception!
final transactionType = ref.watch(transactionTypeProvider);
[...]
}
instead of:
@override
void initState() {
[...]
final transactionType = ref.read(transactionTypeProvider); // ✅ OK
[...]
}
The difference is quite simple:
-
readretrieves the current state once. -
watchlistens for state changes and rebuilds when it updates.
Since initState is called only once when the widget is created, it cannot listen for state changes. Using watch inside initState causes an exception.
In this case, we only need the current transaction type at initialization, so read is the correct choice.
2. Reducing Video Size with Handbrake
In my pull request #402, I included screen recordings to demonstrate the tests I ran on the component.
To reduce the file size of these recordings, I discovered and used Handbrake, an excellent open-source cross-platform tool that helped me compress the videos by 90% using a simple "web-optimized" profile.