hiddencaliber / flutter_multiselect

Flutter package for multi-select UI widget
Other
52 stars 37 forks source link

Reset / clear selection #16

Open knight-dev opened 5 years ago

knight-dev commented 5 years ago

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.

himanshi7046 commented 4 years ago

is there any update for that

bobppsureway commented 4 years ago

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 commented 4 years ago

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

bobppsureway commented 4 years ago

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 commented 4 years ago

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.