I currently have the following code to build this widget:
MonthlyAnnualSection(
title: 'Tax Expense',
onValueChanged: (value) {
setState(() {
defaults.shouldDisplayTaxesExpensesMonthly =
value == 'm';
});
},
children: [
Panel(
key: Key(
'taxes${defaults.shouldDisplayTaxesExpensesMonthly ? 'Monthly' : 'Annual'}'),
title: '💸 Taxes',
isPercentage:
defaults.shouldDisplayTaxesAsPercentage,
onIsPercentageChanged: (val) {
setState(() {
defaults.shouldDisplayTaxesAsPercentage = val;
});
},
child: Column(
children: [
SizedBox(height: 10),
if (defaults.shouldDisplayTaxesAsPercentage)
PercentageTextField(
value: defaults
.shouldDisplayTaxesExpensesMonthly
? defaults.taxesPercent
: defaults.taxesPercent * 12,
placeholder:
'example: ${(defaults.shouldDisplayTaxesExpensesMonthly ? defaults.taxesPercent : defaults.taxesPercent * 12).toPercentage()}',
onValueChanged: (val) {
setState(() {
defaults.taxesPercent = defaults
.shouldDisplayTaxesExpensesMonthly
? val
: val / 12;
});
},
),
if (!defaults.shouldDisplayTaxesAsPercentage)
CurrencyTextField(
value: defaults
.shouldDisplayTaxesExpensesMonthly
? defaults.taxes
: defaults.taxes * 12,
placeholder:
'example: ${(defaults.shouldDisplayTaxesExpensesMonthly ? defaults.taxes : defaults.taxes * 12).toCurrency()}',
onValueChanged: (val) {
setState(() {
defaults.taxes = defaults
.shouldDisplayTaxesExpensesMonthly
? val
: val / 12;
});
},
),
SizedBox(height: 10),
],
),
),
],
),
and this is the class where that defaults
is coming from:
class Defaults {
bool shouldDisplayTaxesExpensesMonthly;
bool shouldDisplayTaxesAsPercentage;
double taxes;
double taxesPercent;
bool shouldDisplayManagementExpensesMonthly;
bool shouldDisplayManagementAsPercentage;
double management;
double managementPercent;
}
The problem as you can see, is that it takes a lot of ternary methods plus if I want to make a second similar widget below it (to use it for the management expenses, instead of tax) it takes a LOT of repeating.
Ideally I would love to just have something like this:
Section(
title: 'Tax Expense',
subtitle: '💸 Taxes',
value: defaults.taxes,
percent: defaults.taxesPercent,
shouldDisplayMonthly: defaults.shouldDisplayTaxesExpensesMonthly,
shouldDisplayAsPercentage: defaults.shouldDisplayTaxesAsPercentage,
),
Section(
title: 'Management Expense',
subtitle: '💸 Management',
value: defaults.management,
percent: defaults.managementPercent,
shouldDisplayMonthly: defaults.shouldDisplayManagementExpensesMonthly,
shouldDisplayAsPercentage: defaults.shouldDisplayManagementsAsPercentage,
),
And then it would be sorted... but apparently Dart doesn't support reflection, so I'm not sure how I could do that?
Any help is appreciated