FloatingSearchBarAction.searchToClear constructor

FloatingSearchBarAction.searchToClear({
  1. double size = 24,
  2. Color? color,
  3. bool showIfClosed = true,
  4. Duration duration = const Duration(milliseconds: 900),
  5. String searchButtonSemanticLabel = 'Search',
  6. String clearButtonSemanticLabel = 'Clear',
})

A search icon that transitions into a clear icon when the query of the FloatingSearchBar is not empty.

Implementation

factory FloatingSearchBarAction.searchToClear({
  double size = 24,
  Color? color,
  bool showIfClosed = true,
  Duration duration = const Duration(milliseconds: 900),
  String searchButtonSemanticLabel = 'Search',
  String clearButtonSemanticLabel = 'Clear',
}) {
  return FloatingSearchBarAction(
    showIfOpened: true,
    showIfClosed: showIfClosed,
    builder: (context, animation) {
      final bar = FloatingSearchAppBar.of(context)!;

      return ValueListenableBuilder<String>(
        valueListenable: bar.queryNotifer,
        builder: (context, query, _) {
          final isEmpty = query.isEmpty;

          return SearchToClear(
            isEmpty: isEmpty,
            size: size,
            color: color ?? bar.style.iconColor,
            duration: duration * 0.5,
            onTap: () {
              if (!isEmpty) {
                bar.clear();
              } else {
                bar.isOpen =
                    !bar.isOpen || (!bar.hasFocus && bar.isAlwaysOpened);
              }
            },
            searchButtonSemanticLabel: searchButtonSemanticLabel,
            clearButtonSemanticLabel: clearButtonSemanticLabel,
          );
        },
      );
    },
  );
}