salim-lachdhaf / searchable_dropdown

Simple and robust Dropdown with item search feature, making it possible to use an offline item list or filtering URL for easy customization.
MIT License
326 stars 316 forks source link

what are the options we have to clear selecteditems in the multiselection(in my case i have reset button when i press those data not disappear) #638

Closed sabari7320 closed 1 month ago

sabari7320 commented 2 months ago

Screenshot_20240425-162449 Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context Add any other context about the problem here.

sabari7320 commented 2 months ago

1 st page: MultiLevelDropdown(

          hintText: 'Designation',
          label:'Designation',
          items:  designationList, // Assuming skillSetList is a list of strings
          selectedItem: reset_multidropdown ? [] : desList, // Reset selected items if reset flag is true
          onChanged: (selectedItems) {
         // designationController.text = selectedItems.isNotEmpty ? selectedItems.join(', ') : ''; // Update the controller with the selected items
            setState(() {
              desList = selectedItems;
              reset_multidropdown=false;// Update the list of selected skills

            });

          print("DESLIST${desList}");

          },
        ),

2:page:class MultiLevelDropdown extends StatefulWidget { final String? hintText; final String? label; final List? items; final Function(List)? onChanged; final List? selectedItem;

MultiLevelDropdown({ required this.hintText, required this.label, required this.items, required this.onChanged, this.selectedItem, });

@override _MultiLevelDropdownState createState() => _MultiLevelDropdownState(); }

class _MultiLevelDropdownState extends State<MultiLevelDropdown> { late List _selectedItems; // Method to reset the MultiLevelDropdown state

@override void initState() { super.initState(); // Initialize selected items with the provided selected items or an empty list _selectedItems = widget.selectedItem ?? []; }

@override Widget build(BuildContext context) { return DropdownSearch.multiSelection(

    popupOnItemRemoved: (selectedItems, removedItem) {
      print("REMOVED ITem");
      print(removedItem);
    },
  dropdownSearchDecoration: InputDecoration(
    labelStyle: TextStyle(fontSize: 15, color: Colors.grey),
    labelText: widget.label,
    hintText: widget.hintText,
    hintStyle: TextStyle(fontSize: 15, color: Colors.grey),
    contentPadding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
    enabledBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.grey),
      borderRadius: BorderRadius.circular(10),
    ),
    focusedBorder: OutlineInputBorder(
      borderSide: BorderSide(color: Colors.grey),
      borderRadius: BorderRadius.circular(10),
    ),
  ),
  mode: Mode.MENU,
  showSearchBox: false,
  items: widget.items!,
  itemAsString: (item) => item.toString(),
  onChanged: (selectedItems) {
    setState(() {
      _selectedItems = selectedItems;
    });
    widget.onChanged!(_selectedItems);

  },

  popupItemBuilder: (context, item, isSelected) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
      child: Row(
        children: [
          SizedBox(width: 8),
          Text(item.toString()),
        ],
      ),
    );
  },
  dropdownBuilder: (context, selectedItem) {
    return Row(
      children: _selectedItems.map((item) {
        return Row(
          children: [
            Text("${item.toString()}"),
            SizedBox(width: 8),
            if (_selectedItems.last != item) Text(", "),
          ],
        );
      }).toList(),

    );
  },
);

}

} my codes

sabari7320 commented 2 months ago

I regret for asking this question because most of things already have in example (i think many doesn't get guidence like comments) that's why their raising repeated questions. ok fine i found solution problem of mine is:
steps: 1) paste this final _multiKey = GlobalKey<DropdownSearchState>(); 2.) pass the multikey 3.)and _multiKey.currentState?.clear(); this would be help for who stuggling to remove all items in multiselection

kashiflab commented 2 months ago

I regret for asking this question because most of things already have in example (i think many doesn't get guidence like comments) that's why their raising repeated questions. ok fine i found solution problem of mine is: steps:

  1. paste this final _multiKey = GlobalKey(); 2.) pass the multikey 3.)and _multiKey.currentState?.clear(); this would be help for who stuggling to remove all items in multiselection

It works 🙌