250 lines
9.1 KiB
Dart
250 lines
9.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:prasule/api/category.dart';
|
|
import 'package:prasule/api/entry_data.dart';
|
|
import 'package:prasule/api/wallet.dart';
|
|
import 'package:prasule/api/wallet_entry.dart';
|
|
import 'package:prasule/api/wallet_manager.dart';
|
|
import 'package:prasule/pw/platformbutton.dart';
|
|
import 'package:prasule/pw/platformfield.dart';
|
|
import 'package:prasule/util/show_message.dart';
|
|
|
|
/// 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,
|
|
required this.locale,
|
|
super.key,
|
|
this.editEntry,
|
|
});
|
|
|
|
/// The wallet, where the entry will be saved to
|
|
final Wallet w;
|
|
|
|
/// Entry we want to edit
|
|
///
|
|
/// Is null unless we are editing an existing entry
|
|
final WalletSingleEntry? editEntry;
|
|
|
|
/// Locale as set on user's system
|
|
final String locale;
|
|
|
|
@override
|
|
State createState() => _CreateSingleEntryViewState();
|
|
}
|
|
|
|
class _CreateSingleEntryViewState extends State<CreateSingleEntryView> {
|
|
late WalletSingleEntry newEntry;
|
|
final _entryNameController = TextEditingController();
|
|
final _entryBalanceController = TextEditingController();
|
|
final _entryDescriptionController = TextEditingController();
|
|
@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,
|
|
);
|
|
}
|
|
_entryNameController.text = newEntry.data.name;
|
|
_entryBalanceController.text = newEntry.data.amount.toString();
|
|
_entryDescriptionController.text = newEntry.data.description;
|
|
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: _entryNameController,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 15,
|
|
),
|
|
SizedBox(
|
|
width: MediaQuery.of(context).size.width * 0.8,
|
|
child: PlatformField(
|
|
labelText: AppLocalizations.of(context).amount,
|
|
controller: _entryBalanceController,
|
|
keyboardType:
|
|
const TextInputType.numberWithOptions(decimal: true),
|
|
inputFormatters: [
|
|
FilteringTextInputFormatter.allow(
|
|
RegExp(r'\d+[\.,]{0,1}\d{0,}'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 20,
|
|
),
|
|
Text(AppLocalizations.of(context).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,
|
|
),
|
|
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,
|
|
),
|
|
),
|
|
const SizedBox(
|
|
height: 20,
|
|
),
|
|
Text(AppLocalizations.of(context).date),
|
|
PlatformButton(
|
|
style: ButtonStyle(
|
|
backgroundColor: WidgetStateProperty.all(
|
|
Theme.of(context).colorScheme.primary,
|
|
),
|
|
foregroundColor: WidgetStateProperty.all(
|
|
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(() {});
|
|
},
|
|
),
|
|
const SizedBox(
|
|
height: 15,
|
|
),
|
|
PlatformButton(
|
|
text: AppLocalizations.of(context).save,
|
|
onPressed: () {
|
|
if (_entryNameController.text.isEmpty) {
|
|
showMessage(
|
|
AppLocalizations.of(context).errorEmptyName,
|
|
context,
|
|
);
|
|
return;
|
|
}
|
|
newEntry.data.name = _entryNameController.text;
|
|
newEntry.data.amount =
|
|
double.parse(_entryBalanceController.text);
|
|
newEntry.data.description =
|
|
_entryDescriptionController.text;
|
|
if (widget.editEntry != null) {
|
|
Navigator.of(context).pop(newEntry);
|
|
return;
|
|
}
|
|
widget.w.entries.add(newEntry);
|
|
WalletManager.saveWallet(widget.w); // TODO loading circle?
|
|
Navigator.of(context).pop(widget.w);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|