Display custom options view while options iterable waits to be resolved or is empty #147900
Display custom options view while options iterable waits to be resolved or is empty #147900victorsanni wants to merge 31 commits intoflutter:masterfrom
Conversation
…th-empty-iterable
justinmc
left a comment
There was a problem hiding this comment.
We need make a final decision on if this 2-boolean approach is the best way to go. It looks pretty good in this PR. Maybe the error case that I mentioned in the comments is something we should make sure that it can do. Is there anything else that this 2-boolean approach would not cover? How about that issue that I tagged you in earlier?
| /// | ||
| /// This means that the original function will be called only after no calls | ||
| /// have been made for the given Duration. | ||
| _Debounceable<S, T> _debounce<S, T>(_Debounceable<S?, T> function) { |
There was a problem hiding this comment.
If you totally removed the debouncing from this example, would it be easier to understand the new stuff, or would it be too unrealistic?
There was a problem hiding this comment.
Yes, but it might also make it difficult to understand why _isLoading should be set to false only when the current text is equal to the last text searched with (_lastValue). So, if someone were to dup the example, remove the (seemingly) extra variables _lastOptions and _lastValue and later add debouncing, they would probably run into errors and get confused.
| Future<void> _onChangedField() async { | ||
| final TextEditingValue value = _textEditingController.value; | ||
| if (widget.showOptionsViewOnUncompletedOptions){ | ||
| _updateOptionsViewVisibility(); |
There was a problem hiding this comment.
How does this affect synchronous values of optionsBuilder? optionsViewBuilder could be called twice back-to-back. Is that something we should avoid or it shouldn't matter?
There was a problem hiding this comment.
If optionsBuilder is synchronous, and showOptionsViewOnPendingOptions is set to true, then the options view will just be rebuilt to display its current contents.
I've added a note for users to ignore showOptionsViewOnPendingOptions if their optionsBuilder is synchronous.
|
Bug report on the example: Screencast.from.2024-05-20.12-02-59.webmUsing the same steps, one time I also saw the "Loading" disappear and a few seconds later the correct results appeared. Couldn't capture that in the recording though. It probably has to do with subtle timing. |
Fixing this bug required me to revert the change which terminates I was also running into another bug where if there was a selection and the app window goes into the background, when the text field is refocused, the loading message displays infinitely. To fix that, I use |
| this.textEditingController, | ||
| this.initialValue, | ||
| this.showOptionsViewOnEmptyOptions = false, | ||
| this.showOptionsViewOnPendingOptions = false, |
There was a problem hiding this comment.
It seems to me that these two flags have a lot in common: they both want to show the options view. I think that's a sign that the API could be more generic / flexible. Can we let the user specify what the logic in _canShowOptionsView should look like, and have the current behavior as the default?
There was a problem hiding this comment.
I thought the same thing. I'd like to see what that would look like.
| bool get _canShowOptionsView => _focusNode.hasFocus && _selection == null && _options.isNotEmpty; | ||
| bool get _canShowOptionsView => _focusNode.hasFocus && _selection == null && | ||
| (_options.isNotEmpty | ||
| || widget.showOptionsViewOnEmptyOptions |
There was a problem hiding this comment.
nit: { } for multiline expressions per the style guide, instead of =>.
There was a problem hiding this comment.
Also the logic doesn't seem correct? showOptionsViewOnEmptyOptions and showOptionsViewOnPendingOptions seem to have the same effect here, but they are different flags?
|
This is waiting on #150776, and then it should be updated with those changes. |
|
@mohsinraza-fdev do you have any insights here from the changes in #150776? |
Okay so i've noticed a few things ill just list them here, some of them might already be in your knowledge. Case 1: Issue: Screen.Recording.2024-09-04.at.5.13.14.PM.mov#150776 might have fixed this because we will only be updating options view from the last Case 2: Issue: Screen.Recording.2024-09-04.at.5.22.39.PM.movBy the way, i was just wondering that wouldn't it be better if we introduce nullable loading and empty state view builder arguments instead of these booleans 🤔, and display the main options view builder as fallback. What do you think? |
Exactly the feedback I got. If you'd like to implement this please feel free to do so, I would definitely shepherd it because this is a highly requested feature and you might have the most context on fixing it at the moment. |
Sure ill start working on it |
|
@victorsanni I've completed the logical implementation. Is this the desired behaviour? Screen.Recording.2024-09-12.at.10.45.07.PM.movI might need a little help with variable naming and comments 👀 |
|
Looks good @mohsinraza-fdev! This was a guide Justin left me when I first started working on this: victorsanni#1 (review). If you can model all these in the solution then we should be good to go. Thanks for the work! |
Sure, Thanks! |
|
Created a pull request for loading and empty state view builders #155506 |
Currently in
Autocomplete, there is no way to:Display a custom options view when the options returned from
optionsBuilderis empty:Autocomplete widget calls optionsViewBuilder even when optionsBuilder return empty Iterable #108258
This is useful if one wants to display a custom message when the field is focused for the first time, or when no options were found.
Display a loading message while waiting for the
optionsBuilderfuture to be resolved.This PR fixes these issues by introducing two boolean parameters to
AutocompleteandRawAutocomplete:showOptionsViewOnEmptyOptionsandshowOptionsViewOnPendingOptions.When set to true,
showOptionsViewOnEmptyOptionsdisplays the options view even when the options returned fromoptionsBuilderis empty.When set to true,
showOptionsViewOnPendingOptionsdisplays the options view just before awaiting theoptionsBuilderfuture, allowing a custom loading view to be shown while waiting for theoptionsBuilderfuture to be resolved.Fixes #108258 and #147377.
Before:
autocomplete_b4.mov
After:
Autocomplete.video.mov
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.