Skip to content

Instantly share code, notes, and snippets.

@NishantDesai1306
Created June 29, 2019 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NishantDesai1306/f3120a7bbeef5d8cafe5d4461243a565 to your computer and use it in GitHub Desktop.
Save NishantDesai1306/f3120a7bbeef5d8cafe5d4461243a565 to your computer and use it in GitHub Desktop.
wiring up the callback functions to widgets in search bar
// register that SearchBar will need these callbacks
SearchBar({
Key key,
@required this.onCancelSearch,
@required this.onSearchQueryChanged,
}) : super(key: key);
final VoidCallback onCancelSearch;
final Function(String) onSearchQueryChanged;
// callback function when suffix button is tapped
clearSearchQuery() {
// clear the input field
_searchFieldController.clear();
// notify DefaultAppBar by calling onSearchQueryChanged with ''
widget.onSearchQueryChanged('');
}
// wire onChange event of search input with onSearchQueryChanged
// and also wire up clear button
TextField(
controller: _searchFieldController,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white),
decoration: InputDecoration(
border: InputBorder.none,
prefixIcon: Icon(Icons.search, color: Colors.white),
hintText: "Search...",
hintStyle: TextStyle(color: Colors.white),
suffixIcon: InkWell(
child: Icon(Icons.close, color: Colors.white),
// map clear button with it's callback function
onTap: clearSearchQuery,
),
),
// wiring up the callback function
onChanged: widget.onSearchQueryChanged,
)
// finally notify DefaultAppBar to stop search mode by calling onCancelSearch
// when user presses on back button.
IconButton(
icon: Icon(Icons.arrow_back, color: Colors.white),
onPressed: widget.onCancelSearch,
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment