2023-09-08 11:50:21 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:prasule/api/category.dart';
|
2023-10-09 17:28:05 +02:00
|
|
|
import 'package:prasule/api/entry_data.dart';
|
|
|
|
import 'package:prasule/api/walletentry.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/api/wallet.dart';
|
|
|
|
import 'package:prasule/api/walletmanager.dart';
|
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
|
|
|
import 'package:prasule/pw/platformfield.dart';
|
|
|
|
|
|
|
|
class CreateEntryView extends StatefulWidget {
|
|
|
|
final Wallet w;
|
2023-10-09 17:28:05 +02:00
|
|
|
final WalletSingleEntry? editEntry;
|
2023-09-14 19:44:34 +02:00
|
|
|
const CreateEntryView({super.key, required this.w, this.editEntry});
|
2023-09-08 11:50:21 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<CreateEntryView> createState() => _CreateEntryViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _CreateEntryViewState extends State<CreateEntryView> {
|
2023-10-09 17:28:05 +02:00
|
|
|
late WalletSingleEntry newEntry;
|
2023-09-08 11:50:21 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-09-14 19:44:34 +02:00
|
|
|
if (widget.editEntry != null) {
|
|
|
|
newEntry = widget.editEntry!;
|
|
|
|
} else {
|
|
|
|
var id = 1;
|
|
|
|
while (widget.w.entries.where((element) => element.id == id).isNotEmpty) {
|
|
|
|
id++; // create unique ID
|
|
|
|
}
|
2023-10-09 17:28:05 +02:00
|
|
|
newEntry = WalletSingleEntry(
|
2023-09-14 19:44:34 +02:00
|
|
|
id: id,
|
2023-10-09 17:28:05 +02:00
|
|
|
data: EntryData(amount: 0, name: ""),
|
2023-09-14 19:44:34 +02:00
|
|
|
type: EntryType.expense,
|
|
|
|
date: DateTime.now(),
|
|
|
|
category: widget.w.categories.first);
|
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text("Create new entry"),
|
|
|
|
),
|
|
|
|
body: SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width,
|
2023-09-08 11:50:21 +02:00
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
child: Center(
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: PlatformField(
|
2023-09-29 11:38:21 +02:00
|
|
|
labelText: "Name",
|
2023-10-09 17:28:05 +02:00
|
|
|
controller: TextEditingController(text: newEntry.data.name),
|
2023-09-08 11:50:21 +02:00
|
|
|
onChanged: (v) {
|
2023-10-09 17:28:05 +02:00
|
|
|
newEntry.data.name = v;
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 15,
|
|
|
|
),
|
|
|
|
SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: PlatformField(
|
2023-09-29 11:38:21 +02:00
|
|
|
labelText: "Amount",
|
2023-10-09 17:28:05 +02:00
|
|
|
controller: TextEditingController(
|
|
|
|
text: newEntry.data.amount.toString()),
|
2023-09-08 11:50:21 +02:00
|
|
|
keyboardType:
|
|
|
|
const TextInputType.numberWithOptions(decimal: true),
|
|
|
|
inputFormatters: [
|
2023-09-14 19:44:34 +02:00
|
|
|
FilteringTextInputFormatter.allow(
|
|
|
|
RegExp(r'\d+[\.,]{0,1}\d{0,}'))
|
2023-09-08 11:50:21 +02:00
|
|
|
],
|
|
|
|
onChanged: (v) {
|
2023-10-09 17:28:05 +02:00
|
|
|
newEntry.data.amount = double.parse(v);
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 20,
|
|
|
|
),
|
|
|
|
const Text("Type"),
|
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: DropdownButton<EntryType>(
|
|
|
|
value: newEntry.type,
|
|
|
|
items: [
|
|
|
|
DropdownMenuItem(
|
|
|
|
value: EntryType.expense,
|
|
|
|
child: SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8 - 24,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: const Text(
|
|
|
|
"Expense",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
DropdownMenuItem(
|
|
|
|
value: EntryType.income,
|
|
|
|
child: SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8 - 24,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: const Text("Income"),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
onChanged: (v) {
|
|
|
|
if (v == null) return;
|
|
|
|
newEntry.type = v;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 20,
|
|
|
|
),
|
|
|
|
const Text("Category"),
|
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
|
|
|
SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: DropdownButton<int>(
|
2023-09-14 19:44:34 +02:00
|
|
|
value: newEntry.category.id,
|
2023-09-08 11:50:21 +02:00
|
|
|
items: List.generate(
|
|
|
|
widget.w.categories.length,
|
|
|
|
(index) => DropdownMenuItem(
|
2023-09-14 19:44:34 +02:00
|
|
|
value: widget.w.categories[index].id,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8 - 24,
|
2023-09-08 11:50:21 +02:00
|
|
|
child: Text(
|
|
|
|
widget.w.categories[index].name,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onChanged: (v) {
|
|
|
|
if (v == null) return;
|
2023-09-14 19:44:34 +02:00
|
|
|
newEntry.category = widget.w.categories
|
|
|
|
.where((element) => element.id == v)
|
|
|
|
.first;
|
2023-09-08 11:50:21 +02:00
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 15,
|
|
|
|
),
|
|
|
|
PlatformButton(
|
|
|
|
text: "Save",
|
|
|
|
onPressed: () {
|
2023-10-09 17:28:05 +02:00
|
|
|
if (newEntry.data.name.isEmpty) {
|
2023-09-08 11:50:21 +02:00
|
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(
|
|
|
|
content: Text("Name cannot be empty"),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-14 19:44:34 +02:00
|
|
|
if (widget.editEntry != null) {
|
|
|
|
Navigator.of(context).pop(newEntry);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
widget.w.entries.add(newEntry);
|
|
|
|
WalletManager.saveWallet(widget.w).then((value) =>
|
|
|
|
Navigator.of(context)
|
|
|
|
.pop(widget.w)); // TODO loading circle?
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|