Successfully migrate the application to SQLIte!
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
// Flutter
|
||||
import 'dart:nativewrappers/_internal/vm/lib/ffi_allocation_patch.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_expense_tracker/db.dart';
|
||||
|
||||
// Local
|
||||
import '/models/expense.dart';
|
||||
import '/models/frequency.dart';
|
||||
|
||||
List<Expense> expenses = [];
|
||||
|
||||
class ExpensePage extends StatefulWidget {
|
||||
const ExpensePage({
|
||||
super.key,
|
||||
@ -25,139 +26,154 @@ class _ExpensePageState extends State<ExpensePage> {
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
expenses.sort(
|
||||
(a, b) => (b.calcComparableCost() - a.calcComparableCost()).toInt(),
|
||||
);
|
||||
|
||||
return expenses.isEmpty
|
||||
? Text("Add expenses to get started!")
|
||||
: ListView.builder(
|
||||
itemCount: expenses.length,
|
||||
itemBuilder: (_, index) {
|
||||
final Expense curr = expenses[index];
|
||||
final String estimateSymbolYearly = curr.frequency.timesPerYear
|
||||
.toStringAsFixed(2)
|
||||
.endsWith(".00") &&
|
||||
curr.calcComparableCost().toStringAsFixed(3).endsWith("0")
|
||||
? ""
|
||||
: "~";
|
||||
final String estimateSymbolDaily =
|
||||
curr.frequency.numDays.toStringAsFixed(2).endsWith(".00") &&
|
||||
curr
|
||||
.calcComparableCostDaily()
|
||||
.toStringAsFixed(3)
|
||||
.endsWith("0")
|
||||
? ""
|
||||
: "~";
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Dismissible(
|
||||
key: Key(curr.toString()),
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.delete),
|
||||
Text("Delete"),
|
||||
],
|
||||
),
|
||||
),
|
||||
secondaryBackground: Container(
|
||||
color: Colors.orange,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Text("Edit"),
|
||||
Icon(Icons.edit),
|
||||
],
|
||||
),
|
||||
),
|
||||
onDismissed: (direction) {
|
||||
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(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
color: theme.colorScheme.onPrimary,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
curr.name,
|
||||
style: TextStyle(fontSize: 20.0),
|
||||
),
|
||||
Text(
|
||||
"${curr.cost.toStringAsFixed(2)} ${curr.frequency.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: Text(
|
||||
curr.description,
|
||||
style: TextStyle(
|
||||
fontSize: 12.0,
|
||||
),
|
||||
softWrap: true,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
//if (curr.frequency != Frequency.daily)
|
||||
Text(
|
||||
"$estimateSymbolDaily${curr.calcComparableCostDaily().toStringAsFixed(2)} ${Frequency.daily.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
//if (curr.frequency != Frequency.yearly)
|
||||
Text(
|
||||
"$estimateSymbolYearly${curr.calcComparableCost().toStringAsFixed(2)} ${Frequency.yearly.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
return FutureBuilder<List<Expense>>(
|
||||
future: DatabaseHelper.instance.getExpenses(),
|
||||
builder: (BuildContext context, AsyncSnapshot<List<Expense>> snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return Center(child: Text('Loading...'));
|
||||
}
|
||||
snapshot.data!.sort(
|
||||
(a, b) => (b.calcComparableCost() - a.calcComparableCost()).toInt(),
|
||||
);
|
||||
return snapshot.data!.isEmpty
|
||||
? Text(
|
||||
"Add expenses to get started.",
|
||||
softWrap: true,
|
||||
)
|
||||
: ListView.builder(
|
||||
itemCount: snapshot.data!.length,
|
||||
itemBuilder: (_, index) {
|
||||
List<Expense> expenses = snapshot.data!;
|
||||
final Expense curr = expenses[index];
|
||||
final String estimateSymbolYearly = curr
|
||||
.frequency.timesPerYear
|
||||
.toStringAsFixed(2)
|
||||
.endsWith(".00") &&
|
||||
curr
|
||||
.calcComparableCost()
|
||||
.toStringAsFixed(3)
|
||||
.endsWith("0")
|
||||
? ""
|
||||
: "~";
|
||||
final String estimateSymbolDaily = curr.frequency.numDays
|
||||
.toStringAsFixed(2)
|
||||
.endsWith(".00") &&
|
||||
curr
|
||||
.calcComparableCostDaily()
|
||||
.toStringAsFixed(3)
|
||||
.endsWith("0")
|
||||
? ""
|
||||
: "~";
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Dismissible(
|
||||
key: Key(curr.id!.toString()),
|
||||
background: Container(
|
||||
color: Colors.red,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: [
|
||||
Icon(Icons.delete),
|
||||
Text("Delete"),
|
||||
],
|
||||
),
|
||||
),
|
||||
secondaryBackground: Container(
|
||||
color: Colors.orange,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Text("Edit"),
|
||||
Icon(Icons.edit),
|
||||
],
|
||||
),
|
||||
),
|
||||
onDismissed: (direction) {
|
||||
setState(() {
|
||||
expenses.remove(curr);
|
||||
switch (direction) {
|
||||
case DismissDirection.startToEnd:
|
||||
DatabaseHelper.instance.removeExpense(curr.id!);
|
||||
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(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
color: theme.colorScheme.onPrimary,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: [
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
curr.name,
|
||||
style: TextStyle(fontSize: 20.0),
|
||||
),
|
||||
Text(
|
||||
"${curr.cost.toStringAsFixed(2)} ${curr.frequency.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: Text(
|
||||
curr.description,
|
||||
style: TextStyle(
|
||||
fontSize: 12.0,
|
||||
),
|
||||
softWrap: true,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
//if (curr.frequency != Frequency.daily)
|
||||
Text(
|
||||
"$estimateSymbolDaily${curr.calcComparableCostDaily().toStringAsFixed(2)} ${Frequency.daily.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
//if (curr.frequency != Frequency.yearly)
|
||||
Text(
|
||||
"$estimateSymbolYearly${curr.calcComparableCost().toStringAsFixed(2)} ${Frequency.yearly.title}",
|
||||
style: TextStyle(fontSize: 12.0),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -178,6 +194,7 @@ class ExpenseInputDialog extends StatefulWidget {
|
||||
class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||
final _expenseFormKey = GlobalKey<FormState>();
|
||||
|
||||
int? _id;
|
||||
String _name = "";
|
||||
double _cost = 0;
|
||||
Frequency _freq = Frequency.monthly;
|
||||
@ -186,6 +203,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.expense != null) {
|
||||
_id = widget.expense!.id;
|
||||
_name = widget.expense!.name;
|
||||
_cost = widget.expense!.cost;
|
||||
_freq = widget.expense!.frequency;
|
||||
@ -202,7 +220,7 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||
onPressed: () {
|
||||
if (widget.expense != null) {
|
||||
setState(() {
|
||||
expenses.add(widget.expense!);
|
||||
DatabaseHelper.instance.addExpense(widget.expense!);
|
||||
widget.notifyParent();
|
||||
});
|
||||
}
|
||||
@ -237,9 +255,12 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||
if (value!.isEmpty) {
|
||||
return "Name must be provided.";
|
||||
}
|
||||
if (!expenses.every((expense) => expense.name != value)) {
|
||||
// TODO: Figure out how to check the DB if a name already exists.
|
||||
/*
|
||||
if (DatabaseHelper.instance.checkExpenseNameExists(value)) {
|
||||
return "Name must be unique, already in use.";
|
||||
}
|
||||
*/
|
||||
return null;
|
||||
},
|
||||
onSaved: (value) {
|
||||
@ -341,13 +362,22 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
||||
if (_expenseFormKey.currentState!.validate()) {
|
||||
_expenseFormKey.currentState!.save();
|
||||
setState(() {
|
||||
expenses.add(
|
||||
Expense(
|
||||
name: _name,
|
||||
cost: _cost,
|
||||
frequency: _freq,
|
||||
description: _desc),
|
||||
Expense expense = Expense(
|
||||
id: _id,
|
||||
name: _name,
|
||||
cost: _cost,
|
||||
frequency: _freq,
|
||||
description: _desc,
|
||||
);
|
||||
if (_id != null) {
|
||||
DatabaseHelper.instance.updateExpense(
|
||||
expense,
|
||||
);
|
||||
} else {
|
||||
DatabaseHelper.instance.addExpense(
|
||||
expense,
|
||||
);
|
||||
}
|
||||
});
|
||||
widget.notifyParent();
|
||||
Navigator.of(context).pop();
|
||||
|
119
lib/pages/home.dart
Normal file
119
lib/pages/home.dart
Normal file
@ -0,0 +1,119 @@
|
||||
// Flutter
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Local
|
||||
import '/pages/expense.dart';
|
||||
import '/pages/income.dart';
|
||||
import '/pages/asset.dart';
|
||||
import '/pages/report.dart';
|
||||
import '/pages/settings.dart';
|
||||
import '/pages/help.dart';
|
||||
|
||||
class HomePage extends StatefulWidget {
|
||||
const HomePage({
|
||||
super.key,
|
||||
});
|
||||
|
||||
@override
|
||||
State<HomePage> createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
var pageSelected = 0;
|
||||
|
||||
refresh() {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget page;
|
||||
Widget? dialog;
|
||||
switch (pageSelected) {
|
||||
case 0:
|
||||
page = ExpensePage();
|
||||
dialog = ExpenseInputDialog(
|
||||
notifyParent: refresh,
|
||||
);
|
||||
case 1:
|
||||
page = IncomePage();
|
||||
case 2:
|
||||
page = AssetPage();
|
||||
case 3:
|
||||
page = ProjectionPage();
|
||||
case 4:
|
||||
page = SettingsPage();
|
||||
case 5:
|
||||
page = HelpPage();
|
||||
default:
|
||||
throw UnimplementedError('No widget for page $pageSelected yet!');
|
||||
}
|
||||
|
||||
Future<void> addNewValue(BuildContext context) {
|
||||
return showDialog(
|
||||
context: context,
|
||||
builder: (_) => AlertDialog(content: dialog),
|
||||
);
|
||||
}
|
||||
|
||||
Widget? floatingButton;
|
||||
if (dialog != null) {
|
||||
floatingButton = IconButton(
|
||||
onPressed: () {
|
||||
addNewValue(context);
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
);
|
||||
}
|
||||
|
||||
return LayoutBuilder(builder: (context, constraints) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("Expense Tracker"),
|
||||
),
|
||||
drawer: NavigationRail(
|
||||
extended: true,
|
||||
destinations: [
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.payment),
|
||||
label: Text('Expenses'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.account_balance),
|
||||
label: Text('Income'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.attach_money),
|
||||
label: Text('Liquid Assets'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.bar_chart),
|
||||
label: Text('Reports'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.settings),
|
||||
label: Text('Settings'),
|
||||
),
|
||||
NavigationRailDestination(
|
||||
icon: Icon(Icons.help),
|
||||
label: Text('Help'),
|
||||
),
|
||||
],
|
||||
selectedIndex: pageSelected,
|
||||
onDestinationSelected: (value) {
|
||||
setState(() {
|
||||
pageSelected = value;
|
||||
Navigator.pop(context);
|
||||
});
|
||||
},
|
||||
),
|
||||
body: Container(
|
||||
color: Theme.of(context).colorScheme.primaryContainer,
|
||||
child: Center(child: page),
|
||||
),
|
||||
floatingActionButton: floatingButton,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user