prasule/lib/views/create_entry.dart

208 lines
7.6 KiB
Dart
Raw Normal View History

2023-09-08 11:50:21 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:prasule/api/category.dart';
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';
2023-10-11 18:37:18 +02:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2023-09-08 11:50:21 +02:00
class CreateEntryView extends StatefulWidget {
final Wallet w;
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> {
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 {
newEntry = WalletSingleEntry(
id: widget.w.nextId,
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(
2023-10-11 18:37:18 +02: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-10-11 18:37:18 +02:00
labelText: AppLocalizations.of(context)!.name,
controller: TextEditingController(text: newEntry.data.name),
2023-09-08 11:50:21 +02:00
onChanged: (v) {
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-10-11 18:37:18 +02:00
labelText: AppLocalizations.of(context)!.amount,
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) {
newEntry.data.amount = double.parse(v);
2023-09-08 11:50:21 +02:00
},
),
),
const SizedBox(
height: 20,
),
2023-11-01 18:54:14 +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(
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-10-11 18:37:18 +02: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-10-11 18:37:18 +02: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(() {});
},
),
),
const SizedBox(
height: 20,
),
Text(AppLocalizations.of(context)!.description),
const SizedBox(
height: 10,
),
2023-11-01 18:57:34 +01:00
ConstrainedBox(
constraints: BoxConstraints(
minWidth: MediaQuery.of(context).size.width * 0.8,
maxWidth: MediaQuery.of(context).size.width * 0.8,
maxHeight: 300),
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-10-11 18:37:18 +02:00
text: AppLocalizations.of(context)!.save,
2023-09-08 11:50:21 +02:00
onPressed: () {
if (newEntry.data.name.isEmpty) {
2023-09-08 11:50:21 +02:00
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
2023-10-11 18:37:18 +02:00
SnackBar(
content: Text(
AppLocalizations.of(context)!.errorEmptyName),
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);
WalletManager.saveWallet(widget.w).then(
(value) => Navigator.of(context).pop(widget.w),
); // TODO loading circle?
2023-09-08 11:50:21 +02:00
},
)
],
),
),
),
),
);
}
}