pickValueSet<T> method

Future<Set<T>?> pickValueSet<T>(
  1. List<T> items, {
  2. Iterable<T>? selected,
  3. String? title,
  4. Widget onItemView(
    1. T,
    2. Set<T>
    )?,
  5. Widget? onTitle(
    1. T
    )?,
  6. Widget? onSubtitle(
    1. T
    )?,
  7. Widget? onLeading(
    1. T
    )?,
  8. Widget? onTrailing(
    1. T
    )?,
  9. List<Widget>? aboveWidgets,
  10. List<Widget>? belowWidgets,
  11. EdgeInsets? padding,
  12. bool separated = true,
})

Implementation

Future<Set<T>?> pickValueSet<T>(
  List<T> items, {
  Iterable<T>? selected,
  String? title,
  Widget Function(T, Set<T>)? onItemView,
  Widget? Function(T)? onTitle,
  Widget? Function(T)? onSubtitle,
  Widget? Function(T)? onLeading,
  Widget? Function(T)? onTrailing,
  List<Widget>? aboveWidgets,
  List<Widget>? belowWidgets,
  EdgeInsets? padding,
  bool separated = true,
}) async {
  Set<T> resultSet = {};
  if (selected != null) resultSet.addAll(selected);
  return showColumn(
    isContentScrollable: true,
    title: title,
    cancel: true,
    ok: true,
    aboveWidgets: aboveWidgets,
    belowWidgets: belowWidgets,
    padding: EdgeInsets.all(0),
    onContent: (uc) {
      bool isCheck(T item) {
        Set<T> ls = uc.getResult() ?? {};
        return ls.contains(item);
      }

      void toggole(T item) {
        Set<T> ls = uc.getResult() ?? {};
        if (ls.contains(item)) {
          ls.remove(item);
        } else {
          ls.add(item);
        }
      }

      if (!uc.hasResult) uc.setResult(resultSet);
      return XListView(
        items: items,
        shrinkWrap: true,
        padding: padding,
        separator: separated,
        itemView: (cii) {
          var item = cii.item;
          if (onItemView != null) {
            Set<T> ls = uc.getResult() ?? {};
            return onItemView(item, ls);
          }
          return ListTile(
            title: onTitle?.call(item) ?? item.toString().titleMedium(),
            subtitle: onSubtitle?.call(item),
            leading: onLeading?.call(item),
            trailing: onTrailing?.call(item) ?? (isCheck(item) ? Icons.check.icon() : null),
            onTap: () {
              toggole(item);
              uc.updateState();
            },
          );
        },
      );
    },
  );
}