SearchBar constructor

SearchBar({
  1. required SetStateCallback setState,
  2. required AppBarCallback buildDefaultAppBar,
  3. TextFieldSubmitCallback? onSubmitted,
  4. TextEditingController? controller,
  5. String hintText = 'Search',
  6. bool inBar = true,
  7. bool closeOnSubmit = true,
  8. bool clearOnSubmit = true,
  9. bool showClearButton = true,
  10. TextFieldChangeCallback? onChanged,
  11. VoidCallback? onClosed,
  12. VoidCallback? onCleared,
})

Implementation

SearchBar({
  required this.setState,
  required this.buildDefaultAppBar,
  this.onSubmitted,
  this.controller,
  this.hintText = 'Search',
  this.inBar = true,
  this.closeOnSubmit = true,
  this.clearOnSubmit = true,
  this.showClearButton = true,
  this.onChanged,
  this.onClosed,
  this.onCleared,
}) {
  if (this.controller == null) {
    this.controller = TextEditingController();
  }

  // Don't waste resources on listeners for the text controller if the dev
  // doesn't want a clear button anyways in the search bar
  if (!this.showClearButton) {
    return;
  }

  this.controller!.addListener(() {
    if (this.controller!.text.isEmpty) {
      // If clear is already disabled, don't disable it
      if (_clearActive) {
        setState(() {
          _clearActive = false;
        });
      }

      return;
    }

    // If clear is already enabled, don't enable it
    if (!_clearActive) {
      setState(() {
        _clearActive = true;
      });
    }
  });
}