More work to make the Expense page arbitrary for re-use.
This commit is contained in:
parent
bea9a4bc36
commit
737264fa2f
@ -3,6 +3,7 @@
|
|||||||
// SQLite
|
// SQLite
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'package:flutter_expense_tracker/models/recurring_tracked_type.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
@ -76,7 +77,7 @@ class DatabaseHelper {
|
|||||||
return expenseList;
|
return expenseList;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<int> addExpense(Expense expense) async {
|
Future<int> addExpense(RecurringTrackedType expense) async {
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
return await db.insert(
|
return await db.insert(
|
||||||
"expense",
|
"expense",
|
||||||
@ -93,7 +94,7 @@ class DatabaseHelper {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<int> updateExpense(Expense expense) async {
|
Future<int> updateExpense(RecurringTrackedType expense) async {
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
return await db.update(
|
return await db.update(
|
||||||
"expense",
|
"expense",
|
||||||
|
@ -11,6 +11,9 @@ abstract class TrackedType {
|
|||||||
required this.description,
|
required this.description,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static String amountText = "Amount";
|
||||||
|
String getAmountText() => amountText;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return toMap().toString();
|
return toMap().toString();
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
// Flutter
|
// Flutter
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_expense_tracker/db.dart';
|
import 'package:flutter_expense_tracker/db.dart';
|
||||||
|
import 'package:flutter_expense_tracker/models/recurring_tracked_type.dart';
|
||||||
|
import 'package:flutter_expense_tracker/models/tracked_type.dart';
|
||||||
|
|
||||||
// Local
|
// Local
|
||||||
import '/models/expense.dart';
|
import '/models/expense.dart';
|
||||||
@ -26,9 +28,10 @@ class _ExpensePageState extends State<ExpensePage> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
|
|
||||||
return FutureBuilder<List<Expense>>(
|
return FutureBuilder<List<RecurringTrackedType>>(
|
||||||
future: DatabaseHelper.instance.getExpenses(),
|
future: DatabaseHelper.instance.getExpenses(),
|
||||||
builder: (BuildContext context, AsyncSnapshot<List<Expense>> snapshot) {
|
builder: (BuildContext context,
|
||||||
|
AsyncSnapshot<List<RecurringTrackedType>> snapshot) {
|
||||||
if (!snapshot.hasData) {
|
if (!snapshot.hasData) {
|
||||||
return Text('Loading...');
|
return Text('Loading...');
|
||||||
}
|
}
|
||||||
@ -45,8 +48,8 @@ class _ExpensePageState extends State<ExpensePage> {
|
|||||||
: ListView.builder(
|
: ListView.builder(
|
||||||
itemCount: snapshot.data!.length,
|
itemCount: snapshot.data!.length,
|
||||||
itemBuilder: (_, index) {
|
itemBuilder: (_, index) {
|
||||||
List<Expense> expenses = snapshot.data!;
|
//List<Expense> expenses = snapshot.data!;
|
||||||
final Expense curr = expenses[index];
|
final RecurringTrackedType curr = snapshot.data![index];
|
||||||
final String estimateSymbolYearly = curr
|
final String estimateSymbolYearly = curr
|
||||||
.frequency.timesPerYear
|
.frequency.timesPerYear
|
||||||
.toStringAsFixed(2)
|
.toStringAsFixed(2)
|
||||||
@ -92,7 +95,7 @@ class _ExpensePageState extends State<ExpensePage> {
|
|||||||
),
|
),
|
||||||
onDismissed: (direction) {
|
onDismissed: (direction) {
|
||||||
setState(() {
|
setState(() {
|
||||||
expenses.remove(curr);
|
snapshot.data!.remove(curr);
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case DismissDirection.startToEnd:
|
case DismissDirection.startToEnd:
|
||||||
DatabaseHelper.instance.removeExpense(curr.id!);
|
DatabaseHelper.instance.removeExpense(curr.id!);
|
||||||
@ -102,9 +105,10 @@ class _ExpensePageState extends State<ExpensePage> {
|
|||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => AlertDialog(
|
builder: (_) => AlertDialog(
|
||||||
content: ExpenseInputDialog(
|
content: RecurringTrackedTypeInputDialog(
|
||||||
notifyParent: refresh,
|
notifyParent: refresh,
|
||||||
expense: curr,
|
entry: curr,
|
||||||
|
amountText: curr.getAmountText(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -179,21 +183,25 @@ class _ExpensePageState extends State<ExpensePage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExpenseInputDialog extends StatefulWidget {
|
class RecurringTrackedTypeInputDialog extends StatefulWidget {
|
||||||
final Function() notifyParent;
|
final Function() notifyParent;
|
||||||
final Expense? expense;
|
final RecurringTrackedType? entry;
|
||||||
|
final String? amountText;
|
||||||
|
|
||||||
const ExpenseInputDialog({
|
const RecurringTrackedTypeInputDialog({
|
||||||
super.key,
|
super.key,
|
||||||
required this.notifyParent,
|
required this.notifyParent,
|
||||||
this.expense,
|
this.entry,
|
||||||
|
this.amountText,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ExpenseInputDialog> createState() => _ExpenseInputDialogState();
|
State<RecurringTrackedTypeInputDialog> createState() =>
|
||||||
|
_RecurringTrackedTypeInputDialogState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
class _RecurringTrackedTypeInputDialogState
|
||||||
|
extends State<RecurringTrackedTypeInputDialog> {
|
||||||
final _expenseFormKey = GlobalKey<FormState>();
|
final _expenseFormKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
int? _id;
|
int? _id;
|
||||||
@ -204,14 +212,17 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (widget.expense != null) {
|
if (widget.entry != null) {
|
||||||
_id = widget.expense!.id;
|
_id = widget.entry!.id;
|
||||||
_name = widget.expense!.name;
|
_name = widget.entry!.name;
|
||||||
_amount = widget.expense!.amount;
|
_amount = widget.entry!.amount;
|
||||||
_freq = widget.expense!.frequency;
|
_freq = widget.entry!.frequency;
|
||||||
_desc = widget.expense!.description;
|
_desc = widget.entry!.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String amountText =
|
||||||
|
widget.amountText != null ? widget.amountText! : TrackedType.amountText;
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
// prevent AlertDialog from taking full vertical height.
|
// prevent AlertDialog from taking full vertical height.
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
@ -220,9 +231,9 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
alignment: FractionalOffset.topRight,
|
alignment: FractionalOffset.topRight,
|
||||||
child: IconButton(
|
child: IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (widget.expense != null) {
|
if (widget.entry != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
DatabaseHelper.instance.addExpense(widget.expense!);
|
DatabaseHelper.instance.addExpense(widget.entry!);
|
||||||
widget.notifyParent();
|
widget.notifyParent();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -234,7 +245,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
AlertDialog(
|
AlertDialog(
|
||||||
insetPadding: EdgeInsets.all(0),
|
insetPadding: EdgeInsets.all(0),
|
||||||
title: Center(
|
title: Center(
|
||||||
child: widget.expense == null
|
child: widget.entry == null
|
||||||
? Text("New Expense")
|
? Text("New Expense")
|
||||||
: Text("Edit Expense"),
|
: Text("Edit Expense"),
|
||||||
),
|
),
|
||||||
@ -279,7 +290,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
keyboardType:
|
keyboardType:
|
||||||
TextInputType.numberWithOptions(decimal: true),
|
TextInputType.numberWithOptions(decimal: true),
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "${Expense.amountText}",
|
labelText: amountText,
|
||||||
hintText: "Example: 10.00",
|
hintText: "Example: 10.00",
|
||||||
hintStyle: TextStyle(fontSize: 10.0),
|
hintStyle: TextStyle(fontSize: 10.0),
|
||||||
errorStyle: TextStyle(fontSize: 10.0),
|
errorStyle: TextStyle(fontSize: 10.0),
|
||||||
@ -287,16 +298,16 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
initialValue: _amount != 0 ? _amount.toString() : "",
|
initialValue: _amount != 0 ? _amount.toString() : "",
|
||||||
validator: (value) {
|
validator: (value) {
|
||||||
if (value == null || value.isEmpty) {
|
if (value == null || value.isEmpty) {
|
||||||
return "${Expense.amountText} must be provided.";
|
return "$amountText must be provided.";
|
||||||
}
|
}
|
||||||
if (double.tryParse(value) == null) {
|
if (double.tryParse(value) == null) {
|
||||||
return "${Expense.amountText} must be a valid number.";
|
return "$amountText must be a valid number.";
|
||||||
}
|
}
|
||||||
if (double.parse(value) < 0) {
|
if (double.parse(value) < 0) {
|
||||||
return "Please use the Income page rather than having negative expenses.";
|
return "Please use the Income page rather than having negative expenses.";
|
||||||
}
|
}
|
||||||
if (double.parse(value) < 0.01) {
|
if (double.parse(value) < 0.01) {
|
||||||
return "${Expense.amountText} must be one hundreth (0.01) or higher.";
|
return "$amountText must be one hundreth (0.01) or higher.";
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
@ -33,7 +33,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
switch (pageSelected) {
|
switch (pageSelected) {
|
||||||
case 0:
|
case 0:
|
||||||
page = ExpensePage();
|
page = ExpensePage();
|
||||||
dialog = ExpenseInputDialog(
|
dialog = RecurringTrackedTypeInputDialog(
|
||||||
notifyParent: refresh,
|
notifyParent: refresh,
|
||||||
);
|
);
|
||||||
case 1:
|
case 1:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user