prasule/lib/views/create_entry.dart

255 lines
9.2 KiB
Dart
Raw Normal View History

2024-06-30 20:25:36 +02:00
// SPDX-FileCopyrightText: (C) 2024 Matyáš Caras
//
// SPDX-License-Identifier: AGPL-3.0-only
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';
2024-01-30 22:19:26 +01:00
import 'package:intl/intl.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
2024-01-30 22:19:26 +01:00
const CreateSingleEntryView({
required this.w,
required this.locale,
super.key,
2024-01-30 22:19:26 +01:00
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
2024-04-22 16:25:59 +02:00
/// Locale as set on user's system
2024-01-30 22:19:26 +01:00
final String locale;
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;
final _entryNameController = TextEditingController();
final _entryBalanceController = TextEditingController();
final _entryDescriptionController = TextEditingController();
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
}
_entryNameController.text = newEntry.data.name;
_entryBalanceController.text = newEntry.data.amount.toString();
_entryDescriptionController.text = newEntry.data.description;
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: _entryNameController,
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: _entryBalanceController,
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
],
),
),
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: _entryDescriptionController,
),
),
2024-01-30 22:19:26 +01:00
const SizedBox(
height: 20,
),
Text(AppLocalizations.of(context).date),
PlatformButton(
style: ButtonStyle(
2024-04-22 16:25:59 +02:00
backgroundColor: WidgetStateProperty.all(
2024-01-30 22:19:26 +01:00
Theme.of(context).colorScheme.primary,
),
2024-04-22 16:25:59 +02:00
foregroundColor: WidgetStateProperty.all(
2024-01-30 22:19:26 +01:00
Theme.of(context).colorScheme.onPrimary,
),
),
text: DateFormat.yMMMMd(widget.locale).format(newEntry.date),
onPressed: () async {
final date = await showDatePicker(
initialDate: newEntry.date,
context: context,
firstDate: DateTime.now()
.subtract(const Duration(days: 20 * 365)),
lastDate: DateTime.now().add(const Duration(days: 365)),
);
if (date == null) return;
newEntry.date = date;
setState(() {});
},
),
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 (_entryNameController.text.isEmpty) {
showMessage(
AppLocalizations.of(context).errorEmptyName,
context,
);
2023-09-08 11:50:21 +02:00
return;
}
newEntry.data.name = _entryNameController.text;
newEntry.data.amount =
double.parse(_entryBalanceController.text);
newEntry.data.description =
_entryDescriptionController.text;
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);
2024-06-30 11:30:07 +02:00
WalletManager.saveWallet(widget.w); // TODO loading circle?
Navigator.of(context).pop(widget.w);
2023-09-08 11:50:21 +02:00
},
),
2023-09-08 11:50:21 +02:00
],
),
),
),
),
);
}
}