Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling setState in the widget resets the form. #1134

Closed
anovis opened this issue Nov 1, 2022 · 2 comments
Closed

calling setState in the widget resets the form. #1134

anovis opened this issue Nov 1, 2022 · 2 comments
Labels
awaiting author response Waiting for author of issue to respond with more info bug Something isn't working

Comments

@anovis
Copy link

anovis commented Nov 1, 2022

I would like to toggle visibility of fields depending on the value of a checkbox (#316).

I am trying to set the state of a seperate variable, but everytime I call setState the state of the form resets to its initial state.

I defaulted a variable and toggle it via setState. everytime setState is called the form resets

Checkbox(
                      value: sameHours,
                      onChanged: (value) {
                        setState(() {
                          sameHours = value!;
                        })
                      },
                    ),

the following attempt using FormBuilderCheckbox didn't reset the form, but never updates the checkbox state (ie setsate for the form is not called)

   FormBuilderCheckbox(
                      name: "same_hours",
                      title: YText("Same Every day?"),
                      onChanged: (value) {
                        setState(() {
                          sameHours = value!;
                        });
                      },
                    )
                    

curious if I am doing something incorrect. or how i could toggle the visability of a field (external variable or internal)

Visibility(
                        visible: samehour or _formKey.currentState?.value["same_hours"],
                        child: FormBuilderCupertinoDateTimePicker(
                            name: "same_open",
                            initialValue: DateTime(1, 1, 1, 9, 0).toUtc(),
                            inputType: CupertinoDateTimePickerInputType.time,
                            decoration: formInput("same_open", width: 120))),
                            

using _formKey.currentState?.value["same_hours"] never updated the visibility

@anovis anovis added the bug Something isn't working label Nov 1, 2022
@deandreamatias
Copy link
Collaborator

Hi @anovis!
Try use _formKey.currentState?.instantValue["same_hours"]
The difference between instantValue and value is that value is only update when save the form (save() method)

@deandreamatias deandreamatias added the awaiting author response Waiting for author of issue to respond with more info label Nov 3, 2022
@anovis
Copy link
Author

anovis commented Nov 3, 2022

Ah!!! thats it. Appreciate the help.

@anovis anovis closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author response Waiting for author of issue to respond with more info bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants