@@ -15,6 +15,7 @@ class GFSearchBar<T> extends StatefulWidget {
1515 @required this .overlaySearchListItemBuilder,
1616 @required this .searchQueryBuilder,
1717 Key key,
18+ this .controller = TextEditingController (),
1819 this .onItemSelected,
1920 this .hideSearchBoxWhenItemSelected = false ,
2021 this .overlaySearchListHeight,
@@ -45,13 +46,16 @@ class GFSearchBar<T> extends StatefulWidget {
4546
4647 /// defines the input decoration of searchBox
4748 final InputDecoration searchBoxInputDecoration;
49+
50+ /// defines the input controller of searchBox
51+ final controller;
52+
4853
4954 @override
5055 MySingleChoiceSearchState <T > createState () => MySingleChoiceSearchState <T >();
5156}
5257
5358class MySingleChoiceSearchState <T > extends State <GFSearchBar <T >> {
54- final _controller = TextEditingController ();
5559 List <T > _list;
5660 List <T > _searchList;
5761 bool isFocused;
@@ -82,7 +86,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
8286 _searchList.addAll (_list);
8387 _focusNode.addListener (() {
8488 if (! _focusNode.hasFocus) {
85- _controller .clear ();
89+ widget.controller .clear ();
8690 if (overlaySearchList != null ) {
8791 overlaySearchList.remove ();
8892 }
@@ -98,8 +102,8 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
98102 }
99103 }
100104 });
101- _controller .addListener (() {
102- final text = _controller .text;
105+ widget.controller .addListener (() {
106+ final text = widget.controller .text;
103107 if (text.trim ().isNotEmpty) {
104108 _searchList.clear ();
105109 final filterList = widget.searchQueryBuilder (text, widget.searchList);
@@ -142,7 +146,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
142146 padding: const EdgeInsets .symmetric (vertical: 12 , horizontal: 16 ),
143147 margin: const EdgeInsets .only (bottom: 12 ),
144148 child: TextField (
145- controller: _controller ,
149+ controller: widget.controller ,
146150 focusNode: _focusNode,
147151 style: TextStyle (fontSize: 16 , color: Colors .grey[600 ]),
148152 decoration: widget.searchBoxInputDecoration == null
@@ -196,7 +200,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
196200 overlaySearchList.remove ();
197201 }
198202 overlaySearchList = null ;
199- _controller. clear () ;
203+ widget.controller = item ;
200204 _focusNode.unfocus ();
201205 setState (() {
202206 notifier.value = item;
0 commit comments