diff --git a/lib/main.dart b/lib/main.dart index 7a878eb..339dbd5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -70,7 +70,9 @@ class _HomePageState extends State { Future addNewValue(BuildContext context) { return showDialog( - context: context, builder: (_) => AlertDialog(content: dialog)); + context: context, + builder: (_) => AlertDialog(content: dialog), + ); } Widget? floatingButton; diff --git a/lib/pages/expense.dart b/lib/pages/expense.dart index 5c45ce7..f4f023a 100644 --- a/lib/pages/expense.dart +++ b/lib/pages/expense.dart @@ -17,6 +17,10 @@ class ExpensePage extends StatefulWidget { } class _ExpensePageState extends State { + refresh() { + setState(() {}); + } + @override Widget build(BuildContext context) { expenses.sort( @@ -39,14 +43,31 @@ class _ExpensePageState extends State { child: Dismissible( key: Key(curr.toString()), background: Container( - color: Colors.red, + color: Colors.blue, child: Row( + mainAxisSize: MainAxisSize.max, children: [ - Icon(Icons.delete), - Text("Delete!"), + Container( + color: Colors.red, + child: Row( + mainAxisSize: MainAxisSize.max, + children: [ + Icon(Icons.delete), + Text("Delete"), + ], + ), + ), Spacer(), - Text("Delete!"), - Icon(Icons.delete), + Container( + color: Colors.yellow, + child: Row( + mainAxisSize: MainAxisSize.max, + children: [ + Text("Edit"), + Icon(Icons.edit), + ], + ), + ), ], ), ), @@ -54,6 +75,26 @@ class _ExpensePageState extends State { setState(() { expenses.remove(curr); }); + switch (direction) { + case DismissDirection.startToEnd: + // Only remove the item from the list. + break; + case DismissDirection.endToStart: + // Open an edit dialog, then remove the item from the list. + showDialog( + context: context, + builder: (_) => AlertDialog( + content: ExpenseInputDialog( + notifyParent: refresh, + expense: curr, + ), + ), + ); + break; + default: + UnimplementedError( + "Direction ${direction.toString()} not recognized."); + } }, child: Container( decoration: BoxDecoration( @@ -96,17 +137,6 @@ class _ExpensePageState extends State { ), ), ), - IconButton( - icon: Icon(Icons.edit_off), - onPressed: () { - // TODO: Open the item in the dialog with the NAME field disabled. - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar( - content: Text("Editing still TBD"), - ), - ); - }, - ), ], ), ), @@ -120,9 +150,12 @@ class _ExpensePageState extends State { class ExpenseInputDialog extends StatefulWidget { final Function() notifyParent; + final Expense? expense; + const ExpenseInputDialog({ super.key, required this.notifyParent, + this.expense, }); @override @@ -147,6 +180,12 @@ class _ExpenseInputDialogState extends State { alignment: FractionalOffset.topRight, child: IconButton( onPressed: () { + if (widget.expense != null) { + setState(() { + expenses.add(widget.expense!); + widget.notifyParent(); + }); + } Navigator.of(context).pop(); }, icon: Icon(Icons.clear), @@ -154,7 +193,9 @@ class _ExpenseInputDialogState extends State { ), AlertDialog( insetPadding: EdgeInsets.all(0), - title: Text("New Expense"), + title: widget.expense == null + ? Text("New Expense") + : Text("Edit Expense"), content: Form( key: _expenseFormKey, child: Column( @@ -168,6 +209,8 @@ class _ExpenseInputDialogState extends State { hintStyle: TextStyle(fontSize: 12.0), errorStyle: TextStyle(fontSize: 10.0), ), + initialValue: + widget.expense == null ? "" : widget.expense!.name, validator: (value) { if (value!.isEmpty) { return "Name must be provided."; @@ -189,6 +232,9 @@ class _ExpenseInputDialogState extends State { hintStyle: TextStyle(fontSize: 12.0), errorStyle: TextStyle(fontSize: 10.0), ), + initialValue: widget.expense == null + ? "" + : widget.expense!.cost.toString(), validator: (value) { if (value == null || value.isEmpty) { return "Cost must be provided."; @@ -230,7 +276,9 @@ class _ExpenseInputDialogState extends State { ), ) .toList(), - value: Frequency.montly, + value: widget.expense == null + ? Frequency.montly + : widget.expense!.frequency, decoration: InputDecoration( labelText: "Frequency", errorStyle: TextStyle(fontSize: 10.0), @@ -256,6 +304,8 @@ class _ExpenseInputDialogState extends State { hintStyle: TextStyle(fontSize: 12.0), errorStyle: TextStyle(fontSize: 10.0), ), + initialValue: + widget.expense == null ? "" : widget.expense!.description, validator: (value) { return null; },