UI libraries / Algolia for Flutter / Widgets

About this widget

Shows the currently active refinements within a given FilterState and lets users remove filters individually.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class CurrentFilters extends StatelessWidget {
  const CurrentFilters(this.filterState, {super.key});

  final FilterState filterState;

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<Filters>(
        stream: filterState.filters,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final filters = snapshot.data!;
            return Wrap(
              spacing: 6.0,
              runSpacing: 6.0,
              children: [...inputChipsOf(filters)],
            );
          } else {
            return Container();
          }
        });
  }

  /// Get [filters] as list of [InputChip]s.
  List<Widget> inputChipsOf(Filters filters) {
    final chips = <Widget>[];
    for (var filterGroup in filters.toFilterGroups()) {
      for (var filter in filterGroup) {
        final chip = InputChip(
          label: Text(label(filter)),
          onDeleted: () => filterState.remove(filterGroup.groupID, {filter}),
          isEnabled: true,
        );
        chips.add(chip);
      }
    }
    return chips;
  }

  /// Get [filter] as a label
  String label(Filter filter) {
    if (filter is FilterFacet) {
      return filter.value;
    } else if (filter is FilterTag) {
      return filter.value;
    } else if (filter is FilterNumeric) {
      final numericValue = filter.value;
      if (numericValue is NumericRange) {
        return '${filter.attribute}: ${numericValue.lowerBound}..${numericValue.upperBound}';
      } else if (numericValue is NumericComparison) {
        return '${filter.attribute} ${numericValue.operator.operator} ${numericValue.number}';
      } else {
        throw UnsupportedError(numericValue.runtimeType.toString());
      }
    } else {
      throw UnsupportedError(runtimeType.toString());
    }
  }
}  
Did you find this page helpful?