prasule/lib/views/create_entry.dart
2023-11-01 17:58:05 +01:00

207 lines
7.4 KiB
Dart

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';
import 'package:prasule/api/wallet.dart';
import 'package:prasule/api/walletmanager.dart';
import 'package:prasule/pw/platformbutton.dart';
import 'package:prasule/pw/platformfield.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class CreateEntryView extends StatefulWidget {
final Wallet w;
final WalletSingleEntry? editEntry;
const CreateEntryView({super.key, required this.w, this.editEntry});
@override
State<CreateEntryView> createState() => _CreateEntryViewState();
}
class _CreateEntryViewState extends State<CreateEntryView> {
late WalletSingleEntry newEntry;
@override
void initState() {
super.initState();
if (widget.editEntry != null) {
newEntry = widget.editEntry!;
} else {
newEntry = WalletSingleEntry(
id: widget.w.nextId,
data: EntryData(amount: 0, name: ""),
type: EntryType.expense,
date: DateTime.now(),
category: widget.w.categories.first);
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.createEntry),
),
body: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: PlatformField(
labelText: AppLocalizations.of(context)!.name,
controller: TextEditingController(text: newEntry.data.name),
onChanged: (v) {
newEntry.data.name = v;
},
),
),
const SizedBox(
height: 15,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: PlatformField(
labelText: AppLocalizations.of(context)!.amount,
controller: TextEditingController(
text: newEntry.data.amount.toString()),
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r'\d+[\.,]{0,1}\d{0,}'))
],
onChanged: (v) {
newEntry.data.amount = double.parse(v);
},
),
),
const SizedBox(
height: 20,
),
const Text("Type"),
const SizedBox(
height: 10,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: DropdownButton<EntryType>(
value: newEntry.type,
items: [
DropdownMenuItem(
value: EntryType.expense,
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8 - 24,
child: Text(
AppLocalizations.of(context)!.expense,
),
),
),
DropdownMenuItem(
value: EntryType.income,
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8 - 24,
child: Text(AppLocalizations.of(context)!.income),
),
),
],
onChanged: (v) {
if (v == null) return;
newEntry.type = v;
setState(() {});
},
),
),
const SizedBox(
height: 20,
),
Text(AppLocalizations.of(context)!.category),
const SizedBox(
height: 10,
),
SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
child: DropdownButton<int>(
value: newEntry.category.id,
items: List.generate(
widget.w.categories.length,
(index) => DropdownMenuItem(
value: widget.w.categories[index].id,
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8 - 24,
child: Text(
widget.w.categories[index].name,
),
),
),
),
onChanged: (v) {
if (v == null) return;
newEntry.category = widget.w.categories
.where((element) => element.id == v)
.first;
setState(() {});
},
),
),
const SizedBox(
height: 20,
),
Text(AppLocalizations.of(context)!.description),
const SizedBox(
height: 10,
),
SizedBox(
height: 300,
width: MediaQuery.of(context).size.width * 0.8,
child: PlatformField(
keyboardType: TextInputType.multiline,
maxLines: null,
controller: TextEditingController(
text: newEntry.data.description,
),
onChanged: (v) {
newEntry.data.description = v;
setState(() {});
},
),
),
const SizedBox(
height: 15,
),
PlatformButton(
text: AppLocalizations.of(context)!.save,
onPressed: () {
if (newEntry.data.name.isEmpty) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
AppLocalizations.of(context)!.errorEmptyName),
),
);
return;
}
if (widget.editEntry != null) {
Navigator.of(context).pop(newEntry);
return;
}
widget.w.entries.add(newEntry);
WalletManager.saveWallet(widget.w).then(
(value) => Navigator.of(context).pop(widget.w),
); // TODO loading circle?
},
)
],
),
),
),
),
);
}
}