filterWidgetCatalog function

List<WidgetCatalogEntry> filterWidgetCatalog({
  1. String? category,
  2. String query = '',
})

Returns catalog entries matching an optional category and fuzzy text query.

Matching is case-insensitive and checks the stable ID, public name, category, and description. An empty query returns every entry in the selected category.

Implementation

List<WidgetCatalogEntry> filterWidgetCatalog({
  String? category,
  String query = '',
}) {
  final normalizedCategory = category?.trim().toLowerCase();
  final normalizedQuery = query.trim().toLowerCase();
  return widgetCatalogEntries
      .where((entry) {
        final categoryMatches =
            normalizedCategory == null ||
            normalizedCategory.isEmpty ||
            entry.category.toLowerCase() == normalizedCategory;
        if (!categoryMatches) return false;
        if (normalizedQuery.isEmpty) return true;
        final haystack =
            '${entry.id} ${entry.name} ${entry.category} '
                    '${entry.description}'
                .toLowerCase();
        return haystack.contains(normalizedQuery);
      })
      .toList(growable: false);
}