prasule/lib/views/create_entry.dart

218 lines
7.7 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:flutter_gen/gen_l10n/app_localizations.dart';
2023-09-08 11:50:21 +02:00
import 'package:prasule/api/category.dart';
import 'package:prasule/api/entry_data.dart';
2023-09-08 11:50:21 +02:00
import 'package:prasule/api/wallet.dart';
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';
import 'package:prasule/util/show_message.dart';
2023-09-08 11:50:21 +02:00
/// Used when user wants to add new entry
class CreateSingleEntryView extends StatefulWidget {
/// Used when user wants to add new entry
const CreateSingleEntryView({required this.w, super.key, this.editEntry});
/// The wallet, where the entry will be saved to
2023-09-08 11:50:21 +02:00
final Wallet w;
/// Entry we want to edit
///
/// Is null unless we are editing an existing entry
final WalletSingleEntry? editEntry;
2023-09-08 11:50:21 +02:00
@override
State createState() => _CreateSingleEntryViewState();
2023-09-08 11:50:21 +02:00
}
class _CreateSingleEntryViewState extends State<CreateSingleEntryView> {
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: ""),
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(
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(
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(
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,
),
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,
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,
),
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(
text: AppLocalizations.of(context).save,
2023-09-08 11:50:21 +02:00
onPressed: () {
if (newEntry.data.name.isEmpty) {
showMessage(
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);
WalletManager.saveWallet(widget.w).then(
(value) => Navigator.of(context).pop(widget.w),
); // TODO loading circle?
2023-09-08 11:50:21 +02:00
},
),
2023-09-08 11:50:21 +02:00
],
),
),
),
),
);
}
}