From bcae40e0e2c1b4e04475ac1f2da26304d20aed83 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Wed, 5 Feb 2025 03:27:11 -0700 Subject: [PATCH] Buttons now close the dialog properly. --- lib/main.dart | 91 +++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 7a4536f..788d8c5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -54,7 +54,7 @@ class _HomePageState extends State { switch (pageSelected) { case 0: page = ExpensePage(); - dialog = ExpenseInputForm(); + dialog = ExpenseInputDialog(); case 1: page = IncomePage(); case 2: @@ -69,10 +69,7 @@ class _HomePageState extends State { Future addNewValue(BuildContext context) { return showDialog( - context: context, - builder: (_) => - AlertDialog(title: Text("Add New Value"), content: dialog) - ); + context: context, builder: (_) => AlertDialog(content: dialog)); } Widget? floatingButton; @@ -159,16 +156,16 @@ class ExpensePage extends StatelessWidget { } } -class ExpenseInputForm extends StatefulWidget { - const ExpenseInputForm({ +class ExpenseInputDialog extends StatefulWidget { + const ExpenseInputDialog({ super.key, }); @override - State createState() => _ExpenseInputFormState(); + State createState() => _ExpenseInputDialogState(); } -class _ExpenseInputFormState extends State { +class _ExpenseInputDialogState extends State { @override Widget build(BuildContext context) { const inputWidth = 300.0; @@ -182,54 +179,61 @@ class _ExpenseInputFormState extends State { DropdownMenuEntry(value: Recurrence.yearly, label: "Yearly"), ]; - return Center( - child: Column(mainAxisSize: MainAxisSize.min, spacing: 10, children: [ - Text('New Expense'), - SizedBox( + return AlertDialog( + title: Text("Add New Expense"), + content: Center( + child: Column(mainAxisSize: MainAxisSize.min, spacing: 10, children: [ + Text('New Expense'), + SizedBox( + width: inputWidth, + height: inputHeight, + child: TextField( + keyboardType: TextInputType.text, + decoration: InputDecoration( + labelText: "Name", + hintText: "Example: Red Pocket Phone Bill", + ), + // https://docs.flutter.dev/cookbook/forms/retrieve-input + //controller: nameFieldController, + )), + SizedBox( width: inputWidth, height: inputHeight, child: TextField( - keyboardType: TextInputType.text, + keyboardType: TextInputType.numberWithOptions(decimal: true), decoration: InputDecoration( - labelText: "Name", - hintText: "Example: Red Pocket Phone Bill", - ), - // https://docs.flutter.dev/cookbook/forms/retrieve-input - //controller: nameFieldController, - )), - SizedBox( - width: inputWidth, - height: inputHeight, - child: TextField( - keyboardType: TextInputType.numberWithOptions(decimal: true), - decoration: - InputDecoration(labelText: "Cost", hintText: "Example: 10.00"), + labelText: "Cost", hintText: "Example: 10.00"), + ), ), - ), - DropdownMenu( - dropdownMenuEntries: recurrenceValues, - width: inputWidth, - label: Text("Recurrence"), - hintText: "Example: Monthly", - ), - SizedBox( + DropdownMenu( + dropdownMenuEntries: recurrenceValues, width: inputWidth, - height: inputHeight, - child: TextField( - keyboardType: TextInputType.text, - decoration: InputDecoration( - labelText: "Description", - hintText: "Example: 1GB data with unlimited talk & text."), - )), + label: Text("Recurrence"), + hintText: "Example: Monthly", + ), + SizedBox( + width: inputWidth, + height: inputHeight, + child: TextField( + keyboardType: TextInputType.text, + decoration: InputDecoration( + labelText: "Description", + hintText: "Example: 1GB data with unlimited talk & text."), + )), + ]), + ), + actions: [ SizedBox( width: inputWidth, height: inputHeight, child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, + crossAxisAlignment: CrossAxisAlignment.center, children: [ ElevatedButton.icon( onPressed: () { print("TODO: Clear fields!"); + Navigator.of(context).pop(); }, icon: Icon(Icons.cancel), label: Text('Cancel'), @@ -237,13 +241,14 @@ class _ExpenseInputFormState extends State { ElevatedButton.icon( onPressed: () { print("TODO: Save expense!"); + Navigator.of(context).pop(); }, icon: Icon(Icons.save), label: Text('Submit'), ), ], )) - ]), + ], ); } }