171 lines
5.9 KiB
Dart
171 lines
5.9 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter/services.dart';
|
||
|
import 'package:prasule/api/category.dart';
|
||
|
import 'package:prasule/api/entry.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';
|
||
|
|
||
|
class CreateEntryView extends StatefulWidget {
|
||
|
final Wallet w;
|
||
|
const CreateEntryView({super.key, required this.w});
|
||
|
|
||
|
@override
|
||
|
State<CreateEntryView> createState() => _CreateEntryViewState();
|
||
|
}
|
||
|
|
||
|
class _CreateEntryViewState extends State<CreateEntryView> {
|
||
|
late WalletEntry newEntry;
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
newEntry = WalletEntry(
|
||
|
name: "",
|
||
|
amount: 0,
|
||
|
type: EntryType.expense,
|
||
|
date: DateTime.now(),
|
||
|
category: widget.w.categories.first);
|
||
|
setState(() {});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text("Create new entry"),
|
||
|
),
|
||
|
body: SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.9,
|
||
|
height: MediaQuery.of(context).size.height,
|
||
|
child: Center(
|
||
|
child: SingleChildScrollView(
|
||
|
child: Column(
|
||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||
|
children: [
|
||
|
const Text("Name"),
|
||
|
SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4,
|
||
|
child: PlatformField(
|
||
|
onChanged: (v) {
|
||
|
newEntry.name = v;
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(
|
||
|
height: 15,
|
||
|
),
|
||
|
const Text("Amount"),
|
||
|
SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4,
|
||
|
child: PlatformField(
|
||
|
controller: TextEditingController(text: "0"),
|
||
|
keyboardType:
|
||
|
const TextInputType.numberWithOptions(decimal: true),
|
||
|
inputFormatters: [
|
||
|
FilteringTextInputFormatter.allow(RegExp(r'\d+[\.,]\d+'))
|
||
|
],
|
||
|
onChanged: (v) {
|
||
|
newEntry.amount = double.parse(v);
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(
|
||
|
height: 20,
|
||
|
),
|
||
|
const Text("Type"),
|
||
|
const SizedBox(
|
||
|
height: 10,
|
||
|
),
|
||
|
SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4,
|
||
|
child: DropdownButton<EntryType>(
|
||
|
value: newEntry.type,
|
||
|
items: [
|
||
|
DropdownMenuItem(
|
||
|
value: EntryType.expense,
|
||
|
child: SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4 - 24,
|
||
|
child: const Text(
|
||
|
"Expense",
|
||
|
textAlign: TextAlign.center,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
DropdownMenuItem(
|
||
|
value: EntryType.income,
|
||
|
child: SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4 - 24,
|
||
|
child: const Text("Income"),
|
||
|
),
|
||
|
),
|
||
|
],
|
||
|
onChanged: (v) {
|
||
|
if (v == null) return;
|
||
|
newEntry.type = v;
|
||
|
setState(() {});
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(
|
||
|
height: 20,
|
||
|
),
|
||
|
const Text("Category"),
|
||
|
const SizedBox(
|
||
|
height: 10,
|
||
|
),
|
||
|
SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4,
|
||
|
child: DropdownButton<int>(
|
||
|
value: widget.w.categories.indexOf(newEntry.category),
|
||
|
items: List.generate(
|
||
|
widget.w.categories.length,
|
||
|
(index) => DropdownMenuItem(
|
||
|
value: widget.w.categories
|
||
|
.indexOf(widget.w.categories[index]),
|
||
|
child: SizedBox(
|
||
|
width: MediaQuery.of(context).size.width * 0.4 - 24,
|
||
|
child: Text(
|
||
|
widget.w.categories[index].name,
|
||
|
textAlign: TextAlign.center,
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
onChanged: (v) {
|
||
|
if (v == null) return;
|
||
|
newEntry.category = widget.w.categories[v];
|
||
|
setState(() {});
|
||
|
},
|
||
|
),
|
||
|
),
|
||
|
const SizedBox(
|
||
|
height: 15,
|
||
|
),
|
||
|
PlatformButton(
|
||
|
text: "Save",
|
||
|
onPressed: () {
|
||
|
if (newEntry.name.isEmpty) {
|
||
|
ScaffoldMessenger.of(context).clearSnackBars();
|
||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||
|
const SnackBar(
|
||
|
content: Text("Name cannot be empty"),
|
||
|
),
|
||
|
);
|
||
|
return;
|
||
|
}
|
||
|
widget.w.entries.add(newEntry);
|
||
|
WalletManager.saveWallet(widget.w).then((value) =>
|
||
|
Navigator.of(context)
|
||
|
.pop(widget.w)); // TODO loading circle?
|
||
|
},
|
||
|
)
|
||
|
],
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|