diff --git a/lib/app.dart b/lib/app.dart index 6e8668e..c17e3eb 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -55,69 +55,82 @@ class _YnabAppState extends ConsumerState { ); return; } - final payeeCtrl = TextEditingController(); final amountCtrl = TextEditingController(); int? accountId = accounts.first.id; DateTime date = DateTime.now(); + bool isExpense = true; await showDialog( context: context, - builder: (ctx) => AlertDialog( - title: const Text('Nouvelle transaction'), - content: SingleChildScrollView( - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - TextField( - controller: payeeCtrl, - decoration: const InputDecoration(labelText: 'Bénéficiaire', hintText: 'Ex: Supermarché'), - autofocus: true, - ), - const SizedBox(height: 12), - TextField( - controller: amountCtrl, - decoration: const InputDecoration(labelText: 'Montant', hintText: '0,00'), - keyboardType: const TextInputType.numberWithOptions(decimal: true), - ), - const SizedBox(height: 12), - DropdownButtonFormField( - value: accountId, - decoration: const InputDecoration(labelText: 'Compte'), - items: accounts - .map((a) => DropdownMenuItem(value: a.id, child: Text(a.name))) - .toList(), - onChanged: (v) => accountId = v, - ), - ], + builder: (ctx) => StatefulBuilder( + builder: (context, setDialogState) => AlertDialog( + title: const Text('Nouvelle transaction'), + content: SingleChildScrollView( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextField( + controller: payeeCtrl, + decoration: const InputDecoration(labelText: 'Bénéficiaire', hintText: 'Ex: Supermarché'), + autofocus: true, + ), + const SizedBox(height: 12), + TextField( + controller: amountCtrl, + decoration: const InputDecoration(labelText: 'Montant', hintText: '0,00'), + keyboardType: const TextInputType.numberWithOptions(decimal: true), + ), + const SizedBox(height: 12), + SegmentedButton( + segments: const [ + ButtonSegment(value: true, label: Text('Dépense'), icon: Icon(Icons.arrow_upward)), + ButtonSegment(value: false, label: Text('Revenu'), icon: Icon(Icons.arrow_downward)), + ], + selected: {isExpense}, + onSelectionChanged: (v) => setDialogState(() => isExpense = v.first), + style: ButtonStyle( + visualDensity: VisualDensity.compact, + ), + ), + const SizedBox(height: 12), + DropdownButtonFormField( + 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() {