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

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

@ -17,6 +17,10 @@ class ExpensePage extends StatefulWidget {
}
class _ExpensePageState extends State<ExpensePage> {
refresh() {
setState(() {});
}
@override
Widget build(BuildContext context) {
expenses.sort(
@ -39,14 +43,31 @@ class _ExpensePageState extends State<ExpensePage> {
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<ExpensePage> {
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<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 {
final Function() notifyParent;
final Expense? expense;
const ExpenseInputDialog({
super.key,
required this.notifyParent,
this.expense,
});
@override
@ -147,6 +180,12 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
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<ExpenseInputDialog> {
),
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<ExpenseInputDialog> {
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<ExpenseInputDialog> {
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<ExpenseInputDialog> {
),
)
.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<ExpenseInputDialog> {
hintStyle: TextStyle(fontSize: 12.0),
errorStyle: TextStyle(fontSize: 10.0),
),
initialValue:
widget.expense == null ? "" : widget.expense!.description,
validator: (value) {
return null;
},