// Flutter import 'package:flutter/material.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 import '/models/expense.dart'; import '/models/frequency.dart'; // TODO: Make this a generic UI based on a superclass of Expense, Income, and Assets. class ExpensePage extends StatefulWidget { const ExpensePage({ super.key, }); @override State createState() => _ExpensePageState(); } class _ExpensePageState extends State { refresh() { setState(() {}); } @override Widget build(BuildContext context) { final theme = Theme.of(context); return FutureBuilder>( future: DatabaseHelper.instance.getExpenses(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (!snapshot.hasData) { return Text('Loading...'); } snapshot.data!.sort( (a, b) => (b.calcComparableAmountYearly() - a.calcComparableAmountYearly()) .toInt(), ); return snapshot.data!.isEmpty ? Text( "Add expenses to get started.", softWrap: true, ) : ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (_, index) { //List expenses = snapshot.data!; final RecurringTrackedType curr = snapshot.data![index]; final String estimateSymbolYearly = curr .frequency.timesPerYear .toStringAsFixed(2) .endsWith(".00") && curr .calcComparableAmountYearly() .toStringAsFixed(3) .endsWith("0") ? "" : "~"; final String estimateSymbolDaily = curr.frequency.numDays .toStringAsFixed(2) .endsWith(".00") && curr .calcComparableAmountDaily() .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(() { snapshot.data!.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: RecurringTrackedTypeInputDialog( notifyParent: refresh, entry: curr, amountText: curr.getAmountText(), ), ), ); 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.amount.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.calcComparableAmountDaily().toStringAsFixed(2)} ${Frequency.daily.title}", style: TextStyle(fontSize: 12.0), ), //if (curr.frequency != Frequency.yearly) Text( "$estimateSymbolYearly${curr.calcComparableAmountYearly().toStringAsFixed(2)} ${Frequency.yearly.title}", style: TextStyle(fontSize: 12.0), ), ], ), ], ), ), ), ), ); }, ); }); } } class RecurringTrackedTypeInputDialog extends StatefulWidget { final Function() notifyParent; final RecurringTrackedType? entry; final String? amountText; const RecurringTrackedTypeInputDialog({ super.key, required this.notifyParent, this.entry, this.amountText, }); @override State createState() => _RecurringTrackedTypeInputDialogState(); } class _RecurringTrackedTypeInputDialogState extends State { final _expenseFormKey = GlobalKey(); int? _id; String _name = ""; double _amount = 0; Frequency _freq = Frequency.monthly; String _desc = ""; @override Widget build(BuildContext context) { if (widget.entry != null) { _id = widget.entry!.id; _name = widget.entry!.name; _amount = widget.entry!.amount; _freq = widget.entry!.frequency; _desc = widget.entry!.description; } String amountText = widget.amountText != null ? widget.amountText! : TrackedType.amountText; return Column( // prevent AlertDialog from taking full vertical height. mainAxisSize: MainAxisSize.min, children: [ Container( alignment: FractionalOffset.topRight, child: IconButton( onPressed: () { if (widget.entry != null) { setState(() { DatabaseHelper.instance.addExpense(widget.entry!); widget.notifyParent(); }); } Navigator.of(context).pop(); }, icon: Icon(Icons.clear), ), ), AlertDialog( insetPadding: EdgeInsets.all(0), title: Center( child: widget.entry == null ? Text("New Expense") : Text("Edit Expense"), ), content: FutureBuilder>( future: DatabaseHelper.instance.getExpenses(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (!snapshot.hasData) { return Center(child: Text('Loading...')); } List expenses = snapshot.data!; return Form( key: _expenseFormKey, child: Column( mainAxisSize: MainAxisSize.min, children: [ TextFormField( keyboardType: TextInputType.text, textCapitalization: TextCapitalization.words, decoration: InputDecoration( labelText: "Name", hintText: "Example: Red Pocket", hintStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0), ), initialValue: _name, validator: (value) { if (value!.isEmpty) { return "Name must be provided."; } if (!expenses.every((expense) => expense.name != value || expense.id == _id)) { return "Name must be unique, already in use."; } return null; }, onSaved: (value) { _name = value!; }, ), TextFormField( keyboardType: TextInputType.numberWithOptions(decimal: true), decoration: InputDecoration( labelText: amountText, hintText: "Example: 10.00", hintStyle: TextStyle(fontSize: 10.0), errorStyle: TextStyle(fontSize: 10.0), ), initialValue: _amount != 0 ? _amount.toString() : "", validator: (value) { if (value == null || value.isEmpty) { return "$amountText must be provided."; } if (double.tryParse(value) == null) { return "$amountText must be a valid number."; } if (double.parse(value) < 0) { return "Please use the Income page rather than having negative expenses."; } if (double.parse(value) < 0.01) { return "$amountText must be one hundreth (0.01) or higher."; } return null; }, onSaved: (value) { _amount = double.parse(value!); }, ), DropdownButtonFormField( items: Frequency.values .map( (freq) => DropdownMenuItem( value: freq, child: Row( children: [ 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) { 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: [ Center( child: ElevatedButton.icon( onPressed: () { if (_expenseFormKey.currentState!.validate()) { _expenseFormKey.currentState!.save(); setState(() { Expense expense = Expense( id: _id, name: _name, amount: _amount, frequency: _freq, description: _desc, ); if (_id != null) { DatabaseHelper.instance.updateExpense( expense, ); } else { DatabaseHelper.instance.addExpense( expense, ); } }); widget.notifyParent(); Navigator.of(context).pop(); } }, icon: Icon(Icons.save), label: Text('Submit'), ), ) ], ), ], ); } }