SearchBar constructor
SearchBar({
- required SetStateCallback setState,
- required AppBarCallback buildDefaultAppBar,
- TextFieldSubmitCallback? onSubmitted,
- TextEditingController? controller,
- String hintText = 'Search',
- bool inBar = true,
- bool closeOnSubmit = true,
- bool clearOnSubmit = true,
- bool showClearButton = true,
- TextFieldChangeCallback? onChanged,
- VoidCallback? onClosed,
- 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;
});
}
});
}