Add unique name check to entry form.

This commit is contained in:
Hyperling 2025-02-07 15:40:07 -07:00
parent ef58a06dfa
commit f5635d6120

View File

@ -1,6 +1,4 @@
// Flutter // Flutter
import 'dart:nativewrappers/_internal/vm/lib/ffi_allocation_patch.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_expense_tracker/db.dart'; import 'package:flutter_expense_tracker/db.dart';
@ -236,125 +234,134 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
? Text("New Expense") ? Text("New Expense")
: Text("Edit Expense"), : Text("Edit Expense"),
), ),
content: Form( content: FutureBuilder<List<Expense>>(
key: _expenseFormKey, future: DatabaseHelper.instance.getExpenses(),
child: Column( builder: (BuildContext context,
mainAxisSize: MainAxisSize.min, AsyncSnapshot<List<Expense>> snapshot) {
children: [ if (!snapshot.hasData) {
TextFormField( return Center(child: Text('Loading...'));
keyboardType: TextInputType.text, }
textCapitalization: TextCapitalization.words, List<Expense> expenses = snapshot.data!;
decoration: InputDecoration( return Form(
labelText: "Name", key: _expenseFormKey,
hintText: "Example: Red Pocket", child: Column(
hintStyle: TextStyle(fontSize: 10.0), mainAxisSize: MainAxisSize.min,
errorStyle: TextStyle(fontSize: 10.0), children: [
), TextFormField(
initialValue: _name, keyboardType: TextInputType.text,
validator: (value) { textCapitalization: TextCapitalization.words,
if (value!.isEmpty) { decoration: InputDecoration(
return "Name must be provided."; labelText: "Name",
} hintText: "Example: Red Pocket",
// TODO: Figure out how to check the DB if a name already exists. hintStyle: TextStyle(fontSize: 10.0),
/* errorStyle: TextStyle(fontSize: 10.0),
if (DatabaseHelper.instance.checkExpenseNameExists(value)) { ),
return "Name must be unique, already in use."; initialValue: _name,
} validator: (value) {
*/ if (value!.isEmpty) {
return null; return "Name must be provided.";
}, }
onSaved: (value) { if (!expenses
_name = value!; .every((expense) => expense.name != value)) {
}, return "Name must be unique, already in use.";
), }
TextFormField( return null;
keyboardType: TextInputType.numberWithOptions(decimal: true), },
decoration: InputDecoration( onSaved: (value) {
labelText: "Cost", _name = value!;
hintText: "Example: 10.00", },
hintStyle: TextStyle(fontSize: 10.0), ),
errorStyle: TextStyle(fontSize: 10.0), TextFormField(
), keyboardType:
initialValue: _cost != 0 ? _cost.toString() : "", TextInputType.numberWithOptions(decimal: true),
validator: (value) { decoration: InputDecoration(
if (value == null || value.isEmpty) { labelText: "Cost",
return "Cost must be provided."; hintText: "Example: 10.00",
} hintStyle: TextStyle(fontSize: 10.0),
if (double.parse(value) < 0) { errorStyle: TextStyle(fontSize: 10.0),
return "Please use the Income page rather than having negative expenses."; ),
} initialValue: _cost != 0 ? _cost.toString() : "",
if (double.parse(value) < 0.01) { validator: (value) {
return "Cost must be one hundreth (0.01) or higher."; if (value == null || value.isEmpty) {
} return "Cost must be provided.";
if (double.tryParse(value) == null) { }
return "Cost must be a valid number."; if (double.parse(value) < 0) {
} return "Please use the Income page rather than having negative expenses.";
return null; }
}, if (double.parse(value) < 0.01) {
onSaved: (value) { return "Cost must be one hundreth (0.01) or higher.";
_cost = double.parse(value!); }
}, if (double.tryParse(value) == null) {
), return "Cost must be a valid number.";
DropdownButtonFormField( }
items: Frequency.values return null;
.map( },
(freq) => DropdownMenuItem( onSaved: (value) {
value: freq, _cost = double.parse(value!);
child: Row( },
children: [ ),
Text( DropdownButtonFormField(
freq.title, items: Frequency.values
), .map(
Padding( (freq) => DropdownMenuItem(
padding: EdgeInsets.all(1.0), value: freq,
child: Text( child: Row(
" (${freq.hint})", children: [
style: TextStyle(fontSize: 10.0), Text(
freq.title,
),
Padding(
padding: EdgeInsets.all(1.0),
child: Text(
" (${freq.hint})",
style: TextStyle(fontSize: 10.0),
),
),
],
), ),
), ),
], )
), .toList(),
value: _freq,
decoration: InputDecoration(
labelText: "Frequency",
errorStyle: TextStyle(fontSize: 10.0),
), ),
) validator: (value) {
.toList(), if (value == null) {
value: _freq, return "Frequency must be provided.";
decoration: InputDecoration( }
labelText: "Frequency", if (!Frequency.values.contains(value)) {
errorStyle: TextStyle(fontSize: 10.0), return "Value not valid.";
}
return null;
},
onChanged: (value) {
_freq = value!;
},
),
TextFormField(
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.sentences,
decoration: InputDecoration(
labelText: "Description",
hintText:
"Example: 1GB data with unlimited talk & text.",
hintStyle: TextStyle(fontSize: 8.0),
errorStyle: TextStyle(fontSize: 10.0),
),
initialValue: _desc,
validator: (value) {
return null;
},
onSaved: (value) {
_desc = value!;
},
),
],
), ),
validator: (value) { );
if (value == null) { }),
return "Frequency must be provided.";
}
if (!Frequency.values.contains(value)) {
return "Value not valid.";
}
return null;
},
onChanged: (value) {
_freq = value!;
},
),
TextFormField(
keyboardType: TextInputType.text,
textCapitalization: TextCapitalization.sentences,
decoration: InputDecoration(
labelText: "Description",
hintText: "Example: 1GB data with unlimited talk & text.",
hintStyle: TextStyle(fontSize: 8.0),
errorStyle: TextStyle(fontSize: 10.0),
),
initialValue: _desc,
validator: (value) {
return null;
},
onSaved: (value) {
_desc = value!;
},
),
],
),
),
actions: [ actions: [
Center( Center(
child: ElevatedButton.icon( child: ElevatedButton.icon(