diff --git a/lib/widgets/new_expense.dart b/lib/widgets/new_expense.dart index d595b62..7e568da 100644 --- a/lib/widgets/new_expense.dart +++ b/lib/widgets/new_expense.dart @@ -34,6 +34,35 @@ class _NewExpenseState extends State { }); } + void _submitExpenseData() { + final enteredAmount = double.tryParse(_amountController.text); + final amountIsInvalid = enteredAmount == null || enteredAmount <= 0; + + if (_titleController.text.trim().isEmpty || + amountIsInvalid || + _selectedCategory == null) { + showDialog( + context: context, + builder: (ctx) => AlertDialog( + title: const Text('Invalid input'), + content: const Text( + 'Please make sure a valid title, amount date and category was entered.'), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(ctx); + }, + child: const Text('Okay'), + ), + ], + ), + ); + return; + } + + // ... + } + @override void dispose() { _titleController.dispose(); @@ -118,10 +147,7 @@ class _NewExpenseState extends State { child: const Text('Cancel'), ), ElevatedButton( - onPressed: () { - print(_titleController.text); - print(_amountController.text); - }, + onPressed: _submitExpenseData, child: const Text('Save Expense'), ), ],