fix: use controllers for textfields

Fixes #39
This commit is contained in:
Matyáš Caras 2024-04-22 16:18:48 +02:00
parent d832080a6d
commit 58dd06f39f
Signed by untrusted user who does not match committer: hernik
GPG key ID: 2A3175F98820C5C6
2 changed files with 37 additions and 40 deletions

View file

@ -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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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<CreateSingleEntryView> {
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;

View file

@ -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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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<CreateRecurringEntryView> {
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;