Forms is working, items show if hot reloaded, need to refactor code and get the global variable into the state of the expense page.
This commit is contained in:
parent
99b1ec82e6
commit
1b95feb5d4
123
lib/main.dart
123
lib/main.dart
@ -8,16 +8,36 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://www.tutorialspoint.com/dart_programming/dart_programming_enumeration.htm
|
// https://www.tutorialspoint.com/dart_programming/dart_programming_enumeration.htm
|
||||||
enum Recurrence { daily, weekly, biweekly, montly, yearly }
|
enum Frequency {
|
||||||
|
daily(title: "Daily"),
|
||||||
|
weekly(title: "Weekly"),
|
||||||
|
biweekly(title: "Biweekly"),
|
||||||
|
montly(title: "Monthly"),
|
||||||
|
yearly(title: "Yearly");
|
||||||
|
|
||||||
|
const Frequency({required this.title});
|
||||||
|
|
||||||
|
final String title;
|
||||||
|
}
|
||||||
|
|
||||||
class Expense {
|
class Expense {
|
||||||
String name;
|
final String name;
|
||||||
double cost;
|
final double cost;
|
||||||
Recurrence recurrence;
|
final Frequency frequency;
|
||||||
String description;
|
final String description;
|
||||||
|
|
||||||
Expense(this.name, this.cost, this.recurrence, this.description);
|
const Expense(
|
||||||
|
{required this.name,
|
||||||
|
required this.cost,
|
||||||
|
required this.frequency,
|
||||||
|
required this.description});
|
||||||
|
|
||||||
|
String toString() {
|
||||||
|
return "$name, $cost, ${frequency.title}, $description";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Expense> expenses = [];
|
||||||
|
|
||||||
class MainApp extends StatelessWidget {
|
class MainApp extends StatelessWidget {
|
||||||
const MainApp({super.key});
|
const MainApp({super.key});
|
||||||
@ -85,6 +105,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
|
|
||||||
return LayoutBuilder(builder: (context, constraints) {
|
return LayoutBuilder(builder: (context, constraints) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
// TODO: Add a drawer instead of nav rail.
|
||||||
body: Row(
|
body: Row(
|
||||||
children: [
|
children: [
|
||||||
SafeArea(
|
SafeArea(
|
||||||
@ -141,6 +162,29 @@ class ExpensePage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: expenses.length,
|
||||||
|
itemBuilder: (_, index) {
|
||||||
|
final Expense curr = expenses[index];
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(4.0),
|
||||||
|
child: Container(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius: BorderRadius.circular(4),
|
||||||
|
color: Colors.greenAccent,
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
Text(curr.name),
|
||||||
|
Text("${curr.cost.toString()} ${curr.frequency.title}"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
/*
|
||||||
return ListView(
|
return ListView(
|
||||||
children: [
|
children: [
|
||||||
ListTile(
|
ListTile(
|
||||||
@ -153,6 +197,7 @@ class ExpensePage extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,22 +213,21 @@ class ExpenseInputDialog extends StatefulWidget {
|
|||||||
class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||||
final _expenseFormKey = GlobalKey<FormState>();
|
final _expenseFormKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
|
String _name = "";
|
||||||
|
double _cost = 0.0;
|
||||||
|
Frequency _freq = Frequency.montly;
|
||||||
|
String _desc = "";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
const inputWidth = 300.0;
|
const inputWidth = 300.0;
|
||||||
const inputHeight = 50.0;
|
const inputHeight = 50.0;
|
||||||
|
|
||||||
const recurrenceValues = <DropdownMenuItem>[
|
List<DropdownMenuItem> freqValues = [];
|
||||||
DropdownMenuItem(value: Recurrence.daily, child: Text("Daily")),
|
for (var freq in Frequency.values) {
|
||||||
DropdownMenuItem(value: Recurrence.weekly, child: Text("Weekly")),
|
freqValues.add(DropdownMenuItem(value: freq, child: Text(freq.title)));
|
||||||
DropdownMenuItem(value: Recurrence.biweekly, child: Text("Biweekly")),
|
}
|
||||||
DropdownMenuItem(value: Recurrence.montly, child: Text("Monthly")),
|
;
|
||||||
DropdownMenuItem(
|
|
||||||
value: Recurrence.yearly,
|
|
||||||
child: Text(
|
|
||||||
"Yearly",
|
|
||||||
)),
|
|
||||||
];
|
|
||||||
|
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: Center(child: Text("Add New Expense")),
|
title: Center(child: Text("Add New Expense")),
|
||||||
@ -206,10 +250,11 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onChanged: (newValue) {
|
onSaved: (newValue) {
|
||||||
// _expenseFormKey.currentState!.reset();
|
_name = newValue!;
|
||||||
},
|
},
|
||||||
)),
|
),
|
||||||
|
),
|
||||||
SizedBox(
|
SizedBox(
|
||||||
width: inputWidth,
|
width: inputWidth,
|
||||||
height: inputHeight,
|
height: inputHeight,
|
||||||
@ -221,13 +266,13 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
if (value!.isEmpty) {
|
if (value!.isEmpty) {
|
||||||
return "Cost must be provided.";
|
return "Cost must be provided.";
|
||||||
}
|
}
|
||||||
if (double.tryParse(value!) == null) {
|
if (double.tryParse(value) == null) {
|
||||||
return "Cost must be a valid number.";
|
return "Cost must be a valid number.";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onChanged: (newValue) {
|
onSaved: (newValue) {
|
||||||
//_expenseFormKey.currentState!.reset();
|
_cost = double.parse(newValue!);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -235,17 +280,20 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
width: inputWidth,
|
width: inputWidth,
|
||||||
height: inputHeight,
|
height: inputHeight,
|
||||||
child: DropdownButtonFormField(
|
child: DropdownButtonFormField(
|
||||||
items: recurrenceValues,
|
items: freqValues,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "Recurrence", hintText: "Example: Monthly"),
|
labelText: "Recurrence", hintText: "Example: Monthly"),
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return "Recurrence must be provided.";
|
return "Frequency must be provided.";
|
||||||
|
}
|
||||||
|
if (!Frequency.values.contains(value)) {
|
||||||
|
return "Value not valid.";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onChanged: (newValue) {
|
onChanged: (newValue) {
|
||||||
//_expenseFormKey.currentState!.reset();
|
_freq = newValue;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -260,10 +308,11 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
validator: (value) {
|
validator: (value) {
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onChanged: (newValue) {
|
onSaved: (newValue) {
|
||||||
//_expenseFormKey.currentState!.reset();
|
_desc = newValue!;
|
||||||
},
|
},
|
||||||
)),
|
),
|
||||||
|
),
|
||||||
]),
|
]),
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
@ -287,6 +336,19 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
print("TODO: Save expense!");
|
print("TODO: Save expense!");
|
||||||
if (_expenseFormKey.currentState!.validate()) {
|
if (_expenseFormKey.currentState!.validate()) {
|
||||||
_expenseFormKey.currentState!.save();
|
_expenseFormKey.currentState!.save();
|
||||||
|
setState(() {
|
||||||
|
expenses.add(
|
||||||
|
Expense(
|
||||||
|
name: _name,
|
||||||
|
cost: _cost,
|
||||||
|
frequency: _freq,
|
||||||
|
description: _desc),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
print(expenses.toString());
|
||||||
|
for (var expense in expenses) {
|
||||||
|
print(expense.toString());
|
||||||
|
}
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -294,7 +356,8 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
label: Text('Submit'),
|
label: Text('Submit'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
))
|
),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user