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

Reset / clear selection #16

Open
knight-dev opened this issue Sep 10, 2019 · 5 comments
Open

Reset / clear selection #16

knight-dev opened this issue Sep 10, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@knight-dev
Copy link

I'm having an issue where I get an error if the current datasource changes and the previously selected items don't match the new datasource. This behaviour mirrors the standard dropdownbutton behaviour which is fine, however, dropdown button offers access to the selected item so it can be set to null. It would be great if this feature could be implemented or some way to clear or reset the selected items.

@hiddencaliber hiddencaliber added the enhancement New feature or request label Sep 26, 2019
@himanshi7046
Copy link

is there any update for that

@bobppsureway
Copy link

I'm facing the same issue.
My solution is wrapping this MultiSelect widget with a new Form and give a new _formkey such as _multiSelectFormkKey.
Once you get a new datasource then call _newFormkKey.currentState.reset(); to reset the multiSelectDropDownForm.
All previous selectedOptionsInfoText and selected items will be reset.
It works like a charm.
But remember now you have a form embedded in another so validate both formKeys as needed.
For example if you are calling formkey.currentState.save(); or formkey.currentState.validate() don't forget to do the same thing to _multiSelectFormkKey.

@hiddencaliber
Copy link
Owner

@bobppsureway : Can you send me a runnable example with your problem?

@bobppsureway
Copy link

bobppsureway commented Jul 15, 2020

List dropDownData1 = [
{
"display": "Australia",
"value": 1,
},
{
"display": "Canada",
"value": 2,
},
];

List dropDownData2 = [
{
"display": "India",
"value": 3,
},
{
"display": "United States",
"value": 4,
},
];

List dropDownData= dropDownData1;

generateDropDownData(bool switch) {
if(swich==true)
{
setState(() {
controlPlanDropDownData = dropDownData1;
});
}
else
{
setState(() {
controlPlanDropDownData = dropDownData2;
});
}
}

Form(
key: _formKey,
child: MultiSelect(
autovalidate: false,
titleText: title,
validator: (value) {
if (value == null) {
return 'Please select one or more option(s)';
}
},
errorText: 'Please select one or more option(s)',
dataSource: dropDownData,
textField: 'display',
valueField: 'value',
filterable: true,
required: true,
value: null,
onSaved: (value) {
print('The value is $value');
}
)
)

If you select Australia and Canada then call generateDropDownData(false); then the dropdown populates with dropDownData2 info. This is right but the previously selected items are not reset. If you don't select any item with the new dropdown, call _formkey.save(); Returned value will be [Australia,Canada]. If select India and United States with the new dropdown the outcome will be [Australia,Canada, India, United States].
Obviously the previous values are stored in the cache.
We need a way to clear or reset the selected items.
Additionally, if the dropdown populates with initialValue [Australia, Canada] then set initialValue=null and call _formkey.currentState.reset(); It looks like the dropdown get reset but call _formkey.currentState.save(); outcome is [Australia, Canada].

@bobppsureway
Copy link

bobppsureway commented Jul 15, 2020

If the dropdown populates with initialValue then setState on initialValue=null.
Use async call
await Future.delayed(
Duration(milliseconds: 20), () {
_formKey.reset();
});
Then the selected items are reset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants