import 'package:flutter/material.dart'; import 'multi_select_item.dart'; /// Contains common actions that are used by different multi select classes. class MultiSelectActions { List onItemCheckedChange(List selectedValues, T itemValue, checked) { if (checked) { selectedValues.add(itemValue); } else { selectedValues.remove(itemValue); } return selectedValues; } /// Pops the dialog from the navigation stack and returns the initially selected values. void onCancelTap(BuildContext ctx, List initiallySelectedValues) { Navigator.pop(ctx, initiallySelectedValues); } /// Pops the dialog from the navigation stack and returns the selected values. /// Calls the onConfirm function if one was provided. void onConfirmTap(BuildContext ctx, List selectedValues, Function(List)? onConfirm) { Navigator.pop(ctx, selectedValues); if (onConfirm != null) { onConfirm(selectedValues); } } /// Pops the dialog from the navigation stack and returns the selected values. /// Calls the onCancel function if one was provided. void onClearTap(BuildContext ctx, List selectedValues, Function(List)? onCancelTap) { Navigator.pop(ctx, selectedValues); if (onCancelTap != null) { onCancelTap(selectedValues); } } /// Accepts the search query, and the original list of items. /// If the search query is valid, return a filtered list, otherwise return the original list. List> updateSearchQuery(String? val, List> allItems) { if (val != null && val.trim().isNotEmpty) { List> filteredItems = []; for (var item in allItems) { if (item.label.toLowerCase().contains(val.toLowerCase())) { filteredItems.add(item); } } return filteredItems; } else { return allItems; } } /// Toggles the search field. bool onSearchTap(showSearch) { return !showSearch; } List> separateSelected(List> list) { List> _selectedItems = []; List> _nonSelectedItems = []; _nonSelectedItems.addAll(list.where((element) => !element.selected)); _nonSelectedItems.sort((a, b) => a.label.compareTo(b.label)); _selectedItems.addAll(list.where((element) => element.selected)); _selectedItems.sort((a, b) => a.label.compareTo(b.label)); return [..._selectedItems, ..._nonSelectedItems]; } }