@@ -9,17 +9,17 @@ typedef QueryBuilder<T> = List<T> Function(
99);
1010
1111class GFSearchBar <T > extends StatefulWidget {
12- const GFSearchBar ({
13- @required this .searchList,
14- @required this .overlaySearchListItemBuilder,
15- @required this .searchQueryBuilder,
16- Key key,
17- this .onItemSelected,
18- this .hideSearchBoxWhenItemSelected = false ,
19- this .overlaySearchListHeight,
20- this .noItemsFoundWidget,
21- this .searchBoxInputDecoration
22- }) : super (key: key);
12+ const GFSearchBar (
13+ { @required this .searchList,
14+ @required this .overlaySearchListItemBuilder,
15+ @required this .searchQueryBuilder,
16+ Key key,
17+ this .onItemSelected,
18+ this .hideSearchBoxWhenItemSelected = false ,
19+ this .overlaySearchListHeight,
20+ this .noItemsFoundWidget,
21+ this .searchBoxInputDecoration})
22+ : super (key: key);
2323
2424 /// List of [text] or [widget] reference for users
2525 final List <T > searchList;
@@ -64,14 +64,15 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
6464 final LayerLink _layerLink = LayerLink ();
6565 final double textBoxHeight = 48 ;
6666 final TextEditingController textController = TextEditingController ();
67+ bool isSearchBoxSelected = false ;
6768
6869 @override
6970 void initState () {
7071 super .initState ();
7172 init ();
7273 }
7374
74- init () {
75+ init () {
7576 _searchList = < T > [];
7677 notifier = ValueNotifier (null );
7778 _focusNode = FocusNode ();
@@ -138,7 +139,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
138139 overlaySearchListHeight = widget.overlaySearchListHeight ??
139140 MediaQuery .of (context).size.height / 4 ;
140141
141- print ('yyys $isFocused ' );
142+ print ('yyys $isSearchBoxSelected ' );
142143
143144 searchBox = Container (
144145 padding: const EdgeInsets .symmetric (vertical: 12 , horizontal: 16 ),
@@ -147,29 +148,30 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
147148 controller: _controller,
148149 focusNode: _focusNode,
149150 style: TextStyle (fontSize: 16 , color: Colors .grey[600 ]),
150- decoration: widget.searchBoxInputDecoration == null ? InputDecoration (
151- enabledBorder: const OutlineInputBorder (
152- borderSide: BorderSide (
153- color: Color (0x4437474F ),
154- ),
155- ),
156- focusedBorder: OutlineInputBorder (
157- borderSide: BorderSide (
158- color: Theme .of (context).primaryColor,
159- ),
160- ),
161- suffixIcon: isFocused ? Icon (Icons .close) : Icon (Icons .search),
162- border: InputBorder .none,
163- hintText: "Search here..." ,
164- contentPadding: const EdgeInsets .only (
165- left: 16 ,
166- right: 20 ,
167- top: 14 ,
168- bottom: 14 ,
169- ),
170- ) : widget.searchBoxInputDecoration,
171- )
172- );
151+ decoration: widget.searchBoxInputDecoration == null
152+ ? InputDecoration (
153+ enabledBorder: const OutlineInputBorder (
154+ borderSide: BorderSide (
155+ color: Color (0x4437474F ),
156+ ),
157+ ),
158+ focusedBorder: OutlineInputBorder (
159+ borderSide: BorderSide (
160+ color: Theme .of (context).primaryColor,
161+ ),
162+ ),
163+ suffixIcon: Icon (Icons .search),
164+ border: InputBorder .none,
165+ hintText: "Search here..." ,
166+ contentPadding: const EdgeInsets .only (
167+ left: 16 ,
168+ right: 20 ,
169+ top: 14 ,
170+ bottom: 14 ,
171+ ),
172+ )
173+ : widget.searchBoxInputDecoration,
174+ ));
173175
174176 final searchBoxBody = Column (
175177 mainAxisSize: MainAxisSize .min,
@@ -187,6 +189,10 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
187189 return searchBoxBody;
188190 }
189191
192+ void onCloseOverlaySearchList () {
193+ onSearchListItemSelected (null );
194+ }
195+
190196 void onSearchListItemSelected (T item) {
191197 if (overlaySearchList != null ) {
192198 overlaySearchList.remove ();
@@ -206,7 +212,7 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
206212
207213 void onTextFieldFocus () {
208214 setState (() {
209- isFocused = true ;
215+ isSearchBoxSelected = true ;
210216 });
211217 final RenderBox searchBoxRenderBox = context.findRenderObject ();
212218 final RenderBox overlay = Overlay .of (context).context.findRenderObject ();
@@ -245,36 +251,39 @@ class MySingleChoiceSearchState<T> extends State<GFSearchBar<T>> {
245251 shape: const RoundedRectangleBorder (
246252 borderRadius: BorderRadius .all (Radius .circular (4 )),
247253 ),
248- title: GFListTile (
249- icon: GFIconButton (
250- icon: Icon (Icons .close),
251- onPressed: () {
252- onSearchListItemSelected (null );
253- }),
254- ),
255254 content: _searchList.isNotEmpty
256- ? Container (
257- height: overlaySearchListHeight,
258- child: Scrollbar (
259- child: ListView .separated (
260- padding: const EdgeInsets .symmetric (vertical: 4 ),
261- separatorBuilder: (context, index) => const Divider (
262- height: 1 ,
263- ),
264- itemBuilder: (context, index) => Material (
265- color: Colors .transparent,
266- child: InkWell (
267- onTap: () =>
268- onSearchListItemSelected (_searchList[index]),
269- child: widget.overlaySearchListItemBuilder (
270- _searchList.elementAt (index),
255+ ? Column (
256+ mainAxisAlignment: MainAxisAlignment .end,
257+ crossAxisAlignment: CrossAxisAlignment .end,
258+ children: < Widget > [
259+ InkWell (
260+ child: Icon (Icons .close, size: 22.0 ,),
261+ onTap: onCloseOverlaySearchList,
262+ ),
263+ Container (
264+ height: overlaySearchListHeight,
265+ child: Scrollbar (
266+ child: ListView .separated (
267+ padding: const EdgeInsets .symmetric (vertical: 4 ),
268+ separatorBuilder: (context, index) => const Divider (
269+ height: 1 ,
270+ ),
271+ itemBuilder: (context, index) => Material (
272+ color: Colors .transparent,
273+ child: InkWell (
274+ onTap: () =>
275+ onSearchListItemSelected (_searchList[index]),
276+ child: widget.overlaySearchListItemBuilder (
277+ _searchList.elementAt (index),
278+ ),
279+ ),
271280 ),
281+ itemCount: _searchList.length,
272282 ),
273283 ),
274- itemCount: _searchList.length,
275284 ),
276- ) ,
277- )
285+ ] ,
286+ )
278287 : widget.noItemsFoundWidget != null
279288 ? Center (
280289 child: widget.noItemsFoundWidget,
0 commit comments