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; final WalletEntry? editEntry; const CreateEntryView({super.key, required this.w, this.editEntry}); @override State createState() => _CreateEntryViewState(); } class _CreateEntryViewState extends State { late WalletEntry newEntry; @override void initState() { super.initState(); if (widget.editEntry != null) { newEntry = widget.editEntry!; } else { var id = 1; while (widget.w.entries.where((element) => element.id == id).isNotEmpty) { id++; // create unique ID } newEntry = WalletEntry( id: id, 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, 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: "Name", controller: TextEditingController(text: newEntry.name), onChanged: (v) { newEntry.name = v; }, ), ), const SizedBox( height: 15, ), SizedBox( width: MediaQuery.of(context).size.width * 0.8, child: PlatformField( labelText: "Amount", controller: TextEditingController(text: newEntry.amount.toString()), keyboardType: const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ FilteringTextInputFormatter.allow( RegExp(r'\d+[\.,]{0,1}\d{0,}')) ], 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.8, child: DropdownButton( value: newEntry.type, items: [ DropdownMenuItem( value: EntryType.expense, child: SizedBox( width: MediaQuery.of(context).size.width * 0.8 - 24, child: const Text( "Expense", ), ), ), DropdownMenuItem( value: EntryType.income, child: SizedBox( width: MediaQuery.of(context).size.width * 0.8 - 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.8, child: DropdownButton( 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: 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; } 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? }, ) ], ), ), ), ), ); } }