ajout type dépense/revenu dans le dialogue transaction
This commit is contained in:
parent
83523b1811
commit
b321ebf7ed
117
lib/app.dart
117
lib/app.dart
|
|
@ -55,69 +55,82 @@ class _YnabAppState extends ConsumerState<YnabApp> {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final payeeCtrl = TextEditingController();
|
final payeeCtrl = TextEditingController();
|
||||||
final amountCtrl = TextEditingController();
|
final amountCtrl = TextEditingController();
|
||||||
int? accountId = accounts.first.id;
|
int? accountId = accounts.first.id;
|
||||||
DateTime date = DateTime.now();
|
DateTime date = DateTime.now();
|
||||||
|
bool isExpense = true;
|
||||||
|
|
||||||
await showDialog(
|
await showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (ctx) => AlertDialog(
|
builder: (ctx) => StatefulBuilder(
|
||||||
title: const Text('Nouvelle transaction'),
|
builder: (context, setDialogState) => AlertDialog(
|
||||||
content: SingleChildScrollView(
|
title: const Text('Nouvelle transaction'),
|
||||||
child: Column(
|
content: SingleChildScrollView(
|
||||||
mainAxisSize: MainAxisSize.min,
|
child: Column(
|
||||||
children: [
|
mainAxisSize: MainAxisSize.min,
|
||||||
TextField(
|
children: [
|
||||||
controller: payeeCtrl,
|
TextField(
|
||||||
decoration: const InputDecoration(labelText: 'Bénéficiaire', hintText: 'Ex: Supermarché'),
|
controller: payeeCtrl,
|
||||||
autofocus: true,
|
decoration: const InputDecoration(labelText: 'Bénéficiaire', hintText: 'Ex: Supermarché'),
|
||||||
),
|
autofocus: true,
|
||||||
const SizedBox(height: 12),
|
),
|
||||||
TextField(
|
const SizedBox(height: 12),
|
||||||
controller: amountCtrl,
|
TextField(
|
||||||
decoration: const InputDecoration(labelText: 'Montant', hintText: '0,00'),
|
controller: amountCtrl,
|
||||||
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
decoration: const InputDecoration(labelText: 'Montant', hintText: '0,00'),
|
||||||
),
|
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||||
const SizedBox(height: 12),
|
),
|
||||||
DropdownButtonFormField<int>(
|
const SizedBox(height: 12),
|
||||||
value: accountId,
|
SegmentedButton<bool>(
|
||||||
decoration: const InputDecoration(labelText: 'Compte'),
|
segments: const [
|
||||||
items: accounts
|
ButtonSegment(value: true, label: Text('Dépense'), icon: Icon(Icons.arrow_upward)),
|
||||||
.map((a) => DropdownMenuItem(value: a.id, child: Text(a.name)))
|
ButtonSegment(value: false, label: Text('Revenu'), icon: Icon(Icons.arrow_downward)),
|
||||||
.toList(),
|
],
|
||||||
onChanged: (v) => accountId = v,
|
selected: {isExpense},
|
||||||
),
|
onSelectionChanged: (v) => setDialogState(() => isExpense = v.first),
|
||||||
],
|
style: ButtonStyle(
|
||||||
|
visualDensity: VisualDensity.compact,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
DropdownButtonFormField<int>(
|
||||||
|
value: accountId,
|
||||||
|
decoration: const InputDecoration(labelText: 'Compte'),
|
||||||
|
items: accounts
|
||||||
|
.map((a) => DropdownMenuItem(value: a.id, child: Text(a.name)))
|
||||||
|
.toList(),
|
||||||
|
onChanged: (v) => accountId = v,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
|
actions: [
|
||||||
|
FilledButton.tonal(
|
||||||
|
onPressed: () => Navigator.pop(ctx),
|
||||||
|
child: const Text('Annuler'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () async {
|
||||||
|
final payee = payeeCtrl.text.trim();
|
||||||
|
final amountStr = amountCtrl.text.trim().replaceAll(',', '.');
|
||||||
|
final raw = double.tryParse(amountStr);
|
||||||
|
if (payee.isEmpty || raw == null || accountId == null) return;
|
||||||
|
final amount = isExpense ? -raw.abs() : raw.abs();
|
||||||
|
await db.into(db.transactions).insert(TransactionsCompanion.insert(
|
||||||
|
accountId: accountId!,
|
||||||
|
payee: payee,
|
||||||
|
amount: amount,
|
||||||
|
date: date,
|
||||||
|
));
|
||||||
|
if (ctx.mounted) Navigator.pop(ctx);
|
||||||
|
},
|
||||||
|
child: const Text('Ajouter'),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
actions: [
|
|
||||||
FilledButton.tonal(
|
|
||||||
onPressed: () => Navigator.pop(ctx),
|
|
||||||
child: const Text('Annuler'),
|
|
||||||
),
|
|
||||||
FilledButton(
|
|
||||||
onPressed: () async {
|
|
||||||
final payee = payeeCtrl.text.trim();
|
|
||||||
final amountStr = amountCtrl.text.trim().replaceAll(',', '.');
|
|
||||||
final amount = double.tryParse(amountStr);
|
|
||||||
if (payee.isEmpty || amount == null || accountId == null) return;
|
|
||||||
await db.into(db.transactions).insert(TransactionsCompanion.insert(
|
|
||||||
accountId: accountId!,
|
|
||||||
payee: payee,
|
|
||||||
amount: amount,
|
|
||||||
date: date,
|
|
||||||
));
|
|
||||||
if (ctx.mounted) Navigator.pop(ctx);
|
|
||||||
},
|
|
||||||
child: const Text('Ajouter'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
payeeCtrl.dispose();
|
|
||||||
amountCtrl.dispose();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _toggleSearch() {
|
void _toggleSearch() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue