Created
June 29, 2019 10:12
-
-
Save NishantDesai1306/f3120a7bbeef5d8cafe5d4461243a565 to your computer and use it in GitHub Desktop.
wiring up the callback functions to widgets in search bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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