okane/lib/ui/pages/loans/add_loan_change.dart

103 lines
2.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:okane/database/collections/loan.dart';
import 'package:okane/database/database.dart';
import 'package:okane/i18n/strings.g.dart';
import 'package:okane/ui/utils.dart';
enum LoanChangeType { owe, loan }
class AddLoanChangePopup extends StatefulWidget {
final VoidCallback onDone;
final Loan loan;
const AddLoanChangePopup({
super.key,
required this.onDone,
required this.loan,
});
@override
AddLoanPopupState createState() => AddLoanPopupState();
}
class AddLoanPopupState extends State<AddLoanChangePopup> {
LoanChangeType _loanChangeType = LoanChangeType.loan;
final TextEditingController _amountController = TextEditingController(
text: "0.00",
);
DateTime _selectedDate = DateTime.now();
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SegmentedButton(
segments: [
ButtonSegment(value: LoanChangeType.loan, label: Text("Loan")),
ButtonSegment(value: LoanChangeType.owe, label: Text("Owe")),
],
selected: {_loanChangeType},
onSelectionChanged: (values) {
setState(() {
_loanChangeType = values.first;
});
},
),
TextField(
decoration: InputDecoration(
icon: Icon(Icons.euro),
hintText: "Amount",
),
controller: _amountController,
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
),
Row(
children: [
Text("Date"),
OutlinedButton(
onPressed: () async {
final dt = await showDatePicker(
context: context,
initialDate: _selectedDate,
firstDate: DateTime(1),
lastDate: DateTime(9999),
);
if (dt == null) return;
setState(() => _selectedDate = dt);
},
child: Text(formatDateTime(_selectedDate)),
),
],
),
Align(
alignment: Alignment.centerRight,
child: OutlinedButton(
onPressed: () async {
final sign = switch (_loanChangeType) {
LoanChangeType.owe => -1,
LoanChangeType.loan => 1,
};
final loanChange =
LoanChange()
..amount = sign * double.parse(_amountController.text).abs()
..date = DateTime.now();
await upsertLoanChange(loanChange);
widget.loan.changes.add(loanChange);
await upsertLoan(widget.loan);
widget.onDone();
},
child: Text(t.modals.add),
),
),
],
);
}
}