@@ -15,7 +15,7 @@ class GFSearchBar<T> extends StatefulWidget {
1515 @required this .overlaySearchListItemBuilder,
1616 @required this .searchQueryBuilder,
1717 Key key,
18- this .controller = const TextEditingController () ,
18+ this .controller,
1919 this .onItemSelected,
2020 this .hideSearchBoxWhenItemSelected = false ,
2121 this .overlaySearchListHeight,
@@ -68,7 +68,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
6868 double overlaySearchListHeight;
6969 final LayerLink _layerLink = LayerLink ();
7070 final double textBoxHeight = 48 ;
71- final TextEditingController textController = TextEditingController ();
71+ TextEditingController textController = TextEditingController ();
7272 bool isSearchBoxSelected = false ;
7373
7474 @override
@@ -79,14 +79,15 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
7979
8080 void init () {
8181 _searchList = < T > [];
82+ textController = widget.controller ?? textController;
8283 notifier = ValueNotifier (null );
8384 _focusNode = FocusNode ();
8485 isFocused = false ;
8586 _list = List <T >.from (widget.searchList);
8687 _searchList.addAll (_list);
8788 _focusNode.addListener (() {
8889 if (! _focusNode.hasFocus) {
89- widget.controller .clear ();
90+ textController .clear ();
9091 if (overlaySearchList != null ) {
9192 overlaySearchList.remove ();
9293 }
@@ -102,8 +103,8 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
102103 }
103104 }
104105 });
105- widget.controller .addListener (() {
106- final text = widget.controller .text;
106+ textController .addListener (() {
107+ final text = textController .text;
107108 if (text.trim ().isNotEmpty) {
108109 _searchList.clear ();
109110 final filterList = widget.searchQueryBuilder (text, widget.searchList);
@@ -146,7 +147,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
146147 padding: const EdgeInsets .symmetric (vertical: 12 , horizontal: 16 ),
147148 margin: const EdgeInsets .only (bottom: 12 ),
148149 child: TextField (
149- controller: widget.controller ,
150+ controller: textController ,
150151 focusNode: _focusNode,
151152 style: TextStyle (fontSize: 16 , color: Colors .grey[600 ]),
152153 decoration: widget.searchBoxInputDecoration == null
0 commit comments