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

Hide form fields based on value of other form fields? #316

Closed
joshuakoh1 opened this issue Jun 13, 2020 · 5 comments
Closed

Hide form fields based on value of other form fields? #316

joshuakoh1 opened this issue Jun 13, 2020 · 5 comments
Labels
question Further information is requested

Comments

@joshuakoh1
Copy link

Using _fbKey.currentState.fields['dependent_field'].currentState.value does not work, throws the error that customState was called on null

@Dennis-Mwea
Copy link

Dennis-Mwea commented Jun 13, 2020

@joshuakoh1 You could use Visibility() widget and set visible to either true or false depending on the value of the other field.

@danvick danvick added the question Further information is requested label Jun 13, 2020
@joshuakoh1
Copy link
Author

joshuakoh1 commented Jun 13, 2020

@Dennis-Mwea Do you mean I'll declare a separate bool value on init then set that with onChanged of the form field that is responsible for the visibility value? Because my current setup throws an error "NoSuchMethodError: The method '[]' was called on null" as the state has not been initialised unless the form has been submitted.

 Visibility(
                            visible: this._fbKey.currentState?.fields['dependent_field']?.currentState.value,
                            maintainState: true,
                            child: Text('Hello'),
                          ),

@Dennis-Mwea
Copy link

Dennis-Mwea commented Jun 14, 2020

@joshuakoh1 Yeah you have to declare separate bool value on init then set that with onChanged of the form field that is responsible for the visibility value. This is what I did

                  bool enterAddressManually = false;



                   Container(
                      margin: EdgeInsets.only(
                        top: 10.0,
                        bottom: enterAddressManually ? 0.0 : 16.0,
                      ),
                      child: RichText(
                        text: TextSpan(
                          text: 'Enter address manually',
                          style: TextStyle(
                            color: Colors.white,
                            fontFamily: 'Gibson',
                            fontSize: 12.0,
                            fontWeight: FontWeight.w400,
                            height: 1.4,
                          ),
                          recognizer: TapGestureRecognizer()
                            ..onTap = () {
                              setState(() {
                                enterAddressManually = true;
                              });
                            },
                        ),
                      ),
                    ),
                    Visibility(
                      visible: enterAddressManually,
                      child: FormBuilderDropdown(
                        attribute: "address",
                        style: TextStyle(color: Colors.white),
                        initialValue: selectedAddress,
                        onChanged: (val) {
                          setState(() {
                            selectedAddress = val;
                            address = addresses
                                .where(
                                    (address) => address.id == selectedAddress)
                                .toList()
                                .first;
                          });
                        },
                        validators: [
                          FormBuilderValidators.required(
                            errorText: 'Please choose your address.',
                          ),
                        ],
                        items: List.generate(
                          addresses.length,
                          (index) => DropdownMenuItem(
                            value: index,
                            child: Text('${addresses[index].address_1}'),
                            onTap: () {
                              setState(() {
                                address1Controller.text =
                                    addresses[index].address_1;
                                address2Controller.text =
                                    addresses[index].address_2;
                                address3Controller.text =
                                    addresses[index].address_3;
                                cityController.text = addresses[index].city;
                                countyController.text = addresses[index].county;
                              });
                            },
                          ),
                        ),
                      ),
                    ),

@deandreamatias
Copy link
Collaborator

Hi @joshuakoh1!
This still an issue?

@deandreamatias deandreamatias added the awaiting author response Waiting for author of issue to respond with more info label Jun 13, 2022
@deandreamatias
Copy link
Collaborator

Due to lack of response and an old error, I will close this issue.
If the bug still exists, feel free to open a new issue

@deandreamatias deandreamatias removed the awaiting author response Waiting for author of issue to respond with more info label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants