2023-09-08 11:50:21 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/api/category.dart';
|
2023-10-09 17:28:05 +02:00
|
|
|
import 'package:prasule/api/entry_data.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/api/wallet.dart';
|
2024-01-08 21:19:15 +01:00
|
|
|
import 'package:prasule/api/wallet_entry.dart';
|
|
|
|
import 'package:prasule/api/wallet_manager.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
|
|
|
import 'package:prasule/pw/platformfield.dart';
|
2024-01-09 00:31:03 +01:00
|
|
|
import 'package:prasule/util/show_message.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Used when user wants to add new entry
|
2024-01-08 21:19:15 +01:00
|
|
|
class CreateSingleEntryView extends StatefulWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Used when user wants to add new entry
|
2024-01-08 21:19:15 +01:00
|
|
|
const CreateSingleEntryView({required this.w, super.key, this.editEntry});
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// The wallet, where the entry will be saved to
|
2023-09-08 11:50:21 +02:00
|
|
|
final Wallet w;
|
2023-12-29 21:39:54 +01:00
|
|
|
|
|
|
|
/// Entry we want to edit
|
|
|
|
///
|
|
|
|
/// Is null unless we are editing an existing entry
|
2023-10-09 17:28:05 +02:00
|
|
|
final WalletSingleEntry? editEntry;
|
2023-09-08 11:50:21 +02:00
|
|
|
|
|
|
|
@override
|
2024-01-08 21:19:15 +01:00
|
|
|
State createState() => _CreateSingleEntryViewState();
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
|
2024-01-08 21:19:15 +01:00
|
|
|
class _CreateSingleEntryViewState extends State<CreateSingleEntryView> {
|
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 {
|
2023-10-09 17:28:05 +02:00
|
|
|
newEntry = WalletSingleEntry(
|
2023-12-29 21:39:54 +01:00
|
|
|
id: widget.w.nextId,
|
|
|
|
data: EntryData(amount: 0, name: ""),
|
|
|
|
type: EntryType.expense,
|
|
|
|
date: DateTime.now(),
|
|
|
|
category: widget.w.categories.first,
|
|
|
|
);
|
2023-09-14 19:44:34 +02:00
|
|
|
}
|
2023-09-08 11:50:21 +02:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-12-25 19:03:52 +01:00
|
|
|
title: Text(AppLocalizations.of(context).createEntry),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
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-12-25 19:03:52 +01:00
|
|
|
labelText: AppLocalizations.of(context).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-12-25 19:03:52 +01:00
|
|
|
labelText: AppLocalizations.of(context).amount,
|
2023-10-09 17:28:05 +02:00
|
|
|
controller: TextEditingController(
|
2023-12-29 21:39:54 +01:00
|
|
|
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(
|
2023-12-29 21:39:54 +01:00
|
|
|
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,
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
Text(AppLocalizations.of(context).type),
|
2023-09-08 11:50:21 +02:00
|
|
|
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-10-11 18:37:18 +02:00
|
|
|
child: Text(
|
2023-12-25 19:03:52 +01:00
|
|
|
AppLocalizations.of(context).expense,
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
DropdownMenuItem(
|
|
|
|
value: EntryType.income,
|
|
|
|
child: SizedBox(
|
2023-09-14 19:44:34 +02:00
|
|
|
width: MediaQuery.of(context).size.width * 0.8 - 24,
|
2023-12-25 19:03:52 +01:00
|
|
|
child: Text(AppLocalizations.of(context).income),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
onChanged: (v) {
|
|
|
|
if (v == null) return;
|
|
|
|
newEntry.type = v;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
height: 20,
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
Text(AppLocalizations.of(context).category),
|
2023-09-08 11:50:21 +02:00
|
|
|
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(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-11-01 17:58:05 +01:00
|
|
|
const SizedBox(
|
|
|
|
height: 20,
|
|
|
|
),
|
2023-12-25 19:03:52 +01:00
|
|
|
Text(AppLocalizations.of(context).description),
|
2023-11-01 17:58:05 +01:00
|
|
|
const SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
2023-11-01 18:57:34 +01:00
|
|
|
ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(
|
2023-12-29 21:39:54 +01:00
|
|
|
minWidth: MediaQuery.of(context).size.width * 0.8,
|
|
|
|
maxWidth: MediaQuery.of(context).size.width * 0.8,
|
|
|
|
maxHeight: 300,
|
|
|
|
),
|
2023-11-01 17:58:05 +01:00
|
|
|
child: PlatformField(
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
maxLines: null,
|
|
|
|
controller: TextEditingController(
|
|
|
|
text: newEntry.data.description,
|
|
|
|
),
|
|
|
|
onChanged: (v) {
|
|
|
|
newEntry.data.description = v;
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
const SizedBox(
|
|
|
|
height: 15,
|
|
|
|
),
|
|
|
|
PlatformButton(
|
2023-12-25 19:03:52 +01:00
|
|
|
text: AppLocalizations.of(context).save,
|
2023-09-08 11:50:21 +02:00
|
|
|
onPressed: () {
|
2023-10-09 17:28:05 +02:00
|
|
|
if (newEntry.data.name.isEmpty) {
|
2024-01-09 00:31:03 +01:00
|
|
|
showMessage(
|
2024-01-22 14:38:01 +01:00
|
|
|
AppLocalizations.of(context).errorEmptyName,
|
|
|
|
context,
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
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);
|
2023-11-01 17:58:05 +01:00
|
|
|
WalletManager.saveWallet(widget.w).then(
|
|
|
|
(value) => Navigator.of(context).pop(widget.w),
|
|
|
|
); // TODO loading circle?
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|