Added editing an expense.

This commit is contained in:
Hyperling 2025-02-07 04:29:10 -07:00
parent 9d8a5e6685
commit 595aaefedc
2 changed files with 71 additions and 19 deletions

View File

@ -70,7 +70,9 @@ class _HomePageState extends State<HomePage> {
Future<void> addNewValue(BuildContext context) { Future<void> addNewValue(BuildContext context) {
return showDialog( return showDialog(
context: context, builder: (_) => AlertDialog(content: dialog)); context: context,
builder: (_) => AlertDialog(content: dialog),
);
} }
Widget? floatingButton; Widget? floatingButton;

View File

@ -17,6 +17,10 @@ class ExpensePage extends StatefulWidget {
} }
class _ExpensePageState extends State<ExpensePage> { class _ExpensePageState extends State<ExpensePage> {
refresh() {
setState(() {});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
expenses.sort( expenses.sort(
@ -39,14 +43,31 @@ class _ExpensePageState extends State<ExpensePage> {
child: Dismissible( child: Dismissible(
key: Key(curr.toString()), key: Key(curr.toString()),
background: Container( background: Container(
color: Colors.blue,
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Container(
color: Colors.red, color: Colors.red,
child: Row( child: Row(
mainAxisSize: MainAxisSize.max,
children: [ children: [
Icon(Icons.delete), Icon(Icons.delete),
Text("Delete!"), Text("Delete"),
],
),
),
Spacer(), Spacer(),
Text("Delete!"), Container(
Icon(Icons.delete), color: Colors.yellow,
child: Row(
mainAxisSize: MainAxisSize.max,
children: [
Text("Edit"),
Icon(Icons.edit),
],
),
),
], ],
), ),
), ),
@ -54,6 +75,26 @@ class _ExpensePageState extends State<ExpensePage> {
setState(() { setState(() {
expenses.remove(curr); 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( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
@ -96,17 +137,6 @@ class _ExpensePageState extends State<ExpensePage> {
), ),
), ),
), ),
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<ExpensePage> {
class ExpenseInputDialog extends StatefulWidget { class ExpenseInputDialog extends StatefulWidget {
final Function() notifyParent; final Function() notifyParent;
final Expense? expense;
const ExpenseInputDialog({ const ExpenseInputDialog({
super.key, super.key,
required this.notifyParent, required this.notifyParent,
this.expense,
}); });
@override @override
@ -147,6 +180,12 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
alignment: FractionalOffset.topRight, alignment: FractionalOffset.topRight,
child: IconButton( child: IconButton(
onPressed: () { onPressed: () {
if (widget.expense != null) {
setState(() {
expenses.add(widget.expense!);
widget.notifyParent();
});
}
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
icon: Icon(Icons.clear), icon: Icon(Icons.clear),
@ -154,7 +193,9 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
), ),
AlertDialog( AlertDialog(
insetPadding: EdgeInsets.all(0), insetPadding: EdgeInsets.all(0),
title: Text("New Expense"), title: widget.expense == null
? Text("New Expense")
: Text("Edit Expense"),
content: Form( content: Form(
key: _expenseFormKey, key: _expenseFormKey,
child: Column( child: Column(
@ -168,6 +209,8 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
hintStyle: TextStyle(fontSize: 12.0), hintStyle: TextStyle(fontSize: 12.0),
errorStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0),
), ),
initialValue:
widget.expense == null ? "" : widget.expense!.name,
validator: (value) { validator: (value) {
if (value!.isEmpty) { if (value!.isEmpty) {
return "Name must be provided."; return "Name must be provided.";
@ -189,6 +232,9 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
hintStyle: TextStyle(fontSize: 12.0), hintStyle: TextStyle(fontSize: 12.0),
errorStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0),
), ),
initialValue: widget.expense == null
? ""
: widget.expense!.cost.toString(),
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return "Cost must be provided."; return "Cost must be provided.";
@ -230,7 +276,9 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
), ),
) )
.toList(), .toList(),
value: Frequency.montly, value: widget.expense == null
? Frequency.montly
: widget.expense!.frequency,
decoration: InputDecoration( decoration: InputDecoration(
labelText: "Frequency", labelText: "Frequency",
errorStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0),
@ -256,6 +304,8 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
hintStyle: TextStyle(fontSize: 12.0), hintStyle: TextStyle(fontSize: 12.0),
errorStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0),
), ),
initialValue:
widget.expense == null ? "" : widget.expense!.description,
validator: (value) { validator: (value) {
return null; return null;
}, },