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

searchableDropDown: setting selectedItem programmatically does not update UI #627

Closed
dmoeyers opened this issue Dec 8, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@dmoeyers
Copy link

dmoeyers commented Dec 8, 2020

Hello,

when updating the selectedItem of a searchableDropDown programmatically

eg via
_formKey.currentState.fields['dropdown'].didChange(newValue);

the state of the form is updated but the previously selected item is still shown in the UI.

@danvick danvick self-assigned this Dec 8, 2020
@danvick danvick added the bug Something isn't working label Dec 8, 2020
@dmoeyers
Copy link
Author

dmoeyers commented Dec 8, 2020

Hi Danvick,

I have been looking at this some more and might have found a possible solution (however I am quite new at this so it's just a suggestion):

In DropdownSearchState<T> there's a function that can be used to change the value programmatically: void changeSelectedItem(T selectedItem)

If we then add final dropdownSearchKey = GlobalKey<dropdown_search.DropdownSearchState<T>>(); to _FormBuilderSearchableDropdownState<T>, we can add this key to the dropdown_search created in the builder function

dropdown_search.DropdownSearch<T>(
               key: state.dropdownSearchKey,

Next we can use the function mentioned above in _FormBuilderSearchableDropdownState<T>, either in the didChange override or in a separate method

class _FormBuilderSearchableDropdownState<T>
    extends FormBuilderFieldState<FormBuilderSearchableDropdown<T>, T> {
  final dropdownSearchKey = GlobalKey<dropdown_search.DropdownSearchState<T>>();

  @override
  void didChange(T value) {
    super.didChange(value);
    dropdownSearchKey.currentState.changeSelectedItem(value);
  }
  // void changeSelectedItem(T value) {
  //   dropdownSearchKey.currentState.changeSelectedItem(value);
  // }
}

The didChange has the advantage of keeping the interface uniform (to programmatically change values), but this creates an infinite loop because in the onChanged callback of the dropdown_search (called by the changeSelectedItem), we call back didChange... This loop can be broken by checking whether the value has really changed

onChanged: (val) {
                  state.requestFocus();
                  if(state.value!=val)
                    state.didChange(val); // todo changed
                },

The new separate method does not have this drawback, but then you would need to cast FormBuilderFieldState to _FormBuilderSearchableDropdownState every time or maybe change the interface so didChange is used internally to respond to change and a new function to programmatically change fields would have to be added...

Food for thought!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants