From 58dd06f39f09ea6653453f9f941b5b8ddea748b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Mon, 22 Apr 2024 16:18:48 +0200 Subject: [PATCH] fix: use controllers for textfields Fixes #39 --- lib/views/create_entry.dart | 35 +++++++++++++------------- lib/views/create_recur_entry.dart | 42 +++++++++++++++---------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/lib/views/create_entry.dart b/lib/views/create_entry.dart index 26593f2..937e5af 100644 --- a/lib/views/create_entry.dart +++ b/lib/views/create_entry.dart @@ -16,7 +16,8 @@ class CreateSingleEntryView extends StatefulWidget { /// Used when user wants to add new entry const CreateSingleEntryView({ required this.w, - required this.locale, super.key, + required this.locale, + super.key, this.editEntry, }); @@ -36,6 +37,9 @@ class CreateSingleEntryView extends StatefulWidget { class _CreateSingleEntryViewState extends State { late WalletSingleEntry newEntry; + final _entryNameController = TextEditingController(); + final _entryBalanceController = TextEditingController(); + final _entryDescriptionController = TextEditingController(); @override void initState() { super.initState(); @@ -50,6 +54,9 @@ class _CreateSingleEntryViewState extends State { category: widget.w.categories.first, ); } + _entryNameController.text = newEntry.data.name; + _entryBalanceController.text = newEntry.data.amount.toString(); + _entryDescriptionController.text = newEntry.data.description; setState(() {}); } @@ -71,10 +78,7 @@ class _CreateSingleEntryViewState extends State { width: MediaQuery.of(context).size.width * 0.8, child: PlatformField( labelText: AppLocalizations.of(context).name, - controller: TextEditingController(text: newEntry.data.name), - onChanged: (v) { - newEntry.data.name = v; - }, + controller: _entryNameController, ), ), const SizedBox( @@ -84,9 +88,7 @@ class _CreateSingleEntryViewState extends State { width: MediaQuery.of(context).size.width * 0.8, child: PlatformField( labelText: AppLocalizations.of(context).amount, - controller: TextEditingController( - text: newEntry.data.amount.toString(), - ), + controller: _entryBalanceController, keyboardType: const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ @@ -94,9 +96,6 @@ class _CreateSingleEntryViewState extends State { RegExp(r'\d+[\.,]{0,1}\d{0,}'), ), ], - onChanged: (v) { - newEntry.data.amount = double.parse(v); - }, ), ), const SizedBox( @@ -183,12 +182,7 @@ class _CreateSingleEntryViewState extends State { child: PlatformField( keyboardType: TextInputType.multiline, maxLines: null, - controller: TextEditingController( - text: newEntry.data.description, - ), - onChanged: (v) { - newEntry.data.description = v; - }, + controller: _entryDescriptionController, ), ), const SizedBox( @@ -224,13 +218,18 @@ class _CreateSingleEntryViewState extends State { PlatformButton( text: AppLocalizations.of(context).save, onPressed: () { - if (newEntry.data.name.isEmpty) { + 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; diff --git a/lib/views/create_recur_entry.dart b/lib/views/create_recur_entry.dart index be03897..5224277 100644 --- a/lib/views/create_recur_entry.dart +++ b/lib/views/create_recur_entry.dart @@ -7,7 +7,6 @@ import 'package:prasule/api/entry_data.dart'; import 'package:prasule/api/recurring_entry.dart'; import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet_manager.dart'; -import 'package:prasule/main.dart'; import 'package:prasule/pw/platformbutton.dart'; import 'package:prasule/pw/platformfield.dart'; import 'package:prasule/util/show_message.dart'; @@ -39,6 +38,10 @@ class CreateRecurringEntryView extends StatefulWidget { class _CreateRecurringEntryViewState extends State { late RecurringWalletEntry newEntry; + final _entryNameController = TextEditingController(); + final _entryBalanceController = TextEditingController(); + final _entryDescriptionController = TextEditingController(); + final _repeatAfterController = TextEditingController(); @override void initState() { super.initState(); @@ -55,6 +58,10 @@ class _CreateRecurringEntryViewState extends State { recurType: RecurType.month, ); } + _entryNameController.text = newEntry.data.name; + _entryBalanceController.text = newEntry.data.amount.toString(); + _entryDescriptionController.text = newEntry.data.description; + _repeatAfterController.text = newEntry.repeatAfter.toString(); setState(() {}); } @@ -76,10 +83,7 @@ class _CreateRecurringEntryViewState extends State { width: MediaQuery.of(context).size.width * 0.8, child: PlatformField( labelText: AppLocalizations.of(context).name, - controller: TextEditingController(text: newEntry.data.name), - onChanged: (v) { - newEntry.data.name = v; - }, + controller: _entryNameController, ), ), const SizedBox( @@ -89,9 +93,7 @@ class _CreateRecurringEntryViewState extends State { width: MediaQuery.of(context).size.width * 0.8, child: PlatformField( labelText: AppLocalizations.of(context).amount, - controller: TextEditingController( - text: newEntry.data.amount.toString(), - ), + controller: _entryBalanceController, keyboardType: const TextInputType.numberWithOptions(decimal: true), inputFormatters: [ @@ -99,10 +101,6 @@ class _CreateRecurringEntryViewState extends State { RegExp(r'\d+[\.,]{0,1}\d{0,}'), ), ], - onChanged: (v) { - logger.i(v); - newEntry.data.amount = double.parse(v); - }, ), ), const SizedBox( @@ -189,12 +187,7 @@ class _CreateRecurringEntryViewState extends State { child: PlatformField( keyboardType: TextInputType.multiline, maxLines: null, - controller: TextEditingController( - text: newEntry.data.description, - ), - onChanged: (v) { - newEntry.data.description = v; - }, + controller: _entryDescriptionController, ), ), const SizedBox( @@ -215,9 +208,7 @@ class _CreateRecurringEntryViewState extends State { SizedBox( width: 50, child: PlatformField( - controller: TextEditingController( - text: newEntry.repeatAfter.toString(), - ), + controller: _repeatAfterController, inputFormatters: [ FilteringTextInputFormatter.digitsOnly, FilteringTextInputFormatter.deny( @@ -313,13 +304,20 @@ class _CreateRecurringEntryViewState extends State { PlatformButton( text: AppLocalizations.of(context).save, onPressed: () { - if (newEntry.data.name.isEmpty) { + if (_entryNameController.text.isEmpty) { showMessage( AppLocalizations.of(context).errorEmptyName, context, ); return; } + newEntry.data.name = _entryNameController.text; + newEntry.data.amount = + double.parse(_entryBalanceController.text); + newEntry.repeatAfter = + int.parse(_repeatAfterController.text); + newEntry.data.description = + _entryDescriptionController.text; if (widget.editEntry != null) { Navigator.of(context).pop(newEntry); return;