showBasicOptionsKDialog<T extends SelectOption> function

Future<List<T>?> showBasicOptionsKDialog<T extends SelectOption>(
  1. BuildContext context, {
  2. required List<T> options,
  3. List<String> initialSelection = const [],
  4. bool allowMultipleSelection = false,
  5. bool searchInput = false,
  6. String? title,
  7. String? acceptText,
  8. String? cancelText,
  9. bool useMaxHeight = false,
})

Implementation

Future<List<T>?> showBasicOptionsKDialog<T extends SelectOption>(
  BuildContext context, {
  required List<T> options,
  List<String> initialSelection = const [],
  bool allowMultipleSelection = false,
  bool searchInput = false,
  String? title,
  String? acceptText,
  String? cancelText,
  bool useMaxHeight = false,
}) async {
  acceptText ??= strings.acceptButtonText;
  cancelText ??= strings.cancelButtonText;

  var selectedOptions = options
      .where((opt) => initialSelection.contains(opt.getID()))
      .toSet(); // Evita duplicados

  bool isSelected(T itm) => selectedOptions.contains(itm);
  void select(T itm) {
    if (!allowMultipleSelection) selectedOptions.clear();
    selectedOptions.add(itm);
  }

  void unselect(T itm) => selectedOptions.remove(itm);

  final result = await showDialog<bool>(
    context: context,
    barrierDismissible: false,
    builder: (context) {
      return PopScope(
        canPop: false,
        child: AlertDialog(
          title: title != null ? Text(title) : null,
          content: _Content<T>(
            useMaxHeight: useMaxHeight,
            options: options,
            select: select,
            unselect: unselect,
            isSelected: isSelected,
            searchInput: searchInput,
          ),
          actionsPadding: title == null
              ? const EdgeInsets.only(right: 24, bottom: 8)
              : null,
          actions: [
            TextButton(
              onPressed: () => Navigator.of(context).pop(false),
              child: Text(
                cancelText!,
                style: const TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
            TextButton(
              onPressed: () => Navigator.of(context).pop(true),
              child: Text(
                acceptText!,
                style: const TextStyle(fontWeight: FontWeight.bold),
              ),
            ),
          ],
        ),
      );
    },
  );
  return result == true ? selectedOptions.toList() : null;
}