@@ -15,6 +15,7 @@ class GFSearchBar<T> extends StatefulWidget {
1515 @required this .overlaySearchListItemBuilder,
1616 @required this .searchQueryBuilder,
1717 Key key,
18+ this .controller,
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 TextEditingController 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;
@@ -64,7 +68,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
6468 double overlaySearchListHeight;
6569 final LayerLink _layerLink = LayerLink ();
6670 final double textBoxHeight = 48 ;
67- final TextEditingController textController = TextEditingController ();
71+ TextEditingController textController = TextEditingController ();
6872 bool isSearchBoxSelected = false ;
6973
7074 @override
@@ -75,14 +79,15 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
7579
7680 void init () {
7781 _searchList = < T > [];
82+ textController = widget.controller ?? textController;
7883 notifier = ValueNotifier (null );
7984 _focusNode = FocusNode ();
8085 isFocused = false ;
8186 _list = List <T >.from (widget.searchList);
8287 _searchList.addAll (_list);
8388 _focusNode.addListener (() {
8489 if (! _focusNode.hasFocus) {
85- _controller .clear ();
90+ textController .clear ();
8691 if (overlaySearchList != null ) {
8792 overlaySearchList.remove ();
8893 }
@@ -98,8 +103,8 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
98103 }
99104 }
100105 });
101- _controller .addListener (() {
102- final text = _controller .text;
106+ textController .addListener (() {
107+ final text = textController .text;
103108 if (text.trim ().isNotEmpty) {
104109 _searchList.clear ();
105110 final filterList = widget.searchQueryBuilder (text, widget.searchList);
@@ -142,7 +147,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
142147 padding: const EdgeInsets .symmetric (vertical: 12 , horizontal: 16 ),
143148 margin: const EdgeInsets .only (bottom: 12 ),
144149 child: TextField (
145- controller: _controller ,
150+ controller: textController ,
146151 focusNode: _focusNode,
147152 style: TextStyle (fontSize: 16 , color: Colors .grey[600 ]),
148153 decoration: widget.searchBoxInputDecoration == null
@@ -196,7 +201,6 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
196201 overlaySearchList.remove ();
197202 }
198203 overlaySearchList = null ;
199- _controller.clear ();
200204 _focusNode.unfocus ();
201205 setState (() {
202206 notifier.value = item;
0 commit comments