@@ -4,41 +4,56 @@ import 'dart:math';
4
4
class AnimSearchBar extends StatefulWidget {
5
5
/// width - double ,isRequired : Yes
6
6
/// textController - TextEditingController ,isRequired : Yes
7
- /// rtl - Boolean, isRequired : No
8
7
/// onSuffixTap - Function, isRequired : Yes
8
+ /// rtl - Boolean, isRequired : No
9
+ /// autoFocus - Boolean, isRequired : No
10
+ /// style - TextStyle, isRequired : No
11
+ /// closeSearchOnSuffixTap - bool , isRequired : No
9
12
/// suffixIcon - IconData ,isRequired : No
10
13
/// prefixIcon - IconData ,isRequired : No
11
14
/// animationDurationInMilli - int ,isRequired : No
12
15
/// helpText - String ,isRequired : No
13
16
14
17
final double width;
15
18
final TextEditingController textController;
16
- final IconData suffixIcon;
17
- final IconData prefixIcon;
19
+ final Icon suffixIcon;
20
+ final Icon prefixIcon;
18
21
final String helpText;
19
22
final int animationDurationInMilli;
20
23
final onSuffixTap;
21
24
final bool rtl;
25
+ final bool autoFocus;
26
+ final TextStyle style;
27
+ final bool closeSearchOnSuffixTap;
28
+
29
+ const AnimSearchBar ({
30
+ Key key,
31
+
32
+ /// The width cannot be null
33
+ @required this .width,
22
34
23
- const AnimSearchBar (
24
- {Key key,
35
+ /// The textController cannot be null
36
+ @required this .textController,
37
+ this .suffixIcon,
38
+ this .prefixIcon,
39
+ this .helpText = "Search..." ,
25
40
26
- /// The width cannot be null
27
- @required this .width,
41
+ /// The onSuffixTap cannot be null
42
+ @required this .onSuffixTap,
43
+ this .animationDurationInMilli = 375 ,
28
44
29
- /// The textController cannot be null
30
- @required this .textController,
31
- this .suffixIcon = Icons .close,
32
- this .prefixIcon = Icons .search,
33
- this .helpText = "Search..." ,
45
+ /// make the search bar to open from right to left
46
+ this .rtl = false ,
34
47
35
- /// The onSuffixTap cannot be null
36
- @required this .onSuffixTap,
37
- this .animationDurationInMilli = 375 ,
48
+ /// make the keyboard to show automatically when the searchbar is expanded
49
+ this .autoFocus = false ,
38
50
39
- /// make the search bar to open from right to left
40
- this .rtl = false })
41
- : assert (
51
+ /// TextStyle of the contents inside the searchbar
52
+ this .style,
53
+
54
+ /// close the search on suffix tap
55
+ this .closeSearchOnSuffixTap = false ,
56
+ }) : assert (
42
57
/// The width cannot be null and double.infinity
43
58
width != null && width != double .infinity,
44
59
@@ -58,6 +73,7 @@ class _AnimSearchBarState extends State<AnimSearchBar>
58
73
with SingleTickerProviderStateMixin {
59
74
///initializing the AnimationController
60
75
AnimationController _con;
76
+ FocusNode focusNode = FocusNode ();
61
77
@override
62
78
void initState () {
63
79
super .initState ();
@@ -120,15 +136,27 @@ class _AnimSearchBarState extends State<AnimSearchBar>
120
136
try {
121
137
///trying to execute the onSuffixTap function
122
138
widget.onSuffixTap ();
139
+
140
+ ///closeSearchOnSuffixTap will execute if it's true
141
+ if (widget.closeSearchOnSuffixTap) {
142
+ FocusScope .of (context).unfocus ();
143
+ setState (() {
144
+ toggle = 0 ;
145
+ });
146
+ }
123
147
} catch (e) {
124
148
///print the error if the try block fails
125
149
print (e);
126
150
}
127
151
},
128
- child: Icon (
129
- widget.suffixIcon,
130
- size: 20.0 ,
131
- ),
152
+
153
+ ///suffixIcon is of type Icon
154
+ child: widget.suffixIcon != null
155
+ ? widget.suffixIcon
156
+ : Icon (
157
+ Icons .close,
158
+ size: 20.0 ,
159
+ ),
132
160
),
133
161
builder: (context, widget) {
134
162
///Using Transform.rotate to rotate the suffix icon when it gets expanded
@@ -158,8 +186,14 @@ class _AnimSearchBarState extends State<AnimSearchBar>
158
186
child: TextField (
159
187
///Text Controller. you can manipulate the text inside this textField by calling this controller.
160
188
controller: widget.textController,
189
+ focusNode: focusNode,
161
190
cursorRadius: Radius .circular (10.0 ),
162
191
cursorWidth: 2.0 ,
192
+
193
+ ///style is of type TextStyle, the default is just a color black
194
+ style: widget.style != null
195
+ ? widget.style
196
+ : TextStyle (color: Colors .black),
163
197
cursorColor: Colors .black,
164
198
decoration: InputDecoration (
165
199
floatingLabelBehavior: FloatingLabelBehavior .never,
@@ -189,21 +223,39 @@ class _AnimSearchBarState extends State<AnimSearchBar>
189
223
190
224
///if toggle is 1, which means it's open. so show the back icon, which will close it.
191
225
///if the toggle is 0, which means it's closed, so tapping on it will expand the widget.
192
- icon: Icon (
193
- toggle == 1 ? Icons .arrow_back_ios : widget.prefixIcon),
226
+ ///prefixIcon is of type Icon
227
+ icon: widget.prefixIcon != null
228
+ ? toggle == 1
229
+ ? Icon (Icons .arrow_back_ios)
230
+ : widget.prefixIcon
231
+ : Icon (
232
+ toggle == 1 ? Icons .arrow_back_ios : Icons .search,
233
+ size: 20.0 ,
234
+ ),
194
235
onPressed: () {
195
236
setState (
196
237
() {
197
238
///if the search bar is closed
198
239
if (toggle == 0 ) {
199
240
toggle = 1 ;
241
+ setState (() {
242
+ ///if the autoFocus is true, the keyboard will pop open, automatically
243
+ if (widget.autoFocus)
244
+ FocusScope .of (context).requestFocus (focusNode);
245
+ });
200
246
201
247
///forward == expand
202
248
_con.forward ();
203
249
} else {
204
250
///if the search bar is expanded
205
251
toggle = 0 ;
206
252
253
+ ///if the autoFocus is true, the keyboard will close, automatically
254
+ setState (() {
255
+ if (widget.autoFocus)
256
+ FocusScope .of (context).unfocus ();
257
+ });
258
+
207
259
///reverse == close
208
260
_con.reverse ();
209
261
}
0 commit comments